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