-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdl_listener.py
51 lines (37 loc) · 1.28 KB
/
hdl_listener.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
import asyncio
import threading
import logging
import hdl_component
logger = logging.getLogger(__name__)
class HDLListener(asyncio.DatagramProtocol):
def __init__(self, host, port, components_ctl):
self.transport = None
self.host = host
self.port = port
self.components_ctl = components_ctl
def connection_made(self, transport):
self.transport = transport
def datagram_received(self, data, addr):
try:
self.components_ctl.update(data)
except hdl_component.HDLValidationError as e:
logger.error(f"Update error: {e}")
except Exception as e:
logger.error(f"Unhandled exception: {e}")
def worker(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
logger.debug("Starting UDP server")
def protocol_factory():
return self
listen = loop.create_datagram_endpoint(protocol_factory, local_addr=(self.host, self.port))
transport, protocol = loop.run_until_complete(listen)
try:
loop.run_forever()
except KeyboardInterrupt:
pass
transport.close()
def run(self):
thread = threading.Thread(target=self.worker)
thread.start()
return thread