Tim Emiola | 3acf05a | 2015-01-13 07:55:34 -0800 | [diff] [blame] | 1 | #!/usr/bin/env ruby |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | # Copyright 2015 gRPC authors. |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 4 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 8 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 10 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 16 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 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' |
Ken Payson | 5a2c918 | 2016-07-26 17:15:08 -0700 | [diff] [blame] | 28 | require 'math_services_pb' |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 29 | require 'optparse' |
| 30 | |
| 31 | include GRPC::Core::TimeConsts |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 32 | |
| 33 | def do_div(stub) |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 34 | GRPC.logger.info('request_response') |
| 35 | GRPC.logger.info('----------------') |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 36 | req = Math::DivArgs.new(dividend: 7, divisor: 3) |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 37 | GRPC.logger.info("div(7/3): req=#{req.inspect}") |
Tim Emiola | 81d950a | 2015-08-28 16:57:28 -0700 | [diff] [blame] | 38 | resp = stub.div(req, timeout: INFINITE_FUTURE) |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 39 | GRPC.logger.info("Answer: #{resp.inspect}") |
| 40 | GRPC.logger.info('----------------') |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 41 | end |
| 42 | |
| 43 | def do_sum(stub) |
| 44 | # to make client streaming requests, pass an enumerable of the inputs |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 45 | GRPC.logger.info('client_streamer') |
| 46 | GRPC.logger.info('---------------') |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 47 | reqs = [1, 2, 3, 4, 5].map { |x| Math::Num.new(num: x) } |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 48 | GRPC.logger.info("sum(1, 2, 3, 4, 5): reqs=#{reqs.inspect}") |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 49 | resp = stub.sum(reqs) # reqs.is_a?(Enumerable) |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 50 | GRPC.logger.info("Answer: #{resp.inspect}") |
| 51 | GRPC.logger.info('---------------') |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 52 | end |
| 53 | |
| 54 | def do_fib(stub) |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 55 | GRPC.logger.info('server_streamer') |
| 56 | GRPC.logger.info('----------------') |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 57 | req = Math::FibArgs.new(limit: 11) |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 58 | GRPC.logger.info("fib(11): req=#{req.inspect}") |
Tim Emiola | 81d950a | 2015-08-28 16:57:28 -0700 | [diff] [blame] | 59 | resp = stub.fib(req, timeout: INFINITE_FUTURE) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 60 | resp.each do |r| |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 61 | GRPC.logger.info("Answer: #{r.inspect}") |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 62 | end |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 63 | GRPC.logger.info('----------------') |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 64 | end |
| 65 | |
| 66 | def do_div_many(stub) |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 67 | GRPC.logger.info('bidi_streamer') |
| 68 | GRPC.logger.info('-------------') |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 69 | reqs = [] |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 70 | reqs << Math::DivArgs.new(dividend: 7, divisor: 3) |
Tim Emiola | 9fd6770 | 2015-02-09 13:00:54 -0800 | [diff] [blame] | 71 | reqs << Math::DivArgs.new(dividend: 5, divisor: 2) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 72 | reqs << Math::DivArgs.new(dividend: 7, divisor: 2) |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 73 | GRPC.logger.info("div(7/3), div(5/2), div(7/2): reqs=#{reqs.inspect}") |
Tim Emiola | 81d950a | 2015-08-28 16:57:28 -0700 | [diff] [blame] | 74 | resp = stub.div_many(reqs, timeout: INFINITE_FUTURE) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 75 | resp.each do |r| |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 76 | GRPC.logger.info("Answer: #{r.inspect}") |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 77 | end |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 78 | GRPC.logger.info('----------------') |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 79 | end |
| 80 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 81 | def load_test_certs |
| 82 | this_dir = File.expand_path(File.dirname(__FILE__)) |
| 83 | data_dir = File.join(File.dirname(this_dir), 'spec/testdata') |
| 84 | files = ['ca.pem', 'server1.key', 'server1.pem'] |
| 85 | files.map { |f| File.open(File.join(data_dir, f)).read } |
| 86 | end |
| 87 | |
| 88 | def test_creds |
| 89 | certs = load_test_certs |
Tim Emiola | 43a7e4e | 2015-10-28 00:17:14 -0700 | [diff] [blame] | 90 | GRPC::Core::ChannelCredentials.new(certs[0]) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 91 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 92 | |
| 93 | def main |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 94 | options = { |
| 95 | 'host' => 'localhost:7071', |
| 96 | 'secure' => false |
| 97 | } |
| 98 | OptionParser.new do |opts| |
temiola | 0f0a6bc | 2015-01-07 18:43:40 -0800 | [diff] [blame] | 99 | opts.banner = 'Usage: [--host <hostname>:<port>] [--secure|-s]' |
| 100 | opts.on('--host HOST', '<hostname>:<port>') do |v| |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 101 | options['host'] = v |
| 102 | end |
| 103 | opts.on('-s', '--secure', 'access using test creds') do |v| |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 104 | options['secure'] = v |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 105 | end |
| 106 | end.parse! |
| 107 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 108 | # The Math::Math:: module occurs because the service has the same name as its |
| 109 | # package. That practice should be avoided by defining real services. |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 110 | |
| 111 | p options |
| 112 | if options['secure'] |
| 113 | stub_opts = { |
| 114 | :creds => test_creds, |
Julien Boeuf | 597a4f2 | 2015-02-23 15:57:14 -0800 | [diff] [blame] | 115 | GRPC::Core::Channel::SSL_TARGET => 'foo.test.google.fr' |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 116 | } |
| 117 | p stub_opts |
| 118 | p options['host'] |
| 119 | stub = Math::Math::Stub.new(options['host'], **stub_opts) |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 120 | GRPC.logger.info("... connecting securely on #{options['host']}") |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 121 | else |
| 122 | stub = Math::Math::Stub.new(options['host']) |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 123 | GRPC.logger.info("... connecting insecurely on #{options['host']}") |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 124 | end |
| 125 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 126 | do_div(stub) |
| 127 | do_sum(stub) |
| 128 | do_fib(stub) |
| 129 | do_div_many(stub) |
| 130 | end |
| 131 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 132 | main |