baseimage-tutorial-framework/lib/tfw/networking/server/tfw_server.py

89 lines
3.0 KiB
Python
Raw Normal View History

from tornado.web import Application
from collections import defaultdict
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
2018-02-26 16:47:35 +00:00
from tfw.message_sender import MessageSender
2018-02-23 11:07:30 +00:00
from tfw.config.logs import logging
log = logging.getLogger(__name__)
class TFWServer:
def __init__(self, fsm_type):
self._fsm = fsm_type()
self._fsm_updater = FSMUpdater(self._fsm)
self._fsm_manager = FSMManager(self._fsm)
self._fsm.subscribe(self._fsm_updater.update)
self.application = Application(
2018-02-23 11:07:30 +00:00
[(r'/ws', ZMQWebSocketProxy, {'make_response': self.make_response,
'proxy_filter': self.proxy_filter})]
)
self.controller_responder = ControllerResponder(self.fsm)
@property
def fsm(self):
return self._fsm
@property
def fsm_manager(self):
return self._fsm_manager
2018-02-23 11:07:30 +00:00
def make_response(self, message):
trigger = message.get('trigger', '')
try: self._fsm_manager.trigger(trigger, 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):
return True
2018-02-23 11:07:30 +00:00
def listen(self, port):
self.application.listen(port)
2018-02-26 13:26:14 +00:00
class FSMManager:
def __init__(self, fsm):
self._fsm = fsm
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
def trigger(self, trigger, message):
2018-02-26 16:47:35 +00:00
predicate_results = []
for predicate in self.trigger_predicates[trigger]:
success, message = predicate()
predicate_results.append(success)
self.messenge_sender.send('FSM', message)
if all(predicate_results):
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_predicate(self, trigger, *predicates):
self.trigger_predicates[trigger].extend(predicates)
2018-02-26 13:26:14 +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
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 = {'data': {'current_state': self.fsm.state,
2018-02-23 11:07:30 +00:00
'valid_transitions':
[{'trigger': trigger} for trigger in self.fsm.machine.get_triggers(self.fsm.state)]}}
2018-02-23 11:13:19 +00:00
return key, response