baseimage-tutorial-framework/tfw/main/tfw_server.py

31 lines
863 B
Python

import logging
from tornado.web import Application
from tfw.internals.networking import ZMQListener
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._listener = ZMQListener(
downlink_bind_addr=f'tcp://*:{TFWENV.PULL_PORT}',
uplink_bind_addr=f'tcp://*:{TFWENV.PUB_PORT}'
)
self.application = Application([(
r'/ws', ZMQWebSocketRouter, {
'listener': self._listener,
}
)])
def listen(self):
self.application.listen(TFWENV.WEB_PORT)