Improve module dependencies by moving port envvars out of tfw.networking

This commit is contained in:
Kristóf Tóth
2019-06-04 13:58:03 +02:00
parent f151ecfbac
commit c8e98af516
14 changed files with 68 additions and 45 deletions

View File

@ -4,9 +4,10 @@
from tornado.web import Application
from tfw.networking import EventHandlerConnector
from tfw.config import TFWENV
from tfw.config.logs import logging
from .zmq_websocket_proxy import ZMQWebSocketProxy
from .zmq_websocket_router import ZMQWebSocketRouter
LOG = logging.getLogger(__name__)
@ -18,12 +19,15 @@ class TFWServer:
SUB socket.
"""
def __init__(self):
self._event_handler_connector = EventHandlerConnector()
self._event_handler_connector = EventHandlerConnector(
downlink_bind_addr=f'tcp://*:{TFWENV.PULL_PORT}',
uplink_bind_addr=f'tcp://*:{TFWENV.PUB_PORT}'
)
self.application = Application([(
r'/ws', ZMQWebSocketProxy, {
r'/ws', ZMQWebSocketRouter, {
'event_handler_connector': self._event_handler_connector,
}
)])
def listen(self, port):
self.application.listen(port)
def listen(self):
self.application.listen(TFWENV.WEB_PORT)

View File

@ -11,7 +11,7 @@ from tfw.config.logs import logging
LOG = logging.getLogger(__name__)
class ZMQWebSocketProxy(WebSocketHandler):
class ZMQWebSocketRouter(WebSocketHandler):
# pylint: disable=abstract-method
instances = set()
@ -22,16 +22,16 @@ class ZMQWebSocketProxy(WebSocketHandler):
def send_to_zmq(self, message):
self.event_handler_connector.send_message(message)
@staticmethod
def send_to_websockets(message):
for instance in ZMQWebSocketProxy.instances:
@classmethod
def send_to_websockets(cls, message):
for instance in cls.instances:
instance.write_message(message)
def prepare(self):
ZMQWebSocketProxy.instances.add(self)
type(self).instances.add(self)
def on_close(self):
ZMQWebSocketProxy.instances.remove(self)
type(self).instances.remove(self)
def open(self, *args, **kwargs):
LOG.debug('WebSocket connection initiated!')