2018-02-11 11:32:30 +00:00
|
|
|
from tornado.web import Application
|
2018-02-26 14:00:17 +00:00
|
|
|
from collections import defaultdict
|
2018-02-11 11:32:30 +00:00
|
|
|
|
2018-02-12 12:22:55 +00:00
|
|
|
from tfw.networking.server.controller_responder import ControllerResponder
|
2018-02-23 11:07:30 +00:00
|
|
|
from tfw.networking.server.zmq_websocket_handler import ZMQWebSocketProxy
|
|
|
|
from tfw.networking.event_handlers.server_connector import ServerUplinkConnector
|
|
|
|
from tfw.config.logs import logging
|
|
|
|
log = logging.getLogger(__name__)
|
2018-02-11 11:32:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TFWServer:
|
|
|
|
def __init__(self, fsm_type):
|
|
|
|
self._fsm = fsm_type()
|
2018-02-23 13:55:58 +00:00
|
|
|
self.fsm_updater = FSMUpdater(self._fsm)
|
|
|
|
self._fsm.subscribe(self.fsm_updater.update)
|
|
|
|
|
2018-02-11 11:32:30 +00:00
|
|
|
self.application = Application(
|
2018-02-23 11:07:30 +00:00
|
|
|
[(r'/ws', ZMQWebSocketProxy, {'make_response': self.make_response,
|
|
|
|
'proxy_filter': self.proxy_filter})]
|
2018-02-11 11:32:30 +00:00
|
|
|
)
|
2018-02-12 12:22:55 +00:00
|
|
|
self.controller_responder = ControllerResponder(self.fsm)
|
2018-02-11 11:32:30 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def fsm(self):
|
|
|
|
return self._fsm
|
|
|
|
|
2018-02-23 11:07:30 +00:00
|
|
|
def make_response(self, message):
|
2018-02-23 14:14:23 +00:00
|
|
|
trigger = message.get('trigger', '')
|
|
|
|
try: self.fsm.trigger(trigger, message=message)
|
|
|
|
except AttributeError: log.debug('FSM failed to execute nonexistent trigger: "{}"'.format(trigger))
|
2018-02-23 11:07:30 +00:00
|
|
|
return message
|
|
|
|
|
|
|
|
def proxy_filter(self, message):
|
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:00:17 +00:00
|
|
|
self.trigger_handlers = defaultdict(list)
|
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 13:26:14 +00:00
|
|
|
if all((handler() for handler in self.trigger_handlers[trigger])):
|
2018-02-26 14:01:26 +00:00
|
|
|
try: self.fsm.trigger(trigger, message=message)
|
2018-02-26 13:26:14 +00:00
|
|
|
except AttributeError: log.debug('FSM failed to execute nonexistent trigger: "{}"'.format(trigger))
|
|
|
|
|
|
|
|
def subscribe_handler(self, trigger, handler):
|
|
|
|
self.trigger_handlers[trigger].append(handler)
|
|
|
|
|
|
|
|
def unsubscribe_handler(self, trigger, handler):
|
|
|
|
self.trigger_handlers[trigger].append(handler)
|
|
|
|
|
|
|
|
|
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-02-23 11:13:19 +00:00
|
|
|
self.uplink.send(*self.generate_fsm_update())
|
2018-02-23 11:07:30 +00:00
|
|
|
|
|
|
|
def generate_fsm_update(self):
|
|
|
|
key = 'FSMUpdate'
|
|
|
|
response = {'key': key,
|
|
|
|
'data': {'current_state': self.fsm.state,
|
|
|
|
'valid_transitions':
|
2018-02-23 11:08:14 +00:00
|
|
|
[{'trigger': trigger} for trigger in self.fsm.machine.get_triggers(self.fsm.state)]}}
|
2018-02-23 11:13:19 +00:00
|
|
|
return key, response
|