-
Notifications
You must be signed in to change notification settings - Fork 9
/
location_client.py
executable file
·95 lines (77 loc) · 3.2 KB
/
location_client.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import copy
import logging
import socket
import time
from threading import Event
import grpc
from arc852.grpc_support import GenericClient
from arc852.grpc_support import TimeoutException
from arc852.utils import setup_logging
from proto.location_service_pb2 import ClientInfo
from proto.location_service_pb2 import LocationServiceStub
logger = logging.getLogger(__name__)
class LocationClient(GenericClient):
def __init__(self, hostname):
super(LocationClient, self).__init__(hostname, desc="location client")
self.__x_ready = Event()
self.__y_ready = Event()
self.__currval = None
def _mark_ready(self):
self.__x_ready.set()
self.__y_ready.set()
def _get_values(self, pause_secs=2.0):
channel = grpc.insecure_channel(self.hostname)
stub = LocationServiceStub(channel)
while not self.stopped:
logger.info("Connecting to gRPC server at {0}...".format(self.hostname))
try:
client_info = ClientInfo(info="{0} client".format(socket.gethostname()))
server_info = stub.registerClient(client_info)
except BaseException as e:
logger.error("Failed to connect to gRPC server at {0} [{1}]".format(self.hostname, e))
time.sleep(pause_secs)
continue
logger.info("Connected to gRPC server at {0} [{1}]".format(self.hostname, server_info.info))
try:
for val in stub.getLocations(client_info):
with self.value_lock:
self.__currval = copy.deepcopy(val)
self._mark_ready()
except BaseException as e:
logger.info("Disconnected from gRPC server at {0} [{1}]".format(self.hostname, e))
time.sleep(pause_secs)
# Non-blocking
def get_loc(self, name):
return self.__currval.x if name == "x" else self.__currval.y
# Non-blocking
def get_size(self, name):
return self.__currval.width if name == "x" else self.__currval.height
# Blocking
def get_x(self, timeout=None):
while not self.stopped:
if not self.__x_ready.wait(timeout):
raise TimeoutException
with self.value_lock:
if self.__x_ready.is_set() and not self.stopped:
self.__x_ready.clear()
return self.__currval.x, self.__currval.width, self.__currval.middle_inc, self.__currval.id
# Blocking
def get_y(self, timeout=None):
while not self.stopped:
if not self.__y_ready.wait(timeout):
raise TimeoutException
with self.value_lock:
if self.__y_ready.is_set() and not self.stopped:
self.__y_ready.clear()
return self.__currval.y, self.__currval.height, self.__currval.middle_inc, self.__currval.id
# Blocking
def get_xy(self):
return self.get_x(), self.get_y()
def main():
setup_logging()
with LocationClient("localhost") as client:
for i in range(1000):
logger.info("Read value: {0}".format(client.get_xy()))
logger.info("Exiting...")
if __name__ == "__main__":
main()