mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-12 19:37:16 +00:00
39 lines
1.4 KiB
Python
39 lines
1.4 KiB
Python
import zmq
|
|
from functools import partial
|
|
from zmq.eventloop import ioloop
|
|
from zmq.eventloop.zmqstream import ZMQStream
|
|
|
|
from .serialization import serialize_all
|
|
from ..config import PUBLISHER_PORT, RECEIVER_PORT
|
|
from ..util import ZMQConnectorBase
|
|
|
|
|
|
ioloop.install()
|
|
|
|
|
|
class ServerDownlinkConnector(ZMQConnectorBase):
|
|
def __init__(self, zmq_context=None):
|
|
super(ServerDownlinkConnector, self).__init__(zmq_context)
|
|
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
|
|
|
|
|
|
class ServerUplinkConnector(ZMQConnectorBase):
|
|
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))
|
|
|
|
def send(self, anchor, response):
|
|
self._zmq_push_socket.send_multipart(serialize_all(anchor, response))
|
|
|
|
|
|
class ServerConnector(ServerUplinkConnector, ServerDownlinkConnector):
|
|
def __init__(self, zmq_context=None):
|
|
super(ServerConnector, self).__init__(zmq_context)
|