2018-04-03 12:49:14 +00:00
|
|
|
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
|
2018-07-13 13:24:45 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2018-07-20 18:47:10 +00:00
|
|
|
from contextlib import suppress
|
2018-07-13 13:24:45 +00:00
|
|
|
|
2018-03-25 13:43:59 +00:00
|
|
|
from tornado.web import Application
|
|
|
|
|
2018-04-06 13:21:45 +00:00
|
|
|
from tfw.networking.event_handlers import ServerUplinkConnector
|
2018-04-06 14:04:25 +00:00
|
|
|
from tfw.networking.server import EventHandlerConnector
|
2018-07-13 13:24:45 +00:00
|
|
|
from tfw.networking import MessageSender
|
2018-07-25 13:46:39 +00:00
|
|
|
from tfw.networking.fsm_aware import FSMAware
|
2018-07-23 15:14:44 +00:00
|
|
|
from tfw.crypto import KeyManager, verify_message, sign_message
|
2018-02-23 11:07:30 +00:00
|
|
|
from tfw.config.logs import logging
|
2018-07-25 13:46:39 +00:00
|
|
|
|
2018-06-29 13:40:07 +00:00
|
|
|
from .zmq_websocket_proxy import ZMQWebSocketProxy
|
2018-03-25 14:06:59 +00:00
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
2018-02-11 11:32:30 +00:00
|
|
|
|
|
|
|
|
2018-07-25 13:46:39 +00:00
|
|
|
class TFWServer(FSMAware):
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
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
|
2018-06-29 20:03:19 +00:00
|
|
|
SUB socket.
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
2018-06-29 20:03:19 +00:00
|
|
|
def __init__(self):
|
2018-07-25 13:46:39 +00:00
|
|
|
super().__init__()
|
2018-04-06 14:04:25 +00:00
|
|
|
self._event_handler_connector = EventHandlerConnector()
|
2018-06-29 20:03:19 +00:00
|
|
|
self._uplink_connector = ServerUplinkConnector()
|
2018-07-23 15:14:44 +00:00
|
|
|
self._auth_key = KeyManager().auth_key
|
2018-02-23 13:55:58 +00:00
|
|
|
|
2018-06-04 20:16:44 +00:00
|
|
|
self.application = Application([(
|
2018-07-16 09:17:06 +00:00
|
|
|
r'/ws', ZMQWebSocketProxy, {
|
2018-06-28 15:33:20 +00:00
|
|
|
'event_handler_connector': self._event_handler_connector,
|
2018-07-25 13:46:39 +00:00
|
|
|
'message_handlers': [
|
|
|
|
self.handle_trigger,
|
|
|
|
self.handle_recover,
|
|
|
|
self.handle_fsm_update
|
|
|
|
],
|
2018-07-13 13:24:45 +00:00
|
|
|
'frontend_message_handlers': [self.save_frontend_messages]
|
2018-07-16 09:17:06 +00:00
|
|
|
})])
|
2018-04-10 11:00:56 +00:00
|
|
|
|
2018-07-13 13:24:45 +00:00
|
|
|
self._frontend_messages = FrontendMessageStorage()
|
|
|
|
|
2018-04-11 13:22:54 +00:00
|
|
|
def handle_trigger(self, message):
|
2018-06-29 20:03:19 +00:00
|
|
|
if 'trigger' in message:
|
|
|
|
LOG.debug('Executing handler for trigger "%s"', message.get('trigger', ''))
|
2018-07-23 15:14:44 +00:00
|
|
|
fsm_eh_command = {
|
2018-06-29 20:03:19 +00:00
|
|
|
'key': 'fsm',
|
|
|
|
'data': {
|
|
|
|
'command': 'trigger',
|
2018-07-23 15:14:44 +00:00
|
|
|
'value': message['trigger']
|
2018-06-29 20:03:19 +00:00
|
|
|
}
|
2018-07-23 15:14:44 +00:00
|
|
|
}
|
|
|
|
if verify_message(self._auth_key, message):
|
|
|
|
sign_message(self._auth_key, fsm_eh_command)
|
|
|
|
self._uplink_connector.send_to_eventhandler(fsm_eh_command)
|
2018-02-23 11:07:30 +00:00
|
|
|
|
2018-07-13 13:24:45 +00:00
|
|
|
def handle_recover(self, message):
|
|
|
|
if message['key'] == 'recover':
|
|
|
|
self._frontend_messages.replay_messages(self._uplink_connector)
|
2018-07-20 07:20:42 +00:00
|
|
|
self._frontend_messages.clear()
|
2018-07-13 13:24:45 +00:00
|
|
|
|
2018-07-25 13:46:39 +00:00
|
|
|
def handle_fsm_update(self, message):
|
|
|
|
self.update_fsm_data(message)
|
|
|
|
|
2018-07-13 13:24:45 +00:00
|
|
|
def save_frontend_messages(self, message):
|
|
|
|
self._frontend_messages.save_message(message)
|
|
|
|
|
2018-02-23 13:55:58 +00:00
|
|
|
def listen(self, port):
|
|
|
|
self.application.listen(port)
|
2018-07-13 13:24:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MessageStorage(ABC):
|
|
|
|
def __init__(self):
|
|
|
|
self.saved_messages = []
|
|
|
|
|
|
|
|
def save_message(self, message):
|
2018-07-20 18:47:10 +00:00
|
|
|
with suppress(KeyError, AttributeError):
|
|
|
|
if self.filter_message(message):
|
|
|
|
self.saved_messages.extend(self.transform_message(message))
|
2018-07-13 13:24:45 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def filter_message(self, message):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2018-07-16 09:17:06 +00:00
|
|
|
def transform_message(self, message): # pylint: disable=no-self-use
|
2018-07-13 13:24:45 +00:00
|
|
|
yield message
|
|
|
|
|
2018-07-20 07:20:42 +00:00
|
|
|
def clear(self):
|
|
|
|
self.saved_messages.clear()
|
|
|
|
|
2018-07-13 13:24:45 +00:00
|
|
|
|
|
|
|
class FrontendMessageStorage(MessageStorage):
|
|
|
|
def filter_message(self, message):
|
2018-07-20 07:16:06 +00:00
|
|
|
key = message['key']
|
|
|
|
command = message.get('data', {}).get('command')
|
|
|
|
return (
|
|
|
|
key in ('message', 'dashboard', 'queueMessages')
|
|
|
|
or key == 'ide' and command in ('select', 'read')
|
|
|
|
)
|
2018-07-13 13:24:45 +00:00
|
|
|
|
|
|
|
def transform_message(self, message):
|
|
|
|
if message['key'] == 'queueMessages':
|
|
|
|
yield from MessageSender.generate_messages_from_queue(message)
|
|
|
|
else:
|
|
|
|
yield message
|
|
|
|
|
|
|
|
def replay_messages(self, connector):
|
|
|
|
for message in self.saved_messages:
|
|
|
|
connector.send(message)
|