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 logging 19import random 20 21import grpc 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 33 34def format_point(point): 35 # not delegating in point.__str__ because it is an empty string when its 36 # values are zero. In addition, it puts a newline between the fields. 37 return "latitude: %d, longitude: %d" % (point.latitude, point.longitude) 38 39 40def guide_get_one_feature(stub, point): 41 feature = stub.GetFeature(point) 42 if not feature.location: 43 print("Server returned incomplete feature") 44 return 45 46 if feature.name: 47 print( 48 "Feature called %r at %s" 49 % (feature.name, format_point(feature.location)) 50 ) 51 else: 52 print("Found no feature at %s" % format_point(feature.location)) 53 54 55def guide_get_feature(stub): 56 guide_get_one_feature( 57 stub, route_guide_pb2.Point(latitude=409146138, longitude=-746188906) 58 ) 59 guide_get_one_feature(stub, route_guide_pb2.Point(latitude=0, longitude=0)) 60 61 62def guide_list_features(stub): 63 rectangle = route_guide_pb2.Rectangle( 64 lo=route_guide_pb2.Point(latitude=400000000, longitude=-750000000), 65 hi=route_guide_pb2.Point(latitude=420000000, longitude=-730000000), 66 ) 67 print("Looking for features between 40, -75 and 42, -73") 68 69 features = stub.ListFeatures(rectangle) 70 71 for feature in features: 72 print( 73 "Feature called %r at %s" 74 % (feature.name, format_point(feature.location)) 75 ) 76 77 78def generate_route(feature_list): 79 for _ in range(0, 10): 80 random_feature = random.choice(feature_list) 81 print("Visiting point %s" % format_point(random_feature.location)) 82 yield random_feature.location 83 84 85def guide_record_route(stub): 86 feature_list = route_guide_resources.read_route_guide_database() 87 88 route_iterator = generate_route(feature_list) 89 route_summary = stub.RecordRoute(route_iterator) 90 print("Finished trip with %s points " % route_summary.point_count) 91 print("Passed %s features " % route_summary.feature_count) 92 print("Travelled %s meters " % route_summary.distance) 93 print("It took %s seconds " % route_summary.elapsed_time) 94 95 96def generate_messages(): 97 messages = [ 98 make_route_note("First message", 0, 0), 99 make_route_note("Second message", 0, 1), 100 make_route_note("Third message", 1, 0), 101 make_route_note("Fourth message", 0, 0), 102 make_route_note("Fifth message", 1, 0), 103 ] 104 for msg in messages: 105 print("Sending %s at %s" % (msg.message, format_point(msg.location))) 106 yield msg 107 108 109def guide_route_chat(stub): 110 responses = stub.RouteChat(generate_messages()) 111 for response in responses: 112 print( 113 "Received message %s at %s" 114 % (response.message, format_point(response.location)) 115 ) 116 117 118def run(): 119 # NOTE(gRPC Python Team): .close() is possible on a channel and should be 120 # used in circumstances in which the with statement does not fit the needs 121 # of the code. 122 with grpc.insecure_channel("localhost:50051") as channel: 123 stub = route_guide_pb2_grpc.RouteGuideStub(channel) 124 print("-------------- GetFeature --------------") 125 guide_get_feature(stub) 126 print("-------------- ListFeatures --------------") 127 guide_list_features(stub) 128 print("-------------- RecordRoute --------------") 129 guide_record_route(stub) 130 print("-------------- RouteChat --------------") 131 guide_route_chat(stub) 132 133 134if __name__ == "__main__": 135 logging.basicConfig() 136 run() 137