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

Side by Side Diff: third_party/grpc/examples/python/route_guide/route_guide_client.py

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 # Copyright 2015, Google Inc.
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 """The Python implementation of the gRPC route guide client."""
31
32 import random
33 import time
34
35 from grpc.beta import implementations
36
37 import route_guide_pb2
38 import route_guide_resources
39
40 _TIMEOUT_SECONDS = 30
41
42
43 def make_route_note(message, latitude, longitude):
44 return route_guide_pb2.RouteNote(
45 message=message,
46 location=route_guide_pb2.Point(latitude=latitude, longitude=longitude))
47
48
49 def guide_get_one_feature(stub, point):
50 feature = stub.GetFeature(point, _TIMEOUT_SECONDS)
51 if not feature.location:
52 print "Server returned incomplete feature"
53 return
54
55 if feature.name:
56 print "Feature called %s at %s" % (feature.name, feature.location)
57 else:
58 print "Found no feature at %s" % feature.location
59
60
61 def guide_get_feature(stub):
62 guide_get_one_feature(stub, route_guide_pb2.Point(latitude=409146138, longitud e=-746188906))
63 guide_get_one_feature(stub, route_guide_pb2.Point(latitude=0, longitude=0))
64
65
66 def guide_list_features(stub):
67 rect = route_guide_pb2.Rectangle(
68 lo=route_guide_pb2.Point(
69 latitude=400000000, longitude = -750000000),
70 hi=route_guide_pb2.Point(
71 latitude = 420000000, longitude = -730000000))
72 print "Looking for features between 40, -75 and 42, -73"
73
74 features = stub.ListFeatures(rect, _TIMEOUT_SECONDS)
75
76 for feature in features:
77 print "Feature called %s at %s" % (feature.name, feature.location)
78
79
80 def generate_route(feature_list):
81 for _ in range(0, 10):
82 random_feature = feature_list[random.randint(0, len(feature_list) - 1)]
83 print "Visiting point %s" % random_feature.location
84 yield random_feature.location
85 time.sleep(random.uniform(0.5, 1.5))
86
87
88 def guide_record_route(stub):
89 feature_list = route_guide_resources.read_route_guide_database()
90
91 route_iter = generate_route(feature_list)
92 route_summary = stub.RecordRoute(route_iter, _TIMEOUT_SECONDS)
93 print "Finished trip with %s points " % route_summary.point_count
94 print "Passed %s features " % route_summary.feature_count
95 print "Travelled %s meters " % route_summary.distance
96 print "It took %s seconds " % route_summary.elapsed_time
97
98
99 def generate_messages():
100 messages = [
101 make_route_note("First message", 0, 0),
102 make_route_note("Second message", 0, 1),
103 make_route_note("Third message", 1, 0),
104 make_route_note("Fourth message", 0, 0),
105 make_route_note("Fifth message", 1, 0),
106 ]
107 for msg in messages:
108 print "Sending %s at %s" % (msg.message, msg.location)
109 yield msg
110 time.sleep(random.uniform(0.5, 1.0))
111
112
113 def guide_route_chat(stub):
114 responses = stub.RouteChat(generate_messages(), _TIMEOUT_SECONDS)
115 for response in responses:
116 print "Received message %s at %s" % (response.message, response.location)
117
118
119 def run():
120 channel = implementations.insecure_channel('localhost', 50051)
121 stub = route_guide_pb2.beta_create_RouteGuide_stub(channel)
122 print "-------------- GetFeature --------------"
123 guide_get_feature(stub)
124 print "-------------- ListFeatures --------------"
125 guide_list_features(stub)
126 print "-------------- RecordRoute --------------"
127 guide_record_route(stub)
128 print "-------------- RouteChat --------------"
129 guide_route_chat(stub)
130
131
132 if __name__ == '__main__':
133 run()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698