14 """The Python implementation of the gRPC route guide server."""
16 from concurrent
import futures
22 import route_guide_pb2
23 import route_guide_pb2_grpc
24 import route_guide_resources
28 """Returns Feature at given location or None."""
29 for feature
in feature_db:
30 if feature.location == point:
36 """Distance between two points."""
37 coord_factor = 10000000.0
38 lat_1 = start.latitude / coord_factor
39 lat_2 = end.latitude / coord_factor
40 lon_1 = start.longitude / coord_factor
41 lon_2 = end.longitude / coord_factor
42 lat_rad_1 = math.radians(lat_1)
43 lat_rad_2 = math.radians(lat_2)
44 delta_lat_rad = math.radians(lat_2 - lat_1)
45 delta_lon_rad = math.radians(lon_2 - lon_1)
48 a = (pow(math.sin(delta_lat_rad / 2), 2) +
49 (math.cos(lat_rad_1) * math.cos(lat_rad_2) *
50 pow(math.sin(delta_lon_rad / 2), 2)))
51 c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
58 """Provides methods that implement functionality of route guide server."""
71 left =
min(request.lo.longitude, request.hi.longitude)
72 right =
max(request.lo.longitude, request.hi.longitude)
73 top =
max(request.lo.latitude, request.hi.latitude)
74 bottom =
min(request.lo.latitude, request.hi.latitude)
75 for feature
in self.
db:
76 if (feature.location.longitude >= left
and
77 feature.location.longitude <= right
and
78 feature.location.latitude >= bottom
and
79 feature.location.latitude <= top):
88 start_time = time.time()
89 for point
in request_iterator:
97 elapsed_time = time.time() - start_time
99 feature_count=feature_count,
100 distance=
int(distance),
101 elapsed_time=
int(elapsed_time))
105 for new_note
in request_iterator:
106 for prev_note
in prev_notes:
107 if prev_note.location == new_note.location:
109 prev_notes.append(new_note)
113 server =
grpc.server(futures.ThreadPoolExecutor(max_workers=10))
116 server.add_insecure_port(
'[::]:50051')
118 server.wait_for_termination()
121 if __name__ ==
'__main__':
122 logging.basicConfig()