route_guide_server.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 server."""
  15. from concurrent import futures
  16. import logging
  17. import math
  18. import time
  19. import grpc
  20. import route_guide_pb2
  21. import route_guide_pb2_grpc
  22. import route_guide_resources
  23. def get_feature(feature_db, point):
  24. """Returns Feature at given location or None."""
  25. for feature in feature_db:
  26. if feature.location == point:
  27. return feature
  28. return None
  29. def get_distance(start, end):
  30. """Distance between two points."""
  31. coord_factor = 10000000.0
  32. lat_1 = start.latitude / coord_factor
  33. lat_2 = end.latitude / coord_factor
  34. lon_1 = start.longitude / coord_factor
  35. lon_2 = end.longitude / coord_factor
  36. lat_rad_1 = math.radians(lat_1)
  37. lat_rad_2 = math.radians(lat_2)
  38. delta_lat_rad = math.radians(lat_2 - lat_1)
  39. delta_lon_rad = math.radians(lon_2 - lon_1)
  40. # Formula is based on http://mathforum.org/library/drmath/view/51879.html
  41. a = (pow(math.sin(delta_lat_rad / 2), 2) +
  42. (math.cos(lat_rad_1) * math.cos(lat_rad_2) *
  43. pow(math.sin(delta_lon_rad / 2), 2)))
  44. c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
  45. R = 6371000
  46. # metres
  47. return R * c
  48. class RouteGuideServicer(route_guide_pb2_grpc.RouteGuideServicer):
  49. """Provides methods that implement functionality of route guide server."""
  50. def __init__(self):
  51. self.db = route_guide_resources.read_route_guide_database()
  52. def GetFeature(self, request, context):
  53. feature = get_feature(self.db, request)
  54. if feature is None:
  55. return route_guide_pb2.Feature(name="", location=request)
  56. else:
  57. return feature
  58. def ListFeatures(self, request, context):
  59. left = min(request.lo.longitude, request.hi.longitude)
  60. right = max(request.lo.longitude, request.hi.longitude)
  61. top = max(request.lo.latitude, request.hi.latitude)
  62. bottom = min(request.lo.latitude, request.hi.latitude)
  63. for feature in self.db:
  64. if (feature.location.longitude >= left and
  65. feature.location.longitude <= right and
  66. feature.location.latitude >= bottom and
  67. feature.location.latitude <= top):
  68. yield feature
  69. def RecordRoute(self, request_iterator, context):
  70. point_count = 0
  71. feature_count = 0
  72. distance = 0.0
  73. prev_point = None
  74. start_time = time.time()
  75. for point in request_iterator:
  76. point_count += 1
  77. if get_feature(self.db, point):
  78. feature_count += 1
  79. if prev_point:
  80. distance += get_distance(prev_point, point)
  81. prev_point = point
  82. elapsed_time = time.time() - start_time
  83. return route_guide_pb2.RouteSummary(point_count=point_count,
  84. feature_count=feature_count,
  85. distance=int(distance),
  86. elapsed_time=int(elapsed_time))
  87. def RouteChat(self, request_iterator, context):
  88. prev_notes = []
  89. for new_note in request_iterator:
  90. for prev_note in prev_notes:
  91. if prev_note.location == new_note.location:
  92. yield prev_note
  93. prev_notes.append(new_note)
  94. def serve():
  95. server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
  96. route_guide_pb2_grpc.add_RouteGuideServicer_to_server(
  97. RouteGuideServicer(), server)
  98. server.add_insecure_port('[::]:50051')
  99. server.start()
  100. server.wait_for_termination()
  101. if __name__ == '__main__':
  102. logging.basicConfig()
  103. serve()