2017-11-27 17:16:35 +00:00
|
|
|
import zmq
|
|
|
|
from zmq.eventloop import ioloop
|
|
|
|
from zmq.eventloop.zmqstream import ZMQStream
|
|
|
|
|
2018-02-02 16:30:26 +00:00
|
|
|
from .serialization import serialize_all
|
2018-01-31 14:50:52 +00:00
|
|
|
from ..config import PUBLISHER_PORT, RECEIVER_PORT
|
|
|
|
from ..config.logs import logging
|
2018-01-29 17:36:29 +00:00
|
|
|
log = logging.getLogger(__name__)
|
2018-01-31 14:50:52 +00:00
|
|
|
from ..util import parse_anchor_from_message, ZMQConnectorBase
|
2017-11-27 17:16:35 +00:00
|
|
|
|
2018-01-29 17:36:29 +00:00
|
|
|
|
2017-11-27 17:16:35 +00:00
|
|
|
ioloop.install()
|
|
|
|
|
|
|
|
|
2018-01-29 14:28:51 +00:00
|
|
|
class EventHandlerDownlinkConnector(ZMQConnectorBase):
|
|
|
|
def __init__(self, zmq_context=None):
|
2018-01-29 15:02:52 +00:00
|
|
|
super(EventHandlerDownlinkConnector, self).__init__(zmq_context)
|
2018-01-29 14:28:51 +00:00
|
|
|
self._zmq_pull_socket = self._zmq_context.socket(zmq.PULL)
|
|
|
|
self._zmq_pull_stream = ZMQStream(self._zmq_pull_socket)
|
|
|
|
address = 'tcp://*:{}'.format(RECEIVER_PORT)
|
|
|
|
self._zmq_pull_socket.bind(address)
|
2018-01-29 17:36:29 +00:00
|
|
|
log.debug('Pull socket bound to {}'.format(address))
|
2017-11-27 17:16:35 +00:00
|
|
|
|
|
|
|
|
2018-01-29 14:28:51 +00:00
|
|
|
class EventHandlerUplinkConnector(ZMQConnectorBase):
|
|
|
|
def __init__(self, zmq_context=None):
|
2018-01-29 15:02:52 +00:00
|
|
|
super(EventHandlerUplinkConnector, self).__init__(zmq_context)
|
2018-01-29 14:28:51 +00:00
|
|
|
self._zmq_pub_socket = self._zmq_context.socket(zmq.PUB)
|
|
|
|
address = 'tcp://*:{}'.format(PUBLISHER_PORT)
|
|
|
|
self._zmq_pub_socket.bind(address)
|
2018-01-29 17:36:29 +00:00
|
|
|
log.debug('Pub socket bound to {}'.format(address))
|
2017-11-27 17:16:35 +00:00
|
|
|
|
|
|
|
|
2018-01-29 14:28:51 +00:00
|
|
|
class EventHandlerConnector(EventHandlerDownlinkConnector, EventHandlerUplinkConnector):
|
|
|
|
def __init__(self, zmq_context=None):
|
2018-01-29 15:02:52 +00:00
|
|
|
super(EventHandlerConnector, self).__init__(zmq_context)
|
2017-11-27 17:16:35 +00:00
|
|
|
|
2018-01-29 14:28:51 +00:00
|
|
|
def register_callback(self, callback):
|
2018-01-29 15:02:52 +00:00
|
|
|
self._zmq_pull_stream.on_recv(callback)
|
2018-01-29 14:28:51 +00:00
|
|
|
|
|
|
|
def send_message(self, message: str, anchor: str = None):
|
|
|
|
if not anchor:
|
|
|
|
anchor = parse_anchor_from_message(message)
|
2018-02-02 16:30:26 +00:00
|
|
|
self._zmq_pub_socket.send_multipart(serialize_all(anchor, message))
|