Split ServerConnector to single responsibility classes

This commit is contained in:
Bálint Bokros 2018-01-29 16:07:36 +01:00
parent e05ae079ad
commit cd5f7084da

View File

@ -9,18 +9,33 @@ from config import PUBLISHER_PORT, RECEIVER_PORT
ioloop.install() ioloop.install()
class ServerConnector: class ServerConnectorBase:
def __init__(self): def __init__(self, zmq_context=None):
self._zmq_context = zmq.Context.instance() self._zmq_context = zmq_context or zmq.Context.instance()
class ServerDownlinkConnector(ServerConnectorBase):
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 = self._zmq_context.socket(zmq.SUB)
self._zmq_sub_socket.connect('tcp://localhost:{}'.format(PUBLISHER_PORT)) self._zmq_sub_socket.connect('tcp://localhost:{}'.format(PUBLISHER_PORT))
self._zmq_sub_stream = ZMQStream(self._zmq_sub_socket) self._zmq_sub_stream = ZMQStream(self._zmq_sub_socket)
self._zmq_push_socket = self._zmq_context.socket(zmq.PUSH)
self._zmq_push_socket.connect('tcp://localhost:{}'.format(RECEIVER_PORT))
self.subscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.SUBSCRIBE) self.subscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.SUBSCRIBE)
self.unsubscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.UNSUBSCRIBE) self.unsubscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.UNSUBSCRIBE)
self.register_callback = self._zmq_sub_stream.on_recv self.register_callback = self._zmq_sub_stream.on_recv
class ServerUplinkConnector(ServerConnectorBase):
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): def send(self, anchor, response):
self._zmq_push_socket.send_multipart([anchor, response]) self._zmq_push_socket.send_multipart([anchor, response])
class ServerConnector(ServerUplinkConnector, ServerDownlinkConnector):
def __init__(self, zmq_context=None):
super(ServerConnector, self).__init__(zmq_context)