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 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 ) 50 guide_get_one_feature(stub, route_guide_pb2.Point(latitude=0, longitude=0)) 51 52 53def guide_list_features(stub): 54 rectangle = route_guide_pb2.Rectangle( 55 lo=route_guide_pb2.Point(latitude=400000000, longitude=-750000000), 56 hi=route_guide_pb2.Point(latitude=420000000, longitude=-730000000), 57 ) 58 print("Looking for features between 40, -75 and 42, -73") 59 60 features = stub.ListFeatures(rectangle) 61 62 for feature in features: 63 print("Feature called %s at %s" % (feature.name, feature.location)) 64 65 66def generate_route(feature_list): 67 for _ in range(0, 10): 68 random_feature = feature_list[random.randint(0, len(feature_list) - 1)] 69 print("Visiting point %s" % random_feature.location) 70 yield random_feature.location 71 72 73def guide_record_route(stub): 74 feature_list = route_guide_resources.read_route_guide_database() 75 76 route_iterator = generate_route(feature_list) 77 route_summary = stub.RecordRoute(route_iterator) 78 print("Finished trip with %s points " % route_summary.point_count) 79 print("Passed %s features " % route_summary.feature_count) 80 print("Travelled %s meters " % route_summary.distance) 81 print("It took %s seconds " % route_summary.elapsed_time) 82 83 84def generate_messages(): 85 messages = [ 86 make_route_note("First message", 0, 0), 87 make_route_note("Second message", 0, 1), 88 make_route_note("Third message", 1, 0), 89 make_route_note("Fourth message", 0, 0), 90 make_route_note("Fifth message", 1, 0), 91 ] 92 for msg in messages: 93 print("Sending %s at %s" % (msg.message, msg.location)) 94 yield msg 95 96 97def guide_route_chat(stub): 98 responses = stub.RouteChat(generate_messages()) 99 for response in responses: 100 print( 101 "Received message %s at %s" % (response.message, response.location) 102 ) 103 104 105def run(): 106 # NOTE(gRPC Python Team): .close() is possible on a channel and should be 107 # used in circumstances in which the with statement does not fit the needs 108 # of the code. 109 with grpc.insecure_channel("localhost:50051") as channel: 110 stub = route_guide_pb2_grpc.RouteGuideStub(channel) 111 print("-------------- GetFeature --------------") 112 guide_get_feature(stub) 113 print("-------------- ListFeatures --------------") 114 guide_list_features(stub) 115 print("-------------- RecordRoute --------------") 116 guide_record_route(stub) 117 print("-------------- RouteChat --------------") 118 guide_route_chat(stub) 119 120 121if __name__ == "__main__": 122 logging.basicConfig() 123 run() 124