2019-06-10 13:32:45 +00:00
|
|
|
import logging
|
|
|
|
|
2018-07-23 15:16:37 +00:00
|
|
|
from tfw.crypto import KeyManager, sign_message, verify_message
|
2019-05-27 12:09:13 +00:00
|
|
|
from tfw.networking import Scope
|
2018-06-29 20:03:19 +00:00
|
|
|
|
2019-06-28 13:11:02 +00:00
|
|
|
from .event_handler import EventHandler
|
|
|
|
|
2018-06-29 20:03:19 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-06-28 13:11:02 +00:00
|
|
|
class FSMManagingEventHandler(EventHandler):
|
2018-08-04 21:27:18 +00:00
|
|
|
"""
|
|
|
|
EventHandler responsible for managing the state machine of
|
|
|
|
the framework (TFW FSM).
|
|
|
|
|
|
|
|
tfw.networking.TFWServer instances automatically send 'trigger'
|
|
|
|
commands to the event handler listening on the 'fsm' key,
|
|
|
|
which should be an instance of this event handler.
|
|
|
|
|
|
|
|
This event handler accepts messages that have a
|
|
|
|
data['command'] key specifying a command to be executed.
|
|
|
|
|
|
|
|
An 'fsm_update' message is broadcasted after every successful
|
|
|
|
command.
|
|
|
|
"""
|
2018-07-23 15:16:37 +00:00
|
|
|
def __init__(self, key, fsm_type, require_signature=False):
|
2019-06-28 13:11:02 +00:00
|
|
|
super().__init__(key, scope=Scope.WEBSOCKET)
|
2018-06-29 20:03:19 +00:00
|
|
|
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:
|
2018-07-24 15:16:57 +00:00
|
|
|
fsm_update_message = self._fsm_updater.fsm_update
|
2018-07-23 15:16:37 +00:00
|
|
|
sign_message(self.auth_key, message)
|
|
|
|
sign_message(self.auth_key, fsm_update_message)
|
2019-05-26 16:26:33 +00:00
|
|
|
self.server_connector.send_message(fsm_update_message, Scope.BROADCAST)
|
2019-07-03 13:49:20 +00:00
|
|
|
self.send_message(message)
|
2018-06-29 20:03:19 +00:00
|
|
|
except KeyError:
|
|
|
|
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
|
|
|
|
2018-07-23 15:16:37 +00:00
|
|
|
def handle_trigger(self, message):
|
2018-08-04 21:27:18 +00:00
|
|
|
"""
|
|
|
|
Attempts to step the FSM with the supplied trigger.
|
|
|
|
|
|
|
|
:param message: TFW message with a data field containing
|
|
|
|
the action to try triggering in data['value']
|
|
|
|
"""
|
2018-07-23 15:16:37 +00:00
|
|
|
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-08-04 21:27:18 +00:00
|
|
|
"""
|
|
|
|
Does nothing, but triggers an 'fsm_update' 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-24 15:16:57 +00:00
|
|
|
@property
|
|
|
|
def fsm_update(self):
|
2018-07-12 13:20:35 +00:00
|
|
|
return {
|
|
|
|
'key': 'fsm_update',
|
2018-07-24 15:16:57 +00:00
|
|
|
'data': self.fsm_update_data
|
2018-07-12 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 15:16:57 +00:00
|
|
|
@property
|
|
|
|
def fsm_update_data(self):
|
2018-06-29 20:03:19 +00:00
|
|
|
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-24 15:16:57 +00:00
|
|
|
last_fsm_event = self.fsm.event_log[-1]
|
|
|
|
last_fsm_event['timestamp'] = last_fsm_event['timestamp'].isoformat()
|
2018-06-29 20:03:19 +00:00
|
|
|
return {
|
2018-07-24 15:16:57 +00:00
|
|
|
'current_state': self.fsm.state,
|
2018-07-14 18:45:26 +00:00
|
|
|
'valid_transitions': valid_transitions,
|
2018-07-24 15:16:57 +00:00
|
|
|
'in_accepted_state': self.fsm.in_accepted_state,
|
|
|
|
'last_event': last_fsm_event
|
2018-06-29 20:03:19 +00:00
|
|
|
}
|