14 """A gRPC server servicing both Greeter and RouteGuide RPCs."""
16 from concurrent
import futures
23 import helloworld_pb2_grpc
24 import route_guide_pb2
25 import route_guide_pb2_grpc
26 import route_guide_resources
30 """Returns Feature at given location or None."""
31 for feature
in feature_db:
32 if feature.location == point:
38 """Distance between two points."""
39 coord_factor = 10000000.0
40 lat_1 = start.latitude / coord_factor
41 lat_2 = end.latitude / coord_factor
42 lon_1 = start.longitude / coord_factor
43 lon_2 = end.longitude / coord_factor
44 lat_rad_1 = math.radians(lat_1)
45 lat_rad_2 = math.radians(lat_2)
46 delta_lat_rad = math.radians(lat_2 - lat_1)
47 delta_lon_rad = math.radians(lon_2 - lon_1)
49 a = (pow(math.sin(delta_lat_rad / 2), 2) +
50 (math.cos(lat_rad_1) * math.cos(lat_rad_2) *
51 pow(math.sin(delta_lon_rad / 2), 2)))
52 c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
62 message=
'Hello, {}!'.
format(request.name))
66 """Provides methods that implement functionality of route guide server."""
79 left =
min(request.lo.longitude, request.hi.longitude)
80 right =
max(request.lo.longitude, request.hi.longitude)
81 top =
max(request.lo.latitude, request.hi.latitude)
82 bottom =
min(request.lo.latitude, request.hi.latitude)
83 for feature
in self.
db:
84 if (feature.location.longitude >= left
and
85 feature.location.longitude <= right
and
86 feature.location.latitude >= bottom
and
87 feature.location.latitude <= top):
96 start_time = time.time()
97 for point
in request_iterator:
105 elapsed_time = time.time() - start_time
107 feature_count=feature_count,
108 distance=
int(distance),
109 elapsed_time=
int(elapsed_time))
113 for new_note
in request_iterator:
114 for prev_note
in prev_notes:
115 if prev_note.location == new_note.location:
117 prev_notes.append(new_note)
121 server =
grpc.server(futures.ThreadPoolExecutor(max_workers=10))
126 server.add_insecure_port(
'[::]:50051')
128 server.wait_for_termination()
131 if __name__ ==
'__main__':
132 logging.basicConfig()