Debian Codegraph @

examples/python/multiplex/multiplex_server.py

raw
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 gRPC server servicing both Greeter and RouteGuide RPCs."""
15
16 from concurrent import futures
17 import logging
18 import math
19 import time
20
21 import grpc
22 import helloworld_pb2
23 import helloworld_pb2_grpc
24 import route_guide_pb2
25 import route_guide_pb2_grpc
26 import route_guide_resources
27
28
29 def _get_feature(feature_db, point):
30 """Returns Feature at given location or None."""
31 for feature in feature_db:
32 if feature.location == point:
33 return feature
34 return None
35
36
37 def _get_distance(start, end):
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)
48
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))
53 R = 6371000
54 # metres
55 return R * c
56
57
58 class _GreeterServicer(helloworld_pb2_grpc.GreeterServicer):
59
60 def SayHello(self, request, context):
61 return helloworld_pb2.HelloReply(
62 message='Hello, {}!'.format(request.name))
63
64
65 class _RouteGuideServicer(route_guide_pb2_grpc.RouteGuideServicer):
66 """Provides methods that implement functionality of route guide server."""
67
68 def __init__(self):
69 self.db = route_guide_resources.read_route_guide_database()
70
71 def GetFeature(self, request, context):
72 feature = _get_feature(self.db, request)
73 if feature is None:
74 return route_guide_pb2.Feature(name="", location=request)
75 else:
76 return feature
77
78 def ListFeatures(self, request, context):
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):
88 yield feature
89
90 def RecordRoute(self, request_iterator, context):
91 point_count = 0
92 feature_count = 0
93 distance = 0.0
94 prev_point = None
95
96 start_time = time.time()
97 for point in request_iterator:
98 point_count += 1
99 if _get_feature(self.db, point):
100 feature_count += 1
101 if prev_point:
102 distance += _get_distance(prev_point, point)
103 prev_point = point
104
105 elapsed_time = time.time() - start_time
106 return route_guide_pb2.RouteSummary(point_count=point_count,
107 feature_count=feature_count,
108 distance=int(distance),
109 elapsed_time=int(elapsed_time))
110
111 def RouteChat(self, request_iterator, context):
112 prev_notes = []
113 for new_note in request_iterator:
114 for prev_note in prev_notes:
115 if prev_note.location == new_note.location:
116 yield prev_note
117 prev_notes.append(new_note)
118
119
120 def serve():
121 server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
122 helloworld_pb2_grpc.add_GreeterServicer_to_server(_GreeterServicer(),
123 server)
124 route_guide_pb2_grpc.add_RouteGuideServicer_to_server(
125 _RouteGuideServicer(), server)
126 server.add_insecure_port('[::]:50051')
127 server.start()
128 server.wait_for_termination()
129
130
131 if __name__ == '__main__':
132 logging.basicConfig()
133 serve()