| 1 |
# Copyright 2020 The 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 AsyncIO implementation of the gRPC route guide server.""" |
| 15 |
|
| 16 |
import asyncio |
| 17 |
import logging |
| 18 |
import math |
| 19 |
import time |
| 20 |
from typing import AsyncIterable, Iterable |
| 21 |
|
| 22 |
import grpc |
| 23 |
import route_guide_pb2 |
| 24 |
import route_guide_pb2_grpc |
| 25 |
import route_guide_resources |
| 26 |
|
| 27 |
|
| 28 |
def get_feature(feature_db: Iterable[route_guide_pb2.Feature], |
| 29 |
point: route_guide_pb2.Point) -> route_guide_pb2.Feature: |
| 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: route_guide_pb2.Point, |
| 38 |
end: route_guide_pb2.Point) -> float: |
| 39 |
"""Distance between two points.""" |
| 40 |
coord_factor = 10000000.0 |
| 41 |
lat_1 = start.latitude / coord_factor |
| 42 |
lat_2 = end.latitude / coord_factor |
| 43 |
lon_1 = start.longitude / coord_factor |
| 44 |
lon_2 = end.longitude / coord_factor |
| 45 |
lat_rad_1 = math.radians(lat_1) |
| 46 |
lat_rad_2 = math.radians(lat_2) |
| 47 |
delta_lat_rad = math.radians(lat_2 - lat_1) |
| 48 |
delta_lon_rad = math.radians(lon_2 - lon_1) |
| 49 |
|
| 50 |
# Formula is based on http://mathforum.org/library/drmath/view/51879.html |
| 51 |
a = (pow(math.sin(delta_lat_rad / 2), 2) + |
| 52 |
(math.cos(lat_rad_1) * math.cos(lat_rad_2) * |
| 53 |
pow(math.sin(delta_lon_rad / 2), 2))) |
| 54 |
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)) |
| 55 |
R = 6371000 |
| 56 |
# metres |
| 57 |
return R * c |
| 58 |
|
| 59 |
|
| 60 |
class RouteGuideServicer(route_guide_pb2_grpc.RouteGuideServicer): |
| 61 |
"""Provides methods that implement functionality of route guide server.""" |
| 62 |
|
| 63 |
def __init__(self) -> None: |
| 64 |
self.db = route_guide_resources.read_route_guide_database() |
| 65 |
|
| 66 |
def GetFeature(self, request: route_guide_pb2.Point, |
| 67 |
unused_context) -> route_guide_pb2.Feature: |
| 68 |
feature = get_feature(self.db, request) |
| 69 |
if feature is None: |
| 70 |
return route_guide_pb2.Feature(name="", location=request) |
| 71 |
else: |
| 72 |
return feature |
| 73 |
|
| 74 |
async def ListFeatures( |
| 75 |
self, request: route_guide_pb2.Rectangle, |
| 76 |
unused_context) -> AsyncIterable[route_guide_pb2.Feature]: |
| 77 |
left = min(request.lo.longitude, request.hi.longitude) |
| 78 |
right = max(request.lo.longitude, request.hi.longitude) |
| 79 |
top = max(request.lo.latitude, request.hi.latitude) |
| 80 |
bottom = min(request.lo.latitude, request.hi.latitude) |
| 81 |
for feature in self.db: |
| 82 |
if (feature.location.longitude >= left and |
| 83 |
feature.location.longitude <= right and |
| 84 |
feature.location.latitude >= bottom and |
| 85 |
feature.location.latitude <= top): |
| 86 |
yield feature |
| 87 |
|
| 88 |
async def RecordRoute(self, request_iterator: AsyncIterable[ |
| 89 |
route_guide_pb2.Point], unused_context) -> route_guide_pb2.RouteSummary: |
| 90 |
point_count = 0 |
| 91 |
feature_count = 0 |
| 92 |
distance = 0.0 |
| 93 |
prev_point = None |
| 94 |
|
| 95 |
start_time = time.time() |
| 96 |
async for point in request_iterator: |
| 97 |
point_count += 1 |
| 98 |
if get_feature(self.db, point): |
| 99 |
feature_count += 1 |
| 100 |
if prev_point: |
| 101 |
distance += get_distance(prev_point, point) |
| 102 |
prev_point = point |
| 103 |
|
| 104 |
elapsed_time = time.time() - start_time |
| 105 |
return route_guide_pb2.RouteSummary(point_count=point_count, |
| 106 |
feature_count=feature_count, |
| 107 |
distance=int(distance), |
| 108 |
elapsed_time=int(elapsed_time)) |
| 109 |
|
| 110 |
async def RouteChat( |
| 111 |
self, request_iterator: AsyncIterable[route_guide_pb2.RouteNote], |
| 112 |
unused_context) -> AsyncIterable[route_guide_pb2.RouteNote]: |
| 113 |
prev_notes = [] |
| 114 |
async for new_note in request_iterator: |
| 115 |
for prev_note in prev_notes: |
| 116 |
if prev_note.location == new_note.location: |
| 117 |
yield prev_note |
| 118 |
prev_notes.append(new_note) |
| 119 |
|
| 120 |
|
| 121 |
async def serve() -> None: |
| 122 |
server = grpc.aio.server() |
| 123 |
route_guide_pb2_grpc.add_RouteGuideServicer_to_server( |
| 124 |
RouteGuideServicer(), server) |
| 125 |
server.add_insecure_port('[::]:50051') |
| 126 |
await server.start() |
| 127 |
await server.wait_for_termination() |
| 128 |
|
| 129 |
|
| 130 |
if __name__ == '__main__': |
| 131 |
logging.basicConfig(level=logging.INFO) |
| 132 |
asyncio.get_event_loop().run_until_complete(serve()) |