2018-06-29 20:03:19 +00:00
|
|
|
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
|
2018-07-12 13:20:35 +00:00
|
|
|
from tfw import EventHandlerBase
|
2018-07-23 15:16:37 +00:00
|
|
|
from tfw.crypto import KeyManager, sign_message, verify_message
|
2018-06-29 20:03:19 +00:00
|
|
|
from tfw.config.logs import logging
|
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-07-12 13:20:35 +00:00
|
|
|
class FSMManagingEventHandler(EventHandlerBase):
|
2018-07-23 15:16:37 +00:00
|
|
|
def __init__(self, key, fsm_type, require_signature=False):
|
2018-06-29 20:03:19 +00:00
|
|
|
super().__init__(key)
|
|
|
|
self.fsm = fsm_type()
|
|
|
|
self._fsm_updater = FSMUpdater(self.fsm)
|
2018-07-16 12:31:52 +00:00
|
|
|
self.auth_key = KeyManager().auth_key
|
2018-07-23 15:16:37 +00:00
|
|
|
self._require_signature = require_signature
|
2018-06-29 20:03:19 +00:00
|
|
|
|
|
|
|
self.command_handlers = {
|
|
|
|
'trigger': self.handle_trigger,
|
|
|
|
'update': self.handle_update
|
|
|
|
}
|
|
|
|
|
|
|
|
def handle_event(self, message):
|
|
|
|
try:
|
2018-07-23 15:16:37 +00:00
|
|
|
message = self.command_handlers[message['data']['command']](message)
|
|
|
|
if message:
|
|
|
|
fsm_update_message = self._fsm_updater.generate_fsm_update()
|
|
|
|
sign_message(self.auth_key, message)
|
|
|
|
sign_message(self.auth_key, fsm_update_message)
|
|
|
|
self.server_connector.broadcast(fsm_update_message)
|
2018-06-29 20:03:19 +00:00
|
|
|
return message
|
|
|
|
except KeyError:
|
|
|
|
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
|
|
|
|
2018-07-23 15:16:37 +00:00
|
|
|
def handle_trigger(self, message):
|
|
|
|
trigger = message['data']['value']
|
|
|
|
if self._require_signature:
|
|
|
|
if not verify_message(self.auth_key, message):
|
|
|
|
LOG.error('Ignoring unsigned trigger command: %s', message)
|
|
|
|
return None
|
|
|
|
if self.fsm.step(trigger):
|
|
|
|
return message
|
|
|
|
return None
|
2018-06-29 20:03:19 +00:00
|
|
|
|
2018-07-23 15:16:37 +00:00
|
|
|
def handle_update(self, message):
|
2018-07-16 09:17:06 +00:00
|
|
|
# pylint: disable=no-self-use
|
2018-07-23 15:16:37 +00:00
|
|
|
return message
|
2018-06-29 20:03:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FSMUpdater:
|
|
|
|
def __init__(self, fsm):
|
|
|
|
self.fsm = fsm
|
|
|
|
|
2018-07-12 13:20:35 +00:00
|
|
|
def generate_fsm_update(self):
|
|
|
|
return {
|
|
|
|
'key': 'fsm_update',
|
|
|
|
'data': self.get_fsm_state_and_transitions()
|
|
|
|
}
|
|
|
|
|
2018-06-29 20:03:19 +00:00
|
|
|
def get_fsm_state_and_transitions(self):
|
|
|
|
state = self.fsm.state
|
|
|
|
valid_transitions = [
|
|
|
|
{'trigger': trigger}
|
2018-07-04 13:48:16 +00:00
|
|
|
for trigger in self.fsm.get_triggers(self.fsm.state)
|
2018-06-29 20:03:19 +00:00
|
|
|
]
|
2018-07-14 18:45:26 +00:00
|
|
|
last_trigger = self.fsm.trigger_history[-1] if self.fsm.trigger_history else None
|
2018-07-20 12:39:23 +00:00
|
|
|
in_accepted_state = state in self.fsm.accepted_states
|
2018-06-29 20:03:19 +00:00
|
|
|
return {
|
|
|
|
'current_state': state,
|
2018-07-14 18:45:26 +00:00
|
|
|
'valid_transitions': valid_transitions,
|
2018-07-20 12:39:23 +00:00
|
|
|
'last_trigger': last_trigger,
|
|
|
|
'in_accepted_state': in_accepted_state
|
2018-06-29 20:03:19 +00:00
|
|
|
}
|