-
Notifications
You must be signed in to change notification settings - Fork 9
/
location_server.py
executable file
·75 lines (58 loc) · 2.41 KB
/
location_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import logging
import time
from threading import Thread
import grpc
from concurrent import futures
from grpc_support import GenericServer
from utils import setup_logging
from utils import sleep
from proto.location_service_pb2 import Location
from proto.location_service_pb2 import LocationServiceServicer
from proto.location_service_pb2 import ServerInfo
from proto.location_service_pb2 import add_LocationServiceServicer_to_server
logger = logging.getLogger(__name__)
class LocationServer(LocationServiceServicer, GenericServer):
def __init__(self, port=None):
super(LocationServer, self).__init__(port=port, desc="location server")
self.grpc_server = None
def registerClient(self, request, context):
logger.info("Connected to {0} client {1} [{2}]".format(self.desc, context.peer(), request.info))
return ServerInfo(info="Server invoke count {0}".format(self.increment_cnt()))
def getLocations(self, request, context):
client_info = request.info
return self.currval_generator(context.peer())
def _init_values_on_start(self):
self.write_location(-1, -1, 0, 0, 0)
def _start_server(self):
logger.info("Starting gRPC {0} listening on {1}".format(self.desc, self.hostname))
self.grpc_server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
add_LocationServiceServicer_to_server(self, self.grpc_server)
self.grpc_server.add_insecure_port(self.hostname)
self.grpc_server.start()
try:
while not self.stopped:
time.sleep(1)
except KeyboardInterrupt:
pass
finally:
self.stop()
def write_location(self, x, y, width, height, middle_inc):
if not self.stopped:
self.set_currval(Location(id=self.id,
x=x,
y=y,
width=width,
height=height,
middle_inc=middle_inc))
self.id += 1
def main():
def _run_server(port):
server = LocationServer(port).start()
for i in range(100):
server.write_location(x=i, y=i + 1, width=i + 2, height=i + 3, middle_inc=i + 4)
time.sleep(1)
setup_logging()
Thread(target=_run_server, args=(50052,)).start()
sleep()
if __name__ == "__main__":
main()