2018-07-25 13:31:03 +00:00
|
|
|
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
|
2019-06-10 13:32:45 +00:00
|
|
|
import logging
|
|
|
|
|
2018-07-25 13:31:03 +00:00
|
|
|
from tfw.crypto import KeyManager, verify_message
|
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class FSMAware:
|
|
|
|
"""
|
|
|
|
Base class for stuff that has to be aware of the framework FSM.
|
|
|
|
This is done by processing 'fsm_update' messages.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self.fsm_state = None
|
|
|
|
self.fsm_in_accepted_state = False
|
2018-07-30 07:25:19 +00:00
|
|
|
self.fsm_event_log = []
|
2018-07-25 13:31:03 +00:00
|
|
|
self._auth_key = KeyManager().auth_key
|
|
|
|
|
2019-06-28 13:16:08 +00:00
|
|
|
def refresh_on_fsm_update(self, message):
|
2018-07-25 13:31:03 +00:00
|
|
|
if message['key'] == 'fsm_update' and verify_message(self._auth_key, message):
|
|
|
|
self._handle_fsm_update(message)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _handle_fsm_update(self, message):
|
|
|
|
try:
|
2018-07-30 07:25:19 +00:00
|
|
|
update_data = message['data']
|
|
|
|
new_state = update_data['current_state']
|
2018-07-25 13:31:03 +00:00
|
|
|
if self.fsm_state != new_state:
|
2018-07-30 07:25:19 +00:00
|
|
|
self.handle_fsm_step(**update_data)
|
2018-07-25 13:31:03 +00:00
|
|
|
self.fsm_state = new_state
|
2018-07-30 07:25:19 +00:00
|
|
|
self.fsm_in_accepted_state = update_data['in_accepted_state']
|
|
|
|
self.fsm_event_log.append(update_data)
|
2018-07-25 13:31:03 +00:00
|
|
|
except KeyError:
|
|
|
|
LOG.error('Invalid fsm_update message received!')
|
|
|
|
|
|
|
|
def handle_fsm_step(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Called in case the TFW FSM has stepped.
|
|
|
|
|
|
|
|
:param kwargs: fsm_update 'data' field
|
|
|
|
"""
|