2019-06-10 13:32:45 +00:00
|
|
|
import logging
|
2018-03-25 13:43:59 +00:00
|
|
|
|
|
|
|
import zmq
|
2018-01-24 17:19:49 +00:00
|
|
|
from zmq.eventloop.zmqstream import ZMQStream
|
|
|
|
|
2019-05-27 12:09:13 +00:00
|
|
|
from .scope import Scope
|
2019-07-31 15:08:47 +00:00
|
|
|
from .serialization import (
|
|
|
|
serialize_tfw_msg,
|
|
|
|
deserialize_tfw_msg,
|
|
|
|
with_deserialize_tfw_msg
|
|
|
|
)
|
2019-05-27 12:09:13 +00:00
|
|
|
|
2018-06-29 09:50:36 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
2018-01-31 14:50:52 +00:00
|
|
|
|
2018-01-24 17:19:49 +00:00
|
|
|
|
2019-07-30 13:17:29 +00:00
|
|
|
class ZMQDownlinkConnector:
|
2019-06-04 11:58:03 +00:00
|
|
|
def __init__(self, connect_addr):
|
2019-05-27 18:02:09 +00:00
|
|
|
self._zmq_sub_socket = zmq.Context.instance().socket(zmq.SUB)
|
2019-05-21 11:55:28 +00:00
|
|
|
self._zmq_sub_socket.setsockopt(zmq.RCVHWM, 0)
|
2019-07-08 18:12:12 +00:00
|
|
|
self._zmq_sub_socket.connect(connect_addr)
|
2018-01-24 17:19:49 +00:00
|
|
|
self._zmq_sub_stream = ZMQStream(self._zmq_sub_socket)
|
|
|
|
|
2019-07-12 21:25:16 +00:00
|
|
|
def subscribe(self, *keys):
|
|
|
|
for key in keys:
|
|
|
|
self._zmq_sub_socket.setsockopt_string(zmq.SUBSCRIBE, key)
|
|
|
|
|
|
|
|
def unsubscribe(self, *keys):
|
|
|
|
for key in keys:
|
|
|
|
self._zmq_sub_socket.setsockopt_string(zmq.UNSUBSCRIBE, key)
|
2018-06-29 09:50:36 +00:00
|
|
|
|
|
|
|
def register_callback(self, callback):
|
2019-08-08 13:05:37 +00:00
|
|
|
callback = with_deserialize_tfw_msg(callback) if callback else None
|
|
|
|
self._zmq_sub_stream.on_recv(callback)
|
2019-07-31 15:08:47 +00:00
|
|
|
|
|
|
|
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!")
|
2019-07-12 21:25:16 +00:00
|
|
|
|
2019-05-20 09:06:57 +00:00
|
|
|
def close(self):
|
|
|
|
self._zmq_sub_stream.close()
|
|
|
|
|
2018-01-29 15:07:36 +00:00
|
|
|
|
2019-07-30 13:17:29 +00:00
|
|
|
class ZMQUplinkConnector:
|
2019-06-04 11:58:03 +00:00
|
|
|
def __init__(self, connect_addr):
|
2019-05-27 18:02:09 +00:00
|
|
|
self._zmq_push_socket = zmq.Context.instance().socket(zmq.PUSH)
|
2019-05-21 11:55:28 +00:00
|
|
|
self._zmq_push_socket.setsockopt(zmq.SNDHWM, 0)
|
2019-07-08 18:12:12 +00:00
|
|
|
self._zmq_push_socket.connect(connect_addr)
|
2018-01-29 15:07:36 +00:00
|
|
|
|
2019-05-26 16:26:33 +00:00
|
|
|
def send_message(self, message, scope=Scope.ZMQ):
|
|
|
|
message['scope'] = scope.value
|
2018-04-13 14:35:30 +00:00
|
|
|
self._zmq_push_socket.send_multipart(serialize_tfw_msg(message))
|
2018-01-29 15:07:36 +00:00
|
|
|
|
2019-05-20 09:06:57 +00:00
|
|
|
def close(self):
|
|
|
|
self._zmq_push_socket.close()
|
|
|
|
|
2018-01-29 15:07:36 +00:00
|
|
|
|
2019-07-30 13:17:29 +00:00
|
|
|
class ZMQConnector(ZMQDownlinkConnector, ZMQUplinkConnector):
|
2019-06-04 11:58:03 +00:00
|
|
|
def __init__(self, downlink_connect_addr, uplink_connect_addr):
|
2019-07-30 13:17:29 +00:00
|
|
|
ZMQDownlinkConnector.__init__(self, downlink_connect_addr)
|
|
|
|
ZMQUplinkConnector.__init__(self, uplink_connect_addr)
|
2019-05-27 18:02:09 +00:00
|
|
|
|
2019-05-20 09:06:57 +00:00
|
|
|
def close(self):
|
2019-07-30 13:17:29 +00:00
|
|
|
ZMQDownlinkConnector.close(self)
|
|
|
|
ZMQUplinkConnector.close(self)
|