2018-01-24 17:19:49 +00:00
|
|
|
import zmq
|
2018-01-31 14:50:52 +00:00
|
|
|
from functools import partial
|
2018-01-24 17:19:49 +00:00
|
|
|
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 ..util import ZMQConnectorBase
|
|
|
|
|
2018-01-24 17:19:49 +00:00
|
|
|
|
2018-01-25 14:24:01 +00:00
|
|
|
ioloop.install()
|
|
|
|
|
2018-01-24 17:19:49 +00:00
|
|
|
|
2018-01-30 13:31:00 +00:00
|
|
|
class ServerDownlinkConnector(ZMQConnectorBase):
|
2018-01-29 15:07:36 +00:00
|
|
|
def __init__(self, zmq_context=None):
|
|
|
|
super(ServerDownlinkConnector, self).__init__(zmq_context)
|
2018-01-24 17:19:49 +00:00
|
|
|
self._zmq_sub_socket = self._zmq_context.socket(zmq.SUB)
|
|
|
|
self._zmq_sub_socket.connect('tcp://localhost:{}'.format(PUBLISHER_PORT))
|
|
|
|
self._zmq_sub_stream = ZMQStream(self._zmq_sub_socket)
|
|
|
|
|
|
|
|
self.subscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.SUBSCRIBE)
|
|
|
|
self.unsubscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.UNSUBSCRIBE)
|
|
|
|
self.register_callback = self._zmq_sub_stream.on_recv
|
2018-01-26 14:16:34 +00:00
|
|
|
|
2018-01-29 15:07:36 +00:00
|
|
|
|
2018-01-30 13:31:00 +00:00
|
|
|
class ServerUplinkConnector(ZMQConnectorBase):
|
2018-01-29 15:07:36 +00:00
|
|
|
def __init__(self, zmq_context=None):
|
|
|
|
super(ServerUplinkConnector, self).__init__(zmq_context)
|
|
|
|
self._zmq_push_socket = self._zmq_context.socket(zmq.PUSH)
|
|
|
|
self._zmq_push_socket.connect('tcp://localhost:{}'.format(RECEIVER_PORT))
|
|
|
|
|
2018-01-24 17:19:49 +00:00
|
|
|
def send(self, anchor, response):
|
2018-02-02 16:30:26 +00:00
|
|
|
self._zmq_push_socket.send_multipart(serialize_all(anchor, response))
|
2018-01-29 15:07:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ServerConnector(ServerUplinkConnector, ServerDownlinkConnector):
|
|
|
|
def __init__(self, zmq_context=None):
|
|
|
|
super(ServerConnector, self).__init__(zmq_context)
|