Debian Codegraph @

examples/python/route_guide/route_guide_client.py

raw
1 # Copyright 2015 gRPC authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """The Python implementation of the gRPC route guide client."""
15
16 from __future__ import print_function
17
18 import logging
19 import random
20
21 import grpc
22 import route_guide_pb2
23 import route_guide_pb2_grpc
24 import route_guide_resources
25
26
27 def make_route_note(message, latitude, longitude):
28 return route_guide_pb2.RouteNote(
29 message=message,
30 location=route_guide_pb2.Point(latitude=latitude, longitude=longitude))
31
32
33 def guide_get_one_feature(stub, point):
34 feature = stub.GetFeature(point)
35 if not feature.location:
36 print("Server returned incomplete feature")
37 return
38
39 if feature.name:
40 print("Feature called %s at %s" % (feature.name, feature.location))
41 else:
42 print("Found no feature at %s" % feature.location)
43
44
45 def guide_get_feature(stub):
46 guide_get_one_feature(
47 stub, route_guide_pb2.Point(latitude=409146138, longitude=-746188906))
48 guide_get_one_feature(stub, route_guide_pb2.Point(latitude=0, longitude=0))
49
50
51 def guide_list_features(stub):
52 rectangle = route_guide_pb2.Rectangle(
53 lo=route_guide_pb2.Point(latitude=400000000, longitude=-750000000),
54 hi=route_guide_pb2.Point(latitude=420000000, longitude=-730000000))
55 print("Looking for features between 40, -75 and 42, -73")
56
57 features = stub.ListFeatures(rectangle)
58
59 for feature in features:
60 print("Feature called %s at %s" % (feature.name, feature.location))
61
62
63 def generate_route(feature_list):
64 for _ in range(0, 10):
65 random_feature = feature_list[random.randint(0, len(feature_list) - 1)]
66 print("Visiting point %s" % random_feature.location)
67 yield random_feature.location
68
69
70 def guide_record_route(stub):
71 feature_list = route_guide_resources.read_route_guide_database()
72
73 route_iterator = generate_route(feature_list)
74 route_summary = stub.RecordRoute(route_iterator)
75 print("Finished trip with %s points " % route_summary.point_count)
76 print("Passed %s features " % route_summary.feature_count)
77 print("Travelled %s meters " % route_summary.distance)
78 print("It took %s seconds " % route_summary.elapsed_time)
79
80
81 def generate_messages():
82 messages = [
83 make_route_note("First message", 0, 0),
84 make_route_note("Second message", 0, 1),
85 make_route_note("Third message", 1, 0),
86 make_route_note("Fourth message", 0, 0),
87 make_route_note("Fifth message", 1, 0),
88 ]
89 for msg in messages:
90 print("Sending %s at %s" % (msg.message, msg.location))
91 yield msg
92
93
94 def guide_route_chat(stub):
95 responses = stub.RouteChat(generate_messages())
96 for response in responses:
97 print("Received message %s at %s" %
98 (response.message, response.location))
99
100
101 def run():
102 # NOTE(gRPC Python Team): .close() is possible on a channel and should be
103 # used in circumstances in which the with statement does not fit the needs
104 # of the code.
105 with grpc.insecure_channel('localhost:50051') as channel:
106 stub = route_guide_pb2_grpc.RouteGuideStub(channel)
107 print("-------------- GetFeature --------------")
108 guide_get_feature(stub)
109 print("-------------- ListFeatures --------------")
110 guide_list_features(stub)
111 print("-------------- RecordRoute --------------")
112 guide_record_route(stub)
113 print("-------------- RouteChat --------------")
114 guide_route_chat(stub)
115
116
117 if __name__ == '__main__':
118 logging.basicConfig()
119 run()