mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-15 05:57:18 +00:00
82 lines
2.6 KiB
Python
82 lines
2.6 KiB
Python
import logging
|
|
|
|
import zmq
|
|
from zmq.eventloop.zmqstream import ZMQStream
|
|
|
|
from .scope import Scope
|
|
from .serialization import (
|
|
serialize_tfw_msg,
|
|
deserialize_tfw_msg,
|
|
with_deserialize_tfw_msg
|
|
)
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
class ZMQDownlinkConnector:
|
|
def __init__(self, connect_addr):
|
|
self.keys = []
|
|
self._on_recv_callback = None
|
|
self._zmq_sub_socket = zmq.Context.instance().socket(zmq.SUB)
|
|
self._zmq_sub_socket.setsockopt(zmq.RCVHWM, 0)
|
|
self._zmq_sub_socket.connect(connect_addr)
|
|
self._zmq_sub_stream = ZMQStream(self._zmq_sub_socket)
|
|
|
|
def subscribe(self, *keys):
|
|
for key in keys:
|
|
self._zmq_sub_socket.setsockopt_string(zmq.SUBSCRIBE, key)
|
|
self.keys.append(key)
|
|
|
|
def unsubscribe(self, *keys):
|
|
for key in keys:
|
|
self._zmq_sub_socket.setsockopt_string(zmq.UNSUBSCRIBE, key)
|
|
self.keys.remove(key)
|
|
|
|
def register_callback(self, callback):
|
|
if callback:
|
|
self._on_recv_callback = callback
|
|
self._zmq_sub_stream.on_recv(with_deserialize_tfw_msg(self._on_recv))
|
|
else:
|
|
self._zmq_sub_stream.on_recv(None)
|
|
|
|
def recv_message(self, *, block=True):
|
|
if self._zmq_sub_stream.receiving():
|
|
raise RuntimeError('Synchronous recv() called while a callback is registered!')
|
|
flags = 0 if block else zmq.NOBLOCK
|
|
try:
|
|
return deserialize_tfw_msg(*self._zmq_sub_socket.recv_multipart(flags))
|
|
except zmq.ZMQError:
|
|
raise IOError("No data available to recv!")
|
|
|
|
def _on_recv(self, message):
|
|
key = message['key']
|
|
if key in self.keys or '' in self.keys:
|
|
self._on_recv_callback(message)
|
|
|
|
def close(self):
|
|
self._zmq_sub_stream.close()
|
|
|
|
|
|
class ZMQUplinkConnector:
|
|
def __init__(self, connect_addr):
|
|
self._zmq_push_socket = zmq.Context.instance().socket(zmq.PUSH)
|
|
self._zmq_push_socket.setsockopt(zmq.SNDHWM, 0)
|
|
self._zmq_push_socket.connect(connect_addr)
|
|
|
|
def send_message(self, message, scope=Scope.ZMQ):
|
|
message['scope'] = scope.value
|
|
self._zmq_push_socket.send_multipart(serialize_tfw_msg(message))
|
|
|
|
def close(self):
|
|
self._zmq_push_socket.close()
|
|
|
|
|
|
class ZMQConnector(ZMQDownlinkConnector, ZMQUplinkConnector):
|
|
def __init__(self, downlink_connect_addr, uplink_connect_addr):
|
|
ZMQDownlinkConnector.__init__(self, downlink_connect_addr)
|
|
ZMQUplinkConnector.__init__(self, uplink_connect_addr)
|
|
|
|
def close(self):
|
|
ZMQDownlinkConnector.close(self)
|
|
ZMQUplinkConnector.close(self)
|