route_guide_client.py 4.0 KB

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