OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env ruby |
| 2 |
| 3 # Copyright 2015, Google Inc. |
| 4 # All rights reserved. |
| 5 # |
| 6 # Redistribution and use in source and binary forms, with or without |
| 7 # modification, are permitted provided that the following conditions are |
| 8 # met: |
| 9 # |
| 10 # * Redistributions of source code must retain the above copyright |
| 11 # notice, this list of conditions and the following disclaimer. |
| 12 # * Redistributions in binary form must reproduce the above |
| 13 # copyright notice, this list of conditions and the following disclaimer |
| 14 # in the documentation and/or other materials provided with the |
| 15 # distribution. |
| 16 # * Neither the name of Google Inc. nor the names of its |
| 17 # contributors may be used to endorse or promote products derived from |
| 18 # this software without specific prior written permission. |
| 19 # |
| 20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 # Sample app that accesses a Calc service running on a Ruby gRPC server and |
| 33 # helps validate RpcServer as a gRPC server using proto2 serialization. |
| 34 # |
| 35 # Usage: $ path/to/math_client.rb |
| 36 |
| 37 this_dir = File.expand_path(File.dirname(__FILE__)) |
| 38 lib_dir = File.join(File.dirname(this_dir), 'lib') |
| 39 $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir) |
| 40 $LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir) |
| 41 |
| 42 require 'grpc' |
| 43 require 'math_services' |
| 44 require 'optparse' |
| 45 |
| 46 include GRPC::Core::TimeConsts |
| 47 |
| 48 def do_div(stub) |
| 49 GRPC.logger.info('request_response') |
| 50 GRPC.logger.info('----------------') |
| 51 req = Math::DivArgs.new(dividend: 7, divisor: 3) |
| 52 GRPC.logger.info("div(7/3): req=#{req.inspect}") |
| 53 resp = stub.div(req, timeout: INFINITE_FUTURE) |
| 54 GRPC.logger.info("Answer: #{resp.inspect}") |
| 55 GRPC.logger.info('----------------') |
| 56 end |
| 57 |
| 58 def do_sum(stub) |
| 59 # to make client streaming requests, pass an enumerable of the inputs |
| 60 GRPC.logger.info('client_streamer') |
| 61 GRPC.logger.info('---------------') |
| 62 reqs = [1, 2, 3, 4, 5].map { |x| Math::Num.new(num: x) } |
| 63 GRPC.logger.info("sum(1, 2, 3, 4, 5): reqs=#{reqs.inspect}") |
| 64 resp = stub.sum(reqs) # reqs.is_a?(Enumerable) |
| 65 GRPC.logger.info("Answer: #{resp.inspect}") |
| 66 GRPC.logger.info('---------------') |
| 67 end |
| 68 |
| 69 def do_fib(stub) |
| 70 GRPC.logger.info('server_streamer') |
| 71 GRPC.logger.info('----------------') |
| 72 req = Math::FibArgs.new(limit: 11) |
| 73 GRPC.logger.info("fib(11): req=#{req.inspect}") |
| 74 resp = stub.fib(req, timeout: INFINITE_FUTURE) |
| 75 resp.each do |r| |
| 76 GRPC.logger.info("Answer: #{r.inspect}") |
| 77 end |
| 78 GRPC.logger.info('----------------') |
| 79 end |
| 80 |
| 81 def do_div_many(stub) |
| 82 GRPC.logger.info('bidi_streamer') |
| 83 GRPC.logger.info('-------------') |
| 84 reqs = [] |
| 85 reqs << Math::DivArgs.new(dividend: 7, divisor: 3) |
| 86 reqs << Math::DivArgs.new(dividend: 5, divisor: 2) |
| 87 reqs << Math::DivArgs.new(dividend: 7, divisor: 2) |
| 88 GRPC.logger.info("div(7/3), div(5/2), div(7/2): reqs=#{reqs.inspect}") |
| 89 resp = stub.div_many(reqs, timeout: INFINITE_FUTURE) |
| 90 resp.each do |r| |
| 91 GRPC.logger.info("Answer: #{r.inspect}") |
| 92 end |
| 93 GRPC.logger.info('----------------') |
| 94 end |
| 95 |
| 96 def load_test_certs |
| 97 this_dir = File.expand_path(File.dirname(__FILE__)) |
| 98 data_dir = File.join(File.dirname(this_dir), 'spec/testdata') |
| 99 files = ['ca.pem', 'server1.key', 'server1.pem'] |
| 100 files.map { |f| File.open(File.join(data_dir, f)).read } |
| 101 end |
| 102 |
| 103 def test_creds |
| 104 certs = load_test_certs |
| 105 GRPC::Core::ChannelCredentials.new(certs[0]) |
| 106 end |
| 107 |
| 108 def main |
| 109 options = { |
| 110 'host' => 'localhost:7071', |
| 111 'secure' => false |
| 112 } |
| 113 OptionParser.new do |opts| |
| 114 opts.banner = 'Usage: [--host <hostname>:<port>] [--secure|-s]' |
| 115 opts.on('--host HOST', '<hostname>:<port>') do |v| |
| 116 options['host'] = v |
| 117 end |
| 118 opts.on('-s', '--secure', 'access using test creds') do |v| |
| 119 options['secure'] = v |
| 120 end |
| 121 end.parse! |
| 122 |
| 123 # The Math::Math:: module occurs because the service has the same name as its |
| 124 # package. That practice should be avoided by defining real services. |
| 125 |
| 126 p options |
| 127 if options['secure'] |
| 128 stub_opts = { |
| 129 :creds => test_creds, |
| 130 GRPC::Core::Channel::SSL_TARGET => 'foo.test.google.fr' |
| 131 } |
| 132 p stub_opts |
| 133 p options['host'] |
| 134 stub = Math::Math::Stub.new(options['host'], **stub_opts) |
| 135 GRPC.logger.info("... connecting securely on #{options['host']}") |
| 136 else |
| 137 stub = Math::Math::Stub.new(options['host']) |
| 138 GRPC.logger.info("... connecting insecurely on #{options['host']}") |
| 139 end |
| 140 |
| 141 do_div(stub) |
| 142 do_sum(stub) |
| 143 do_fib(stub) |
| 144 do_div_many(stub) |
| 145 end |
| 146 |
| 147 main |
OLD | NEW |