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