multiplex_client.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. from __future__ import print_function
  16. import logging
  17. import random
  18. import time
  19. import grpc
  20. import helloworld_pb2
  21. import helloworld_pb2_grpc
  22. import route_guide_pb2
  23. import route_guide_pb2_grpc
  24. import route_guide_resources
  25. def make_route_note(message, latitude, longitude):
  26. return route_guide_pb2.RouteNote(
  27. message=message,
  28. location=route_guide_pb2.Point(latitude=latitude, longitude=longitude))
  29. def guide_get_one_feature(route_guide_stub, point):
  30. feature = route_guide_stub.GetFeature(point)
  31. if not feature.location:
  32. print("Server returned incomplete feature")
  33. return
  34. if feature.name:
  35. print("Feature called %s at %s" % (feature.name, feature.location))
  36. else:
  37. print("Found no feature at %s" % feature.location)
  38. def guide_get_feature(route_guide_stub):
  39. guide_get_one_feature(
  40. route_guide_stub,
  41. route_guide_pb2.Point(latitude=409146138, longitude=-746188906))
  42. guide_get_one_feature(route_guide_stub,
  43. route_guide_pb2.Point(latitude=0, longitude=0))
  44. def guide_list_features(route_guide_stub):
  45. rectangle = route_guide_pb2.Rectangle(
  46. lo=route_guide_pb2.Point(latitude=400000000, longitude=-750000000),
  47. hi=route_guide_pb2.Point(latitude=420000000, longitude=-730000000))
  48. print("Looking for features between 40, -75 and 42, -73")
  49. features = route_guide_stub.ListFeatures(rectangle)
  50. for feature in features:
  51. print("Feature called %s at %s" % (feature.name, feature.location))
  52. def generate_route(feature_list):
  53. for _ in range(0, 10):
  54. random_feature = feature_list[random.randint(0, len(feature_list) - 1)]
  55. print("Visiting point %s" % random_feature.location)
  56. yield random_feature.location
  57. time.sleep(random.uniform(0.5, 1.5))
  58. def guide_record_route(route_guide_stub):
  59. feature_list = route_guide_resources.read_route_guide_database()
  60. route_iterator = generate_route(feature_list)
  61. route_summary = route_guide_stub.RecordRoute(route_iterator)
  62. print("Finished trip with %s points " % route_summary.point_count)
  63. print("Passed %s features " % route_summary.feature_count)
  64. print("Travelled %s meters " % route_summary.distance)
  65. print("It took %s seconds " % route_summary.elapsed_time)
  66. def generate_messages():
  67. messages = [
  68. make_route_note("First message", 0, 0),
  69. make_route_note("Second message", 0, 1),
  70. make_route_note("Third message", 1, 0),
  71. make_route_note("Fourth message", 0, 0),
  72. make_route_note("Fifth message", 1, 0),
  73. ]
  74. for msg in messages:
  75. print("Sending %s at %s" % (msg.message, msg.location))
  76. yield msg
  77. time.sleep(random.uniform(0.5, 1.0))
  78. def guide_route_chat(route_guide_stub):
  79. responses = route_guide_stub.RouteChat(generate_messages())
  80. for response in responses:
  81. print("Received message %s at %s" %
  82. (response.message, response.location))
  83. def run():
  84. # NOTE(gRPC Python Team): .close() is possible on a channel and should be
  85. # used in circumstances in which the with statement does not fit the needs
  86. # of the code.
  87. with grpc.insecure_channel('localhost:50051') as channel:
  88. greeter_stub = helloworld_pb2_grpc.GreeterStub(channel)
  89. route_guide_stub = route_guide_pb2_grpc.RouteGuideStub(channel)
  90. greeter_response = greeter_stub.SayHello(
  91. helloworld_pb2.HelloRequest(name='you'))
  92. print("Greeter client received: " + greeter_response.message)
  93. print("-------------- GetFeature --------------")
  94. guide_get_feature(route_guide_stub)
  95. print("-------------- ListFeatures --------------")
  96. guide_list_features(route_guide_stub)
  97. print("-------------- RecordRoute --------------")
  98. guide_record_route(route_guide_stub)
  99. print("-------------- RouteChat --------------")
  100. guide_route_chat(route_guide_stub)
  101. if __name__ == '__main__':
  102. logging.basicConfig()
  103. run()