| 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 helps validate RpcServer without protobuf serialization. |
| 18 |
# |
| 19 |
# Usage: $ ruby -S path/to/noproto_client.rb |
| 20 |
|
| 21 |
this_dir = File.expand_path(File.dirname(__FILE__)) |
| 22 |
lib_dir = File.join(File.dirname(this_dir), 'lib') |
| 23 |
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir) |
| 24 |
|
| 25 |
require 'grpc' |
| 26 |
require 'optparse' |
| 27 |
|
| 28 |
# a simple non-protobuf message class. |
| 29 |
class NoProtoMsg |
| 30 |
def self.marshal(_o) |
| 31 |
'' |
| 32 |
end |
| 33 |
|
| 34 |
def self.unmarshal(_o) |
| 35 |
NoProtoMsg.new |
| 36 |
end |
| 37 |
end |
| 38 |
|
| 39 |
# service the uses the non-protobuf message class. |
| 40 |
class NoProtoService |
| 41 |
include GRPC::GenericService |
| 42 |
rpc :AnRPC, NoProtoMsg, NoProtoMsg |
| 43 |
end |
| 44 |
|
| 45 |
NoProtoStub = NoProtoService.rpc_stub_class |
| 46 |
|
| 47 |
def load_test_certs |
| 48 |
this_dir = File.expand_path(File.dirname(__FILE__)) |
| 49 |
data_dir = File.join(File.dirname(this_dir), 'spec/testdata') |
| 50 |
files = ['ca.pem', 'server1.key', 'server1.pem'] |
| 51 |
files.map { |f| File.open(File.join(data_dir, f)).read } |
| 52 |
end |
| 53 |
|
| 54 |
def test_creds |
| 55 |
certs = load_test_certs |
| 56 |
GRPC::Core::ChannelCredentials.new(certs[0]) |
| 57 |
end |
| 58 |
|
| 59 |
def main |
| 60 |
options = { |
| 61 |
'host' => 'localhost:7071', |
| 62 |
'secure' => false |
| 63 |
} |
| 64 |
OptionParser.new do |opts| |
| 65 |
opts.banner = 'Usage: [--host <hostname>:<port>] [--secure|-s]' |
| 66 |
opts.on('--host HOST', '<hostname>:<port>') do |v| |
| 67 |
options['host'] = v |
| 68 |
end |
| 69 |
opts.on('-s', '--secure', 'access using test creds') do |v| |
| 70 |
options['secure'] = v |
| 71 |
end |
| 72 |
end.parse! |
| 73 |
|
| 74 |
if options['secure'] |
| 75 |
stub_opts = { |
| 76 |
:creds => test_creds, |
| 77 |
GRPC::Core::Channel::SSL_TARGET => 'foo.test.google.fr' |
| 78 |
} |
| 79 |
p stub_opts |
| 80 |
p options['host'] |
| 81 |
stub = NoProtoStub.new(options['host'], **stub_opts) |
| 82 |
GRPC.logger.info("... connecting securely on #{options['host']}") |
| 83 |
else |
| 84 |
stub = NoProtoStub.new(options['host']) |
| 85 |
GRPC.logger.info("... connecting insecurely on #{options['host']}") |
| 86 |
end |
| 87 |
|
| 88 |
GRPC.logger.info('sending a NoProto rpc') |
| 89 |
resp = stub.an_rpc(NoProtoMsg.new) |
| 90 |
GRPC.logger.info("got a response: #{resp}") |
| 91 |
end |
| 92 |
|
| 93 |
main |