• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16from __future__ import print_function
17
18import random
19import logging
20
21import grpc
22
23import route_guide_pb2
24import route_guide_pb2_grpc
25import route_guide_resources
26
27
28def make_route_note(message, latitude, longitude):
29    return route_guide_pb2.RouteNote(
30        message=message,
31        location=route_guide_pb2.Point(latitude=latitude, longitude=longitude))
32
33
34def guide_get_one_feature(stub, point):
35    feature = stub.GetFeature(point)
36    if not feature.location:
37        print("Server returned incomplete feature")
38        return
39
40    if feature.name:
41        print("Feature called %s at %s" % (feature.name, feature.location))
42    else:
43        print("Found no feature at %s" % feature.location)
44
45
46def guide_get_feature(stub):
47    guide_get_one_feature(
48        stub, route_guide_pb2.Point(latitude=409146138, longitude=-746188906))
49    guide_get_one_feature(stub, route_guide_pb2.Point(latitude=0, longitude=0))
50
51
52def guide_list_features(stub):
53    rectangle = route_guide_pb2.Rectangle(
54        lo=route_guide_pb2.Point(latitude=400000000, longitude=-750000000),
55        hi=route_guide_pb2.Point(latitude=420000000, longitude=-730000000))
56    print("Looking for features between 40, -75 and 42, -73")
57
58    features = stub.ListFeatures(rectangle)
59
60    for feature in features:
61        print("Feature called %s at %s" % (feature.name, feature.location))
62
63
64def generate_route(feature_list):
65    for _ in range(0, 10):
66        random_feature = feature_list[random.randint(0, len(feature_list) - 1)]
67        print("Visiting point %s" % random_feature.location)
68        yield random_feature.location
69
70
71def guide_record_route(stub):
72    feature_list = route_guide_resources.read_route_guide_database()
73
74    route_iterator = generate_route(feature_list)
75    route_summary = stub.RecordRoute(route_iterator)
76    print("Finished trip with %s points " % route_summary.point_count)
77    print("Passed %s features " % route_summary.feature_count)
78    print("Travelled %s meters " % route_summary.distance)
79    print("It took %s seconds " % route_summary.elapsed_time)
80
81
82def generate_messages():
83    messages = [
84        make_route_note("First message", 0, 0),
85        make_route_note("Second message", 0, 1),
86        make_route_note("Third message", 1, 0),
87        make_route_note("Fourth message", 0, 0),
88        make_route_note("Fifth message", 1, 0),
89    ]
90    for msg in messages:
91        print("Sending %s at %s" % (msg.message, msg.location))
92        yield msg
93
94
95def guide_route_chat(stub):
96    responses = stub.RouteChat(generate_messages())
97    for response in responses:
98        print("Received message %s at %s" %
99              (response.message, response.location))
100
101
102def run():
103    # NOTE(gRPC Python Team): .close() is possible on a channel and should be
104    # used in circumstances in which the with statement does not fit the needs
105    # of the code.
106    with grpc.insecure_channel('localhost:50051') as channel:
107        stub = route_guide_pb2_grpc.RouteGuideStub(channel)
108        print("-------------- GetFeature --------------")
109        guide_get_feature(stub)
110        print("-------------- ListFeatures --------------")
111        guide_list_features(stub)
112        print("-------------- RecordRoute --------------")
113        guide_record_route(stub)
114        print("-------------- RouteChat --------------")
115        guide_route_chat(stub)
116
117
118if __name__ == '__main__':
119    logging.basicConfig()
120    run()
121