blob: 15120c0c0dd19f2919fc3c36cb8ce666c4db8e9f [file] [log] [blame]
Tim Emiola3acf05a2015-01-13 07:55:34 -08001#!/usr/bin/env ruby
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003# Copyright 2015 gRPC authors.
nnoble097ef9b2014-12-01 17:06:10 -08004#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005# 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
nnoble097ef9b2014-12-01 17:06:10 -08008#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009# http://www.apache.org/licenses/LICENSE-2.0
nnoble097ef9b2014-12-01 17:06:10 -080010#
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011# 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.
nnoble097ef9b2014-12-01 17:06:10 -080016
nnoble097ef9b2014-12-01 17:06:10 -080017# 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
22this_dir = File.expand_path(File.dirname(__FILE__))
23lib_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
27require 'grpc'
Ken Payson5a2c9182016-07-26 17:15:08 -070028require 'math_services_pb'
nnoble0c475f02014-12-05 15:37:39 -080029require 'optparse'
30
31include GRPC::Core::TimeConsts
nnoble097ef9b2014-12-01 17:06:10 -080032
33def do_div(stub)
Nick Gauthierf233d962015-05-20 14:02:50 -040034 GRPC.logger.info('request_response')
35 GRPC.logger.info('----------------')
Tim Emiolae2860c52015-01-16 02:58:41 -080036 req = Math::DivArgs.new(dividend: 7, divisor: 3)
Nick Gauthierf233d962015-05-20 14:02:50 -040037 GRPC.logger.info("div(7/3): req=#{req.inspect}")
Tim Emiola81d950a2015-08-28 16:57:28 -070038 resp = stub.div(req, timeout: INFINITE_FUTURE)
Nick Gauthierf233d962015-05-20 14:02:50 -040039 GRPC.logger.info("Answer: #{resp.inspect}")
40 GRPC.logger.info('----------------')
nnoble097ef9b2014-12-01 17:06:10 -080041end
42
43def do_sum(stub)
44 # to make client streaming requests, pass an enumerable of the inputs
Nick Gauthierf233d962015-05-20 14:02:50 -040045 GRPC.logger.info('client_streamer')
46 GRPC.logger.info('---------------')
Tim Emiolae2860c52015-01-16 02:58:41 -080047 reqs = [1, 2, 3, 4, 5].map { |x| Math::Num.new(num: x) }
Nick Gauthierf233d962015-05-20 14:02:50 -040048 GRPC.logger.info("sum(1, 2, 3, 4, 5): reqs=#{reqs.inspect}")
nnoble097ef9b2014-12-01 17:06:10 -080049 resp = stub.sum(reqs) # reqs.is_a?(Enumerable)
Nick Gauthierf233d962015-05-20 14:02:50 -040050 GRPC.logger.info("Answer: #{resp.inspect}")
51 GRPC.logger.info('---------------')
nnoble097ef9b2014-12-01 17:06:10 -080052end
53
54def do_fib(stub)
Nick Gauthierf233d962015-05-20 14:02:50 -040055 GRPC.logger.info('server_streamer')
56 GRPC.logger.info('----------------')
Tim Emiolae2860c52015-01-16 02:58:41 -080057 req = Math::FibArgs.new(limit: 11)
Nick Gauthierf233d962015-05-20 14:02:50 -040058 GRPC.logger.info("fib(11): req=#{req.inspect}")
Tim Emiola81d950a2015-08-28 16:57:28 -070059 resp = stub.fib(req, timeout: INFINITE_FUTURE)
nnoble097ef9b2014-12-01 17:06:10 -080060 resp.each do |r|
Nick Gauthierf233d962015-05-20 14:02:50 -040061 GRPC.logger.info("Answer: #{r.inspect}")
nnoble097ef9b2014-12-01 17:06:10 -080062 end
Nick Gauthierf233d962015-05-20 14:02:50 -040063 GRPC.logger.info('----------------')
nnoble097ef9b2014-12-01 17:06:10 -080064end
65
66def do_div_many(stub)
Nick Gauthierf233d962015-05-20 14:02:50 -040067 GRPC.logger.info('bidi_streamer')
68 GRPC.logger.info('-------------')
nnoble097ef9b2014-12-01 17:06:10 -080069 reqs = []
Tim Emiolae2860c52015-01-16 02:58:41 -080070 reqs << Math::DivArgs.new(dividend: 7, divisor: 3)
Tim Emiola9fd67702015-02-09 13:00:54 -080071 reqs << Math::DivArgs.new(dividend: 5, divisor: 2)
Tim Emiolae2860c52015-01-16 02:58:41 -080072 reqs << Math::DivArgs.new(dividend: 7, divisor: 2)
Nick Gauthierf233d962015-05-20 14:02:50 -040073 GRPC.logger.info("div(7/3), div(5/2), div(7/2): reqs=#{reqs.inspect}")
Tim Emiola81d950a2015-08-28 16:57:28 -070074 resp = stub.div_many(reqs, timeout: INFINITE_FUTURE)
nnoble097ef9b2014-12-01 17:06:10 -080075 resp.each do |r|
Nick Gauthierf233d962015-05-20 14:02:50 -040076 GRPC.logger.info("Answer: #{r.inspect}")
nnoble097ef9b2014-12-01 17:06:10 -080077 end
Nick Gauthierf233d962015-05-20 14:02:50 -040078 GRPC.logger.info('----------------')
nnoble097ef9b2014-12-01 17:06:10 -080079end
80
nnoble0c475f02014-12-05 15:37:39 -080081def 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 }
86end
87
88def test_creds
89 certs = load_test_certs
Tim Emiola43a7e4e2015-10-28 00:17:14 -070090 GRPC::Core::ChannelCredentials.new(certs[0])
nnoble0c475f02014-12-05 15:37:39 -080091end
nnoble097ef9b2014-12-01 17:06:10 -080092
93def main
nnoble0c475f02014-12-05 15:37:39 -080094 options = {
95 'host' => 'localhost:7071',
96 'secure' => false
97 }
98 OptionParser.new do |opts|
temiola0f0a6bc2015-01-07 18:43:40 -080099 opts.banner = 'Usage: [--host <hostname>:<port>] [--secure|-s]'
100 opts.on('--host HOST', '<hostname>:<port>') do |v|
nnoble0c475f02014-12-05 15:37:39 -0800101 options['host'] = v
102 end
103 opts.on('-s', '--secure', 'access using test creds') do |v|
Tim Emiolae2860c52015-01-16 02:58:41 -0800104 options['secure'] = v
nnoble0c475f02014-12-05 15:37:39 -0800105 end
106 end.parse!
107
nnoble097ef9b2014-12-01 17:06:10 -0800108 # 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.
nnoble0c475f02014-12-05 15:37:39 -0800110
111 p options
112 if options['secure']
113 stub_opts = {
114 :creds => test_creds,
Julien Boeuf597a4f22015-02-23 15:57:14 -0800115 GRPC::Core::Channel::SSL_TARGET => 'foo.test.google.fr'
nnoble0c475f02014-12-05 15:37:39 -0800116 }
117 p stub_opts
118 p options['host']
119 stub = Math::Math::Stub.new(options['host'], **stub_opts)
Nick Gauthierf233d962015-05-20 14:02:50 -0400120 GRPC.logger.info("... connecting securely on #{options['host']}")
nnoble0c475f02014-12-05 15:37:39 -0800121 else
122 stub = Math::Math::Stub.new(options['host'])
Nick Gauthierf233d962015-05-20 14:02:50 -0400123 GRPC.logger.info("... connecting insecurely on #{options['host']}")
nnoble0c475f02014-12-05 15:37:39 -0800124 end
125
nnoble097ef9b2014-12-01 17:06:10 -0800126 do_div(stub)
127 do_sum(stub)
128 do_fib(stub)
129 do_div_many(stub)
130end
131
Craig Tiller190d3602015-02-18 09:23:38 -0800132main