Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: third_party/grpc/examples/python/route_guide/route_guide_server.py

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2015-2016, Google Inc.
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 """The Python implementation of the gRPC route guide server."""
31
32 import time
33 import math
34
35 import route_guide_pb2
36 import route_guide_resources
37
38 _ONE_DAY_IN_SECONDS = 60 * 60 * 24
39
40
41 def get_feature(feature_db, point):
42 """Returns Feature at given location or None."""
43 for feature in feature_db:
44 if feature.location == point:
45 return feature
46 return None
47
48
49 def get_distance(start, end):
50 """Distance between two points."""
51 coord_factor = 10000000.0
52 lat_1 = start.latitude / coord_factor
53 lat_2 = end.latitude / coord_factor
54 lon_1 = start.latitude / coord_factor
55 lon_2 = end.longitude / coord_factor
56 lat_rad_1 = math.radians(lat_1)
57 lat_rad_2 = math.radians(lat_2)
58 delta_lat_rad = math.radians(lat_2 - lat_1)
59 delta_lon_rad = math.radians(lon_2 - lon_1)
60
61 a = (pow(math.sin(delta_lat_rad / 2), 2) +
62 (math.cos(lat_rad_1) * math.cos(lat_rad_2) *
63 pow(math.sin(delta_lon_rad / 2), 2)))
64 c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
65 R = 6371000; # metres
66 return R * c;
67
68 class RouteGuideServicer(route_guide_pb2.BetaRouteGuideServicer):
69 """Provides methods that implement functionality of route guide server."""
70
71 def __init__(self):
72 self.db = route_guide_resources.read_route_guide_database()
73
74 def GetFeature(self, request, context):
75 feature = get_feature(self.db, request)
76 if feature is None:
77 return route_guide_pb2.Feature(name="", location=request)
78 else:
79 return feature
80
81 def ListFeatures(self, request, context):
82 left = min(request.lo.longitude, request.hi.longitude)
83 right = max(request.lo.longitude, request.hi.longitude)
84 top = max(request.lo.latitude, request.hi.latitude)
85 bottom = min(request.lo.latitude, request.hi.latitude)
86 for feature in self.db:
87 if (feature.location.longitude >= left and
88 feature.location.longitude <= right and
89 feature.location.latitude >= bottom and
90 feature.location.latitude <= top):
91 yield feature
92
93 def RecordRoute(self, request_iterator, context):
94 point_count = 0
95 feature_count = 0
96 distance = 0.0
97 prev_point = None
98
99 start_time = time.time()
100 for point in request_iterator:
101 point_count += 1
102 if get_feature(self.db, point):
103 feature_count += 1
104 if prev_point:
105 distance += get_distance(prev_point, point)
106 prev_point = point
107
108 elapsed_time = time.time() - start_time
109 return route_guide_pb2.RouteSummary(point_count=point_count,
110 feature_count=feature_count,
111 distance=int(distance),
112 elapsed_time=int(elapsed_time))
113
114 def RouteChat(self, request_iterator, context):
115 prev_notes = []
116 for new_note in request_iterator:
117 for prev_note in prev_notes:
118 if prev_note.location == new_note.location:
119 yield prev_note
120 prev_notes.append(new_note)
121
122
123 def serve():
124 server = route_guide_pb2.beta_create_RouteGuide_server(RouteGuideServicer())
125 server.add_insecure_port('[::]:50051')
126 server.start()
127 try:
128 while True:
129 time.sleep(_ONE_DAY_IN_SECONDS)
130 except KeyboardInterrupt:
131 server.stop(0)
132
133 if __name__ == '__main__':
134 serve()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698