-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomms.py
347 lines (279 loc) · 11.7 KB
/
comms.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
"""
Communication can be split between Inter-Process Communication (IPC) which
communicates between processes on the same device, and IP networking, aka
Internet Protocol, which would be between devices.
When executing on a unix-lixe device, IPC is manged via unix domain sockets.
Python's asyncio manages both with similar approaches but separate function
calls.
Abstracting out the commuication allows easy replacement as needed.
"""
import asyncio
import datetime
import json
from json import JSONDecodeError
import logging
from logging.handlers import TimedRotatingFileHandler
import os
from signal import (
SIGTERM,
SIGINT,
)
from settings import (
Config,
ProdConfig
)
class Comms:
""" This class abstracts communication between components.
Components on the same unix-like device use unix domain sockets.
Components on different devices use IP/TCP sockets.
Note:
This class is specific for unix domain sockets. The intention is to
extent it for IP/TCP.
To use, instanciate an instance with a device-unique name. This impies a
component specific socket at `<socket_root>/<name>.sock`. `start` will
create the socket if a server is necessary, i.e. if this components listens
for connections. Not all components need to listen, some just generate
requests.
Read via `new_data = await self.in_q.get()` to handing incoming data, and
write via `await self.out_q.put(output)` in the specific application.
If a device only makes requests, it can use the `request` method which
requires a response.
The rest of the communication is abstracted.
You can subclass this if you are a monster, or just instanciate it via
the `create_comms` helper function below.
FIXME: Haven't decided if this will be ONLY unix domain scokets and there
will be another class for IP/TCP, and a bridge will be required, or
if this can be stuffed with all types of connections and bridges
explicitly manged via the user of the instance.
"""
def __init__(self, name):
self.name = name
self.callback = None
self.clients = {}
self.config = None
self.connections = {}
self.logger = logging.getLogger(name)
self.server = None
self.servers = {}
self.socket_root = "."
self.in_q = asyncio.Queue()
self.out_q = asyncio.Queue()
self.set_callback()
self.tasks = {}
def set_config(self, config):
self.config = config
self.socket_root = config.SOCKET_ROOT
def start(self):
""" Start's a server listening to a unix domain socket which is
located in the `self.socket_root` directory with filename
`{self.name}.sock`.
Adds signal handlers for SIGTERM and SIGINT to call self.cleanup.
"""
self.server = asyncio.start_unix_server(
self.callback,
f"{self.socket_root}/{self.name}.sock"
)
self.tasks['receiver'] = asyncio.create_task(self.server)
self.tasks['responder'] = asyncio.create_task(self.response())
loop = asyncio.get_event_loop()
loop.add_signal_handler(SIGTERM, self.cleanup)
loop.add_signal_handler(SIGINT, self.cleanup)
def stop(self):
""" Stops the server and all associated tasks, removing the socket
from the filesystem"""
if self.server:
self.server.close()
sock_path = f"{self.name}.sock"
if sock_path in os.listdir(self.socket_root):
os.unlink(f"{self.socket_root}/{sock_path}")
task_names = list(self.tasks.keys())
while self.tasks:
task = self.tasks.pop(task_names.pop())
task.cancel()
def cleanup(self):
""" When a signal is received, this function is called to stop the
server and exit gracefully
"""
self.logger.warning("Cleaning up")
self.stop()
self.logger.warning("Exiting")
exit()
def set_callback(self):
""" Constructs the client callback for an asyncio server, which
closes over 'self' so the class' members are accessible in the
callback.
"""
async def client_callback(reader, writer):
""" Implements the basic communication protocol.
When a client connections, it MUST send a `\n` terminated
string.
That string MUST be decodeable as 'utf-8' into a valid
JSON string.
That JSON string MUST be valid request(see schema.py), though
for performance reasons it's not programmatically validated.
The request `source_id` dict member is used as the
client identification and the client connections are stored
in the self.connetions dict via that key.
The request's dictionary representation is placed into the
self.in_q for reading by the user of this comm instance.
This is looped for all incoming client data.
The server will close connections on errors in the incoming
data.
"""
req = await reader.readline()
if reader.at_eof():
return
if (req == b''):
return
try:
req = json.loads(req.decode('utf-8'))
except (AttributeError, JSONDecodeError) as e:
writer.close()
await writer.wait_closed()
raise e
if (src_id := req.get('source_id')):
if src_id in self.clients:
self.logger.error(f"Duplicate connection from {src_id}")
temp_r, temp_w = self.clients.pop(src_id)
try:
temp_w.close()
await temp_w.wait_closed()
except Exception as e:
self.logger(f"{e}")
if src_id not in self.clients:
self.clients[src_id] = (reader, writer)
await self.in_q.put(req)
while True:
req = await reader.readline()
self.logger.debug(f"{src_id} -> {req}")
if reader.at_eof():
self.logger.warning(f"Done with {src_id}")
reader, writer = self.clients.pop(src_id)
writer.close()
await writer.wait_closed()
break
if (req == b''):
self.logger.error("Empty request")
continue
try:
req = json.loads(req.decode('utf-8'))
except (AttributeError, JSONDecodeError) as e:
writer.close()
await writer.wait_closed()
raise e
if (src_id := req.get('source_id')):
if src_id not in self.clients:
self.clients[src_id] = (reader, writer)
await self.in_q.put(req)
self.callback = client_callback
async def connect(self, server):
""" Make a connection to a local unix domain socket in the
socket_root directory and keeps the connection in self.servers.
"""
if server not in self.servers:
sock_path = self.socket_root + "/" + server + ".sock"
reader, writer = await asyncio.open_unix_connection(sock_path)
self.servers[server] = (reader, writer)
async def disconnect(self, conn_name):
""" Disconnects from a local unix domain socket in self.servers.
"""
if conn_name in self.servers:
reader, writer = self.servers.pop(conn_name)
writer.close()
await writer.wait_closed()
async def request(self, addr, req, resp=True):
""" A one-off request to a server.
A user of this comms instance makes requests to other components on
this device via thier unique name which identifies a socket in
the socket_root directory.
Manages the client-side request of the protocol.
A request MUST be a valid request (see schema.py).
That request MUST (and in this case, will) encode as a 'utf-8'
json string.
That string MUST end with a b'\n'.
Expects a single response returned to the caller.
"""
if addr not in self.servers:
sock_path = f"{self.socket_root}/{addr}.sock"
reader, writer = await asyncio.open_unix_connection(sock_path)
self.servers[addr] = (reader, writer)
self.logger.debug(f"New connection made to {addr}")
else:
self.logger.debug(f"Found connection: {addr}")
reader, writer = self.servers[addr]
self.logger.debug(f"Sending req to {addr}")
msg = json.dumps(req).encode('utf-8')
writer.write(msg + b'\n')
await writer.drain()
if not resp:
return {}
self.logger.debug(f"Waiting on response from {addr}")
data = await reader.readline()
self.logger.debug(f"Got response from {addr}")
if data == b'':
return {}
try:
data = json.loads(data)
except JSONDecodeError as e:
self.logger.error(f"{e}")
self.logger.debug(f"{addr} said: {data}")
return data
async def response(self):
""" A response is always sent back to a connected client.
Data put into the out_q MUST be a valid response (see schema.py).
The source_id of the response is where the response is going.
The source_id ought to be in self.clients dict to use the
connection created via the server's client_callback.
The Server does not close the connections.
Note that the response must be generated by the user of this
comm instance.
"""
while True:
data = await self.out_q.get()
client_id = data['source_id']
msg = json.dumps(data).encode('utf-8')
if client_id in self.clients:
_, client = self.clients[client_id]
client.write(msg + b'\n')
await client.drain()
def config_logging(comms, comms_config):
""" Configures logging for comms related components.
Uses a TimedRotatingFileHandler to rotate logs automatically
once a week, keeping a years worth of logs by default.
Pass in a Comm instance and a Config from settings.py.
"""
handler = TimedRotatingFileHandler(
filename=comms_config.LOG_FILE,
when="W6",
interval=1,
backupCount=2,
encoding=None,
delay=False,
utc=True,
atTime=datetime.time(0, 0))
formatter = logging.Formatter(comms_config.LOG_FORMAT)
handler.setLevel(comms_config.LOG_LEVEL)
handler.setFormatter(formatter)
comms.logger.addHandler(handler)
comms.logger.setLevel(comms_config.LOG_LEVEL)
comms.logger.info("Logging enabled")
def create_comms(name):
""" Helper function to create a configured Comms instance.
Specificy a `name` which is unique on the executing device which
corresponse to the component using a unix domain socket with the
same name.
e.g. you must consider the `name` a a device-unique identifier which
addresses this component, and that other components on this device will
use to address this component.
"""
comms = Comms(name)
# I don't like this. Inherent from some flask stuff.
# make a loader/executor which just configs the thing, or something.
if os.environ.get('QUEERIOUSLABS_ENV') == 'PROD':
comms_config = ProdConfig()
else:
comms_config = Config()
comms.set_config(comms_config)
config_logging(comms, comms_config)
comms.logger.info(f"App {name} initialized")
return comms