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 19 20import grpc 21 22import route_guide_pb2 23import route_guide_pb2_grpc 24import route_guide_resources 25 26 27def 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 33def 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 45def guide_get_feature(stub): 46 guide_get_one_feature(stub, 47 route_guide_pb2.Point( 48 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" % (response.message, 99 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 run() 120