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-02-26 14:00:17 +00:00
|
|
|
from collections import defaultdict
|
2018-02-11 11:32:30 +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 import MessageSender
|
|
|
|
from tfw.networking.event_handlers import ServerUplinkConnector
|
2018-04-06 14:04:25 +00:00
|
|
|
from tfw.networking.server import EventHandlerConnector
|
2018-02-23 11:07:30 +00:00
|
|
|
from tfw.config.logs import logging
|
2018-04-06 13:21:45 +00:00
|
|
|
from .zmq_websocket_handler import ZMQWebSocketProxy
|
2018-03-25 14:06:59 +00:00
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
2018-02-11 11:32:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TFWServer:
|
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
|
|
|
|
SUB socket. It also manages an FSM you can define as a constructor argument.
|
|
|
|
"""
|
2018-02-11 11:32:30 +00:00
|
|
|
def __init__(self, fsm_type):
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
:param fsm_type: the type of FSM you want TFW to use
|
|
|
|
"""
|
2018-02-11 11:32:30 +00:00
|
|
|
self._fsm = fsm_type()
|
2018-02-27 12:39:31 +00:00
|
|
|
self._fsm_updater = FSMUpdater(self._fsm)
|
|
|
|
self._fsm_manager = FSMManager(self._fsm)
|
2018-03-07 09:12:58 +00:00
|
|
|
self._fsm.subscribe_callback(self._fsm_updater.update)
|
2018-04-06 14:04:25 +00:00
|
|
|
self._event_handler_connector = EventHandlerConnector()
|
2018-02-23 13:55:58 +00:00
|
|
|
|
2018-02-11 11:32:30 +00:00
|
|
|
self.application = Application(
|
2018-04-16 10:57:08 +00:00
|
|
|
[(r'/ws', ZMQWebSocketProxy, {'make_eventhandler_message': self.make_eventhandler_message,
|
2018-04-06 14:04:25 +00:00
|
|
|
'proxy_filter': self.proxy_filter,
|
2018-04-11 13:22:54 +00:00
|
|
|
'handle_trigger': self.handle_trigger,
|
2018-04-06 14:04:25 +00:00
|
|
|
'event_handler_connector': self._event_handler_connector})]
|
2018-02-11 11:32:30 +00:00
|
|
|
)
|
2018-03-23 10:32:11 +00:00
|
|
|
#self.controller_responder = ControllerResponder(self.fsm) TODO: add this once controller stuff is resolved
|
2018-02-11 11:32:30 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def fsm(self):
|
|
|
|
return self._fsm
|
|
|
|
|
2018-02-27 12:39:31 +00:00
|
|
|
@property
|
|
|
|
def fsm_manager(self):
|
|
|
|
return self._fsm_manager
|
|
|
|
|
2018-04-16 10:57:08 +00:00
|
|
|
def make_eventhandler_message(self, message):
|
2018-04-10 11:00:56 +00:00
|
|
|
self.trigger_fsm(message)
|
|
|
|
message['FSMUpdate'] = self._fsm_updater.get_fsm_state_and_transitions()
|
|
|
|
return message
|
|
|
|
|
2018-04-11 13:22:54 +00:00
|
|
|
def handle_trigger(self, message):
|
|
|
|
LOG.debug('Executing handler for trigger "%s"', message.get('trigger', ''))
|
|
|
|
self.trigger_fsm(message)
|
|
|
|
|
2018-04-10 11:00:56 +00:00
|
|
|
def trigger_fsm(self, message):
|
2018-02-23 14:14:23 +00:00
|
|
|
trigger = message.get('trigger', '')
|
2018-03-30 15:50:20 +00:00
|
|
|
try:
|
|
|
|
self._fsm_manager.trigger(trigger, message)
|
|
|
|
except AttributeError:
|
|
|
|
LOG.debug('FSM failed to execute nonexistent trigger: "%s"', trigger)
|
2018-02-23 11:07:30 +00:00
|
|
|
|
|
|
|
def proxy_filter(self, message):
|
2018-03-30 16:11:38 +00:00
|
|
|
# pylint: disable=unused-argument,no-self-use
|
2018-02-23 14:14:23 +00:00
|
|
|
return True
|
2018-02-23 11:07:30 +00:00
|
|
|
|
2018-02-23 13:55:58 +00:00
|
|
|
def listen(self, port):
|
|
|
|
self.application.listen(port)
|
|
|
|
|
|
|
|
|
2018-02-26 13:26:14 +00:00
|
|
|
class FSMManager:
|
2018-02-26 14:07:37 +00:00
|
|
|
def __init__(self, fsm):
|
|
|
|
self._fsm = fsm
|
2018-02-26 14:54:10 +00:00
|
|
|
self.trigger_predicates = defaultdict(list)
|
2018-02-26 16:47:35 +00:00
|
|
|
self.messenge_sender = MessageSender()
|
2018-02-26 13:26:14 +00:00
|
|
|
|
2018-02-26 14:02:06 +00:00
|
|
|
@property
|
|
|
|
def fsm(self):
|
|
|
|
return self._fsm
|
|
|
|
|
2018-02-26 14:01:26 +00:00
|
|
|
def trigger(self, trigger, message):
|
2018-02-26 16:47:35 +00:00
|
|
|
predicate_results = []
|
|
|
|
for predicate in self.trigger_predicates[trigger]:
|
2018-04-06 15:14:06 +00:00
|
|
|
success, message = predicate(message)
|
2018-02-26 16:47:35 +00:00
|
|
|
predicate_results.append(success)
|
|
|
|
self.messenge_sender.send('FSM', message)
|
|
|
|
|
|
|
|
if all(predicate_results):
|
2018-03-30 15:50:20 +00:00
|
|
|
try:
|
|
|
|
self.fsm.trigger(trigger, message=message)
|
|
|
|
except AttributeError:
|
|
|
|
LOG.debug('FSM failed to execute nonexistent trigger: "%s"', trigger)
|
2018-02-26 13:26:14 +00:00
|
|
|
|
2018-02-26 16:52:34 +00:00
|
|
|
def subscribe_predicate(self, trigger, *predicates):
|
|
|
|
self.trigger_predicates[trigger].extend(predicates)
|
2018-02-26 13:26:14 +00:00
|
|
|
|
2018-02-26 16:52:34 +00:00
|
|
|
def unsubscribe_predicate(self, trigger, *predicates):
|
|
|
|
self.trigger_predicates[trigger] = [predicate for predicate in self.trigger_predicates[trigger]
|
|
|
|
not in predicates]
|
2018-02-26 13:26:14 +00:00
|
|
|
|
|
|
|
|
2018-02-23 13:55:58 +00:00
|
|
|
class FSMUpdater:
|
|
|
|
def __init__(self, fsm):
|
|
|
|
self.fsm = fsm
|
|
|
|
self.uplink = ServerUplinkConnector()
|
|
|
|
|
|
|
|
def update(self, kwargs_dict):
|
2018-03-30 16:11:38 +00:00
|
|
|
# pylint: disable=unused-argument
|
2018-04-12 13:11:52 +00:00
|
|
|
self.uplink.send(self.generate_fsm_update())
|
2018-02-23 11:07:30 +00:00
|
|
|
|
|
|
|
def generate_fsm_update(self):
|
2018-04-12 13:11:52 +00:00
|
|
|
return {'key': 'FSMUpdate',
|
|
|
|
'data': self.get_fsm_state_and_transitions()}
|
2018-04-06 15:09:35 +00:00
|
|
|
|
|
|
|
def get_fsm_state_and_transitions(self):
|
|
|
|
state = self.fsm.state
|
|
|
|
valid_transitions = [{'trigger': trigger} for trigger in self.fsm.machine.get_triggers(self.fsm.state)]
|
|
|
|
return {'current_state': state, 'valid_transitions': valid_transitions}
|