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


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:41