Simplify package structure

This commit is contained in:
Kristóf Tóth
2019-07-24 15:50:41 +02:00
parent a23224aced
commit 52399f413c
79 changed files with 22 additions and 24 deletions

4
tfw/main/__init__.py Normal file
View File

@ -0,0 +1,4 @@
from .tfw_connector import TFWUplinkConnector, TFWConnector
from .event_handler_factory import EventHandlerFactory
from .signal_handling import setup_signal_handlers
from .tfw_server import TFWServer

View File

@ -0,0 +1,8 @@
from tfw.internals.event_handling import EventHandlerFactoryBase
from .tfw_connector import TFWConnector
class EventHandlerFactory(EventHandlerFactoryBase):
def _build_server_connector(self):
return TFWConnector()

View File

@ -0,0 +1,11 @@
from signal import signal, SIGTERM, SIGINT
from tfw.internals.event_handling import EventHandler
def setup_signal_handlers():
def stop(*_):
EventHandler.stop_all_instances()
exit(0)
signal(SIGTERM, stop)
signal(SIGINT, stop)

25
tfw/main/tfw_connector.py Normal file
View File

@ -0,0 +1,25 @@
from tfw.internals.networking import ServerConnector, ServerUplinkConnector
from tfw.config import TFWENV
class ConnAddrMixin:
@property
def uplink_conn_addr(self):
return f'tcp://localhost:{TFWENV.PULL_PORT}'
@property
def downlink_conn_addr(self):
return f'tcp://localhost:{TFWENV.PUB_PORT}'
class TFWUplinkConnector(ServerUplinkConnector, ConnAddrMixin):
def __init__(self):
super().__init__(self.uplink_conn_addr)
class TFWConnector(ServerConnector, ConnAddrMixin):
def __init__(self):
super().__init__(
self.downlink_conn_addr,
self.uplink_conn_addr
)

31
tfw/main/tfw_server.py Normal file
View File

@ -0,0 +1,31 @@
import logging
from tornado.web import Application
from tfw.internals.networking import EventHandlerConnector
from tfw.internals.server import ZMQWebSocketRouter
from tfw.config import TFWENV
LOG = logging.getLogger(__name__)
class TFWServer:
"""
This class handles the proxying of messages between the frontend and event handers.
It proxies messages from the "/ws" route to all event handlers subscribed to a ZMQ
SUB socket.
"""
def __init__(self):
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', ZMQWebSocketRouter, {
'event_handler_connector': self._event_handler_connector,
}
)])
def listen(self):
self.application.listen(TFWENV.WEB_PORT)