Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: third_party/grpc/examples/ruby/route_guide/route_guide_client.rb

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env ruby
2
3 # Copyright 2015-2016, 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 connects to a Route Guide service.
33 #
34 # Usage: $ path/to/route_guide_client.rb path/to/route_guide_db.json &
35
36 this_dir = File.expand_path(File.dirname(__FILE__))
37 lib_dir = File.join(File.dirname(this_dir), 'lib')
38 $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
39
40 require 'grpc'
41 require 'route_guide_services'
42
43 include Routeguide
44
45 GET_FEATURE_POINTS = [
46 Point.new(latitude: 409_146_138, longitude: -746_188_906),
47 Point.new(latitude: 0, longitude: 0)
48 ]
49
50 # runs a GetFeature rpc.
51 #
52 # - once with a point known to be present in the sample route database
53 # - once with a point that is not in the sample database
54 def run_get_feature(stub)
55 p 'GetFeature'
56 p '----------'
57 GET_FEATURE_POINTS.each do |pt|
58 resp = stub.get_feature(pt)
59 if resp.name != ''
60 p "- found '#{resp.name}' at #{pt.inspect}"
61 else
62 p "- found nothing at #{pt.inspect}"
63 end
64 end
65 end
66
67 LIST_FEATURES_RECT = Rectangle.new(
68 lo: Point.new(latitude: 400_000_000, longitude: -750_000_000),
69 hi: Point.new(latitude: 420_000_000, longitude: -730_000_000))
70
71 # runs a ListFeatures rpc.
72 #
73 # - the rectangle to chosen to include most of the known features
74 # in the sample db.
75 def run_list_features(stub)
76 p 'ListFeatures'
77 p '------------'
78 resps = stub.list_features(LIST_FEATURES_RECT)
79 resps.each do |r|
80 p "- found '#{r.name}' at #{r.location.inspect}"
81 end
82 end
83
84 # RandomRoute provides an Enumerable that yields a random 'route' of points
85 # from a list of Features.
86 class RandomRoute
87 def initialize(features, size)
88 @features = features
89 @size = size
90 end
91
92 # yields a point, waiting between 0 and 1 seconds between each yield
93 #
94 # @return an Enumerable that yields a random point
95 def each
96 return enum_for(:each) unless block_given?
97 @size.times do
98 json_feature = @features[rand(0..@features.length)]
99 next if json_feature.nil?
100 location = json_feature['location']
101 pt = Point.new(
102 Hash[location.each_pair.map { |k, v| [k.to_sym, v] }])
103 p "- next point is #{pt.inspect}"
104 yield pt
105 sleep(rand(0..1))
106 end
107 end
108 end
109
110 # runs a RecordRoute rpc.
111 #
112 # - the rectangle to chosen to include most of the known features
113 # in the sample db.
114 def run_record_route(stub, features)
115 p 'RecordRoute'
116 p '-----------'
117 points_on_route = 10 # arbitrary
118 deadline = points_on_route # as delay b/w each is max 1 second
119 reqs = RandomRoute.new(features, points_on_route)
120 resp = stub.record_route(reqs.each, deadline)
121 p "summary: #{resp.inspect}"
122 end
123
124 ROUTE_CHAT_NOTES = [
125 RouteNote.new(message: 'doh - a deer',
126 location: Point.new(latitude: 0, longitude: 0)),
127 RouteNote.new(message: 'ray - a drop of golden sun',
128 location: Point.new(latitude: 0, longitude: 1)),
129 RouteNote.new(message: 'me - the name I call myself',
130 location: Point.new(latitude: 1, longitude: 0)),
131 RouteNote.new(message: 'fa - a longer way to run',
132 location: Point.new(latitude: 1, longitude: 1)),
133 RouteNote.new(message: 'soh - with needle and a thread',
134 location: Point.new(latitude: 0, longitude: 1))
135 ]
136
137 # runs a RouteChat rpc.
138 #
139 # sends a canned set of route notes and prints out the responses.
140 def run_route_chat(stub)
141 p 'Route Chat'
142 p '----------'
143 # TODO: decouple sending and receiving, i.e have the response enumerator run
144 # on its own thread.
145 resps = stub.route_chat(ROUTE_CHAT_NOTES)
146 resps.each { |r| p "received #{r.inspect}" }
147 end
148
149 def main
150 stub = RouteGuide::Stub.new('localhost:50051', :this_channel_is_insecure)
151 run_get_feature(stub)
152 run_list_features(stub)
153 run_route_chat(stub)
154 if ARGV.length == 0
155 p 'no feature database; skipping record_route'
156 exit
157 end
158 raw_data = []
159 File.open(ARGV[0]) do |f|
160 raw_data = MultiJson.load(f.read)
161 end
162 run_record_route(stub, raw_data)
163 end
164
165 main
OLDNEW
« no previous file with comments | « third_party/grpc/examples/ruby/route_guide/README.md ('k') | third_party/grpc/examples/ruby/route_guide/route_guide_server.rb » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698