baseimage-tutorial-framework/lib/tfw/networking/server/tfw_server.py
2018-06-29 22:03:19 +02:00

44 lines
1.4 KiB
Python

# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details.
from tornado.web import Application
from tfw.networking.event_handlers import ServerUplinkConnector
from tfw.networking.server import EventHandlerConnector
from tfw.config.logs import logging
from .zmq_websocket_proxy import ZMQWebSocketProxy
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()
self._uplink_connector = ServerUplinkConnector()
self.application = Application([(
r'/ws', ZMQWebSocketProxy,{
'event_handler_connector': self._event_handler_connector,
'message_handlers': [self.handle_trigger]
})]
)
def handle_trigger(self, message):
if 'trigger' in message:
LOG.debug('Executing handler for trigger "%s"', message.get('trigger', ''))
self._uplink_connector.send_to_eventhandler({
'key': 'fsm',
'data': {
'command': 'trigger',
'value': message.get('trigger', '')
}
})
def listen(self, port):
self.application.listen(port)