| 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 |
|
| 16 |
from concurrent import futures |
| 17 |
import logging |
| 18 |
import math |
| 19 |
import time |
| 20 |
|
| 21 |
import grpc |
| 22 |
import route_guide_pb2 |
| 23 |
import route_guide_pb2_grpc |
| 24 |
import route_guide_resources |
| 25 |
|
| 26 |
|
| 27 |
def get_feature(feature_db, point): |
| 28 |
"""Returns Feature at given location or None.""" |
| 29 |
for feature in feature_db: |
| 30 |
if feature.location == point: |
| 31 |
return feature |
| 32 |
return None |
| 33 |
|
| 34 |
|
| 35 |
def get_distance(start, end): |
| 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) |
| 46 |
|
| 47 |
# Formula is based on http://mathforum.org/library/drmath/view/51879.html |
| 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)) |
| 52 |
R = 6371000 |
| 53 |
# metres |
| 54 |
return R * c |
| 55 |
|
| 56 |
|
| 57 |
class RouteGuideServicer(route_guide_pb2_grpc.RouteGuideServicer): |
| 58 |
"""Provides methods that implement functionality of route guide server.""" |
| 59 |
|
| 60 |
def __init__(self): |
| 61 |
self.db = route_guide_resources.read_route_guide_database() |
| 62 |
|
| 63 |
def GetFeature(self, request, context): |
| 64 |
feature = get_feature(self.db, request) |
| 65 |
if feature is None: |
| 66 |
return route_guide_pb2.Feature(name="", location=request) |
| 67 |
else: |
| 68 |
return feature |
| 69 |
|
| 70 |
def ListFeatures(self, request, context): |
| 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): |
| 80 |
yield feature |
| 81 |
|
| 82 |
def RecordRoute(self, request_iterator, context): |
| 83 |
point_count = 0 |
| 84 |
feature_count = 0 |
| 85 |
distance = 0.0 |
| 86 |
prev_point = None |
| 87 |
|
| 88 |
start_time = time.time() |
| 89 |
for point in request_iterator: |
| 90 |
point_count += 1 |
| 91 |
if get_feature(self.db, point): |
| 92 |
feature_count += 1 |
| 93 |
if prev_point: |
| 94 |
distance += get_distance(prev_point, point) |
| 95 |
prev_point = point |
| 96 |
|
| 97 |
elapsed_time = time.time() - start_time |
| 98 |
return route_guide_pb2.RouteSummary(point_count=point_count, |
| 99 |
feature_count=feature_count, |
| 100 |
distance=int(distance), |
| 101 |
elapsed_time=int(elapsed_time)) |
| 102 |
|
| 103 |
def RouteChat(self, request_iterator, context): |
| 104 |
prev_notes = [] |
| 105 |
for new_note in request_iterator: |
| 106 |
for prev_note in prev_notes: |
| 107 |
if prev_note.location == new_note.location: |
| 108 |
yield prev_note |
| 109 |
prev_notes.append(new_note) |
| 110 |
|
| 111 |
|
| 112 |
def serve(): |
| 113 |
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) |
| 114 |
route_guide_pb2_grpc.add_RouteGuideServicer_to_server( |
| 115 |
RouteGuideServicer(), server) |
| 116 |
server.add_insecure_port('[::]:50051') |
| 117 |
server.start() |
| 118 |
server.wait_for_termination() |
| 119 |
|
| 120 |
|
| 121 |
if __name__ == '__main__': |
| 122 |
logging.basicConfig() |
| 123 |
serve() |