Debian Codegraph @

src/ruby/bin/math_client.rb

raw
1 #!/usr/bin/env ruby
2
3 # Copyright 2015 gRPC authors.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Sample app that accesses a Calc service running on a Ruby gRPC server and
18 # helps validate RpcServer as a gRPC server using proto2 serialization.
19 #
20 # Usage: $ path/to/math_client.rb
21
22 this_dir = File.expand_path(File.dirname(__FILE__))
23 lib_dir = File.join(File.dirname(this_dir), 'lib')
24 $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
25 $LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir)
26
27 require 'grpc'
28 require 'math_services_pb'
29 require 'optparse'
30 require 'logger'
31
32 include GRPC::Core::TimeConsts
33
34 module StdoutLogger
35 def logger
36 LOGGER
37 end
38
39 LOGGER = Logger.new(STDOUT)
40 end
41
42 GRPC.extend(StdoutLogger)
43
44 def do_div(stub)
45 GRPC.logger.info('request_response')
46 GRPC.logger.info('----------------')
47 req = Math::DivArgs.new(dividend: 7, divisor: 3)
48 GRPC.logger.info("div(7/3): req=#{req.inspect}")
49 resp = stub.div(req)
50 GRPC.logger.info("Answer: #{resp.inspect}")
51 GRPC.logger.info('----------------')
52 end
53
54 def do_sum(stub)
55 # to make client streaming requests, pass an enumerable of the inputs
56 GRPC.logger.info('client_streamer')
57 GRPC.logger.info('---------------')
58 reqs = [1, 2, 3, 4, 5].map { |x| Math::Num.new(num: x) }
59 GRPC.logger.info("sum(1, 2, 3, 4, 5): reqs=#{reqs.inspect}")
60 resp = stub.sum(reqs) # reqs.is_a?(Enumerable)
61 GRPC.logger.info("Answer: #{resp.inspect}")
62 GRPC.logger.info('---------------')
63 end
64
65 def do_fib(stub)
66 GRPC.logger.info('server_streamer')
67 GRPC.logger.info('----------------')
68 req = Math::FibArgs.new(limit: 11)
69 GRPC.logger.info("fib(11): req=#{req.inspect}")
70 resp = stub.fib(req)
71 resp.each do |r|
72 GRPC.logger.info("Answer: #{r.inspect}")
73 end
74 GRPC.logger.info('----------------')
75 end
76
77 def do_div_many(stub)
78 GRPC.logger.info('bidi_streamer')
79 GRPC.logger.info('-------------')
80 reqs = []
81 reqs << Math::DivArgs.new(dividend: 7, divisor: 3)
82 reqs << Math::DivArgs.new(dividend: 5, divisor: 2)
83 reqs << Math::DivArgs.new(dividend: 7, divisor: 2)
84 GRPC.logger.info("div(7/3), div(5/2), div(7/2): reqs=#{reqs.inspect}")
85 resp = stub.div_many(reqs)
86 resp.each do |r|
87 GRPC.logger.info("Answer: #{r.inspect}")
88 end
89 GRPC.logger.info('----------------')
90 end
91
92 def load_test_certs
93 this_dir = File.expand_path(File.dirname(__FILE__))
94 data_dir = File.join(File.dirname(this_dir), 'spec/testdata')
95 files = ['ca.pem', 'server1.key', 'server1.pem']
96 files.map { |f| File.open(File.join(data_dir, f)).read }
97 end
98
99 def test_creds
100 certs = load_test_certs
101 GRPC::Core::ChannelCredentials.new(certs[0])
102 end
103
104 def main
105 options = {
106 'host' => 'localhost:7071',
107 'secure' => false
108 }
109 OptionParser.new do |opts|
110 opts.banner = 'Usage: [--host <hostname>:<port>] [--secure|-s]'
111 opts.on('--host HOST', '<hostname>:<port>') do |v|
112 options['host'] = v
113 end
114 opts.on('-s', '--secure', 'access using test creds') do |v|
115 options['secure'] = v
116 end
117 end.parse!
118
119 # The Math::Math:: module occurs because the service has the same name as its
120 # package. That practice should be avoided by defining real services.
121 if options['secure']
122 stub_opts = {
123 :creds => test_creds,
124 GRPC::Core::Channel::SSL_TARGET => 'foo.test.google.fr',
125 timeout: INFINITE_FUTURE,
126 }
127 stub = Math::Math::Stub.new(options['host'], **stub_opts)
128 GRPC.logger.info("... connecting securely on #{options['host']}")
129 else
130 stub = Math::Math::Stub.new(options['host'], :this_channel_is_insecure, timeout: INFINITE_FUTURE)
131 GRPC.logger.info("... connecting insecurely on #{options['host']}")
132 end
133
134 do_div(stub)
135 do_sum(stub)
136 do_fib(stub)
137 do_div_many(stub)
138 end
139
140 main