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