mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2025-06-28 09:35:11 +00:00
Simplify package structure
This commit is contained in:
4
tfw/main/__init__.py
Normal file
4
tfw/main/__init__.py
Normal 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
|
8
tfw/main/event_handler_factory.py
Normal file
8
tfw/main/event_handler_factory.py
Normal 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()
|
11
tfw/main/signal_handling.py
Normal file
11
tfw/main/signal_handling.py
Normal 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
25
tfw/main/tfw_connector.py
Normal 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
31
tfw/main/tfw_server.py
Normal 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)
|
Reference in New Issue
Block a user