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 gRPC Ruby server that implements the Math::Calc service and helps |
| 18 | # validate GRPC::RpcServer as GRPC implementation using proto2 serialization. |
| 19 | # |
| 20 | # Usage: $ path/to/math_server.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 'forwardable' |
| 28 | require 'grpc' |
Tim Emiola | 81d950a | 2015-08-28 16:57:28 -0700 | [diff] [blame] | 29 | require 'logger' |
Ken Payson | 5a2c918 | 2016-07-26 17:15:08 -0700 | [diff] [blame] | 30 | require 'math_services_pb' |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 31 | require 'optparse' |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 32 | |
Tim Emiola | 81d950a | 2015-08-28 16:57:28 -0700 | [diff] [blame] | 33 | # RubyLogger defines a logger for gRPC based on the standard ruby logger. |
| 34 | module RubyLogger |
| 35 | def logger |
| 36 | LOGGER |
| 37 | end |
| 38 | |
| 39 | LOGGER = Logger.new(STDOUT) |
| 40 | end |
| 41 | |
| 42 | # GRPC is the general RPC module |
| 43 | module GRPC |
| 44 | # Inject the noop #logger if no module-level logger method has been injected. |
| 45 | extend RubyLogger |
| 46 | end |
| 47 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 48 | # Holds state for a fibonacci series |
| 49 | class Fibber |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 50 | def initialize(limit) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 51 | fail "bad limit: got #{limit}, want limit > 0" if limit < 1 |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 52 | @limit = limit |
| 53 | end |
| 54 | |
| 55 | def generator |
| 56 | return enum_for(:generator) unless block_given? |
| 57 | idx, current, previous = 0, 1, 1 |
| 58 | until idx == @limit |
Aggelos Avgerinos | 57e7dc8 | 2015-05-09 13:08:03 +0300 | [diff] [blame] | 59 | if idx.zero? || idx == 1 |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 60 | yield Math::Num.new(num: 1) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 61 | idx += 1 |
| 62 | next |
| 63 | end |
| 64 | tmp = current |
| 65 | current = previous + current |
| 66 | previous = tmp |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 67 | yield Math::Num.new(num: current) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 68 | idx += 1 |
| 69 | end |
| 70 | end |
| 71 | end |
| 72 | |
| 73 | # A EnumeratorQueue wraps a Queue to yield the items added to it. |
| 74 | class EnumeratorQueue |
| 75 | extend Forwardable |
| 76 | def_delegators :@q, :push |
| 77 | |
| 78 | def initialize(sentinel) |
| 79 | @q = Queue.new |
| 80 | @sentinel = sentinel |
| 81 | end |
| 82 | |
| 83 | def each_item |
| 84 | return enum_for(:each_item) unless block_given? |
| 85 | loop do |
| 86 | r = @q.pop |
| 87 | break if r.equal?(@sentinel) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 88 | fail r if r.is_a? Exception |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 89 | yield r |
| 90 | end |
| 91 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 92 | end |
| 93 | |
| 94 | # The Math::Math:: module occurs because the service has the same name as its |
| 95 | # package. That practice should be avoided by defining real services. |
| 96 | class Calculator < Math::Math::Service |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 97 | def div(div_args, _call) |
Aggelos Avgerinos | 57e7dc8 | 2015-05-09 13:08:03 +0300 | [diff] [blame] | 98 | if div_args.divisor.zero? |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 99 | # To send non-OK status handlers raise a StatusError with the code and |
| 100 | # and detail they want sent as a Status. |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 101 | fail GRPC::StatusError.new(GRPC::Status::INVALID_ARGUMENT, |
| 102 | 'divisor cannot be 0') |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 103 | end |
| 104 | |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 105 | Math::DivReply.new(quotient: div_args.dividend / div_args.divisor, |
| 106 | remainder: div_args.dividend % div_args.divisor) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 107 | end |
| 108 | |
| 109 | def sum(call) |
| 110 | # the requests are accesible as the Enumerator call#each_request |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 111 | nums = call.each_remote_read.collect(&:num) |
| 112 | sum = nums.inject { |s, x| s + x } |
| 113 | Math::Num.new(num: sum) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 114 | end |
| 115 | |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 116 | def fib(fib_args, _call) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 117 | if fib_args.limit < 1 |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 118 | fail StatusError.new(Status::INVALID_ARGUMENT, 'limit must be >= 0') |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 119 | end |
| 120 | |
| 121 | # return an Enumerator of Nums |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 122 | Fibber.new(fib_args.limit).generator |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 123 | # just return the generator, GRPC::GenericServer sends each actual response |
| 124 | end |
| 125 | |
| 126 | def div_many(requests) |
| 127 | # requests is an lazy Enumerator of the requests sent by the client. |
| 128 | q = EnumeratorQueue.new(self) |
| 129 | t = Thread.new do |
| 130 | begin |
| 131 | requests.each do |req| |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 132 | GRPC.logger.info("read #{req.inspect}") |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 133 | resp = Math::DivReply.new(quotient: req.dividend / req.divisor, |
| 134 | remainder: req.dividend % req.divisor) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 135 | q.push(resp) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 136 | Thread.pass # let the internal Bidi threads run |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 137 | end |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 138 | GRPC.logger.info('finished reads') |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 139 | q.push(self) |
| 140 | rescue StandardError => e |
| 141 | q.push(e) # share the exception with the enumerator |
| 142 | raise e |
| 143 | end |
| 144 | end |
| 145 | t.priority = -2 # hint that the div_many thread should not be favoured |
| 146 | q.each_item |
| 147 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 148 | end |
| 149 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 150 | def load_test_certs |
| 151 | this_dir = File.expand_path(File.dirname(__FILE__)) |
| 152 | data_dir = File.join(File.dirname(this_dir), 'spec/testdata') |
| 153 | files = ['ca.pem', 'server1.key', 'server1.pem'] |
| 154 | files.map { |f| File.open(File.join(data_dir, f)).read } |
| 155 | end |
| 156 | |
| 157 | def test_server_creds |
| 158 | certs = load_test_certs |
Tim Emiola | 73a540a | 2015-08-28 18:56:17 -0700 | [diff] [blame] | 159 | GRPC::Core::ServerCredentials.new( |
| 160 | nil, [{ private_key: certs[1], cert_chain: certs[2] }], false) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 161 | end |
| 162 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 163 | def main |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 164 | options = { |
| 165 | 'host' => 'localhost:7071', |
| 166 | 'secure' => false |
| 167 | } |
| 168 | OptionParser.new do |opts| |
temiola | 0f0a6bc | 2015-01-07 18:43:40 -0800 | [diff] [blame] | 169 | opts.banner = 'Usage: [--host <hostname>:<port>] [--secure|-s]' |
| 170 | opts.on('--host HOST', '<hostname>:<port>') do |v| |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 171 | options['host'] = v |
| 172 | end |
| 173 | opts.on('-s', '--secure', 'access using test creds') do |v| |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 174 | options['secure'] = v |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 175 | end |
| 176 | end.parse! |
| 177 | |
Tim Emiola | 0ce8edc | 2015-03-05 15:17:30 -0800 | [diff] [blame] | 178 | s = GRPC::RpcServer.new |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 179 | if options['secure'] |
Tim Emiola | 0ce8edc | 2015-03-05 15:17:30 -0800 | [diff] [blame] | 180 | s.add_http2_port(options['host'], test_server_creds) |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 181 | GRPC.logger.info("... running securely on #{options['host']}") |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 182 | else |
Tim Emiola | c03138a | 2015-09-24 13:11:03 -0700 | [diff] [blame] | 183 | s.add_http2_port(options['host'], :this_port_is_insecure) |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 184 | GRPC.logger.info("... running insecurely on #{options['host']}") |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 185 | end |
| 186 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 187 | s.handle(Calculator) |
Tim Emiola | 321871e | 2015-04-16 12:56:11 -0700 | [diff] [blame] | 188 | s.run_till_terminated |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 189 | end |
| 190 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 191 | main |