baseimage-tutorial-framework/tfw/components/fsm/fsm_handler.py

43 lines
1.3 KiB
Python
Raw Permalink Normal View History

2019-06-10 13:32:45 +00:00
import logging
from tfw.internals.crypto import KeyManager, sign_message
from tfw.internals.networking import Scope, Intent
from .fsm_updater import FSMUpdater
LOG = logging.getLogger(__name__)
class FSMHandler:
2019-08-08 13:05:37 +00:00
keys = ['fsm']
type_id = 'ControlEventHandler'
2019-08-08 13:05:37 +00:00
def __init__(self, *, fsm_type):
self.connector = None
self.fsm = fsm_type()
self._fsm_updater = FSMUpdater(self.fsm)
self.auth_key = KeyManager().auth_key
self.command_handlers = {
2019-08-28 14:34:55 +00:00
'fsm.trigger': self.handle_trigger,
2019-08-28 14:31:43 +00:00
'fsm.update' : self.handle_update
}
2019-08-26 09:10:39 +00:00
def handle_event(self, message, _):
try:
message = self.command_handlers[message['key']](message)
if message:
2018-07-24 15:16:57 +00:00
fsm_update_message = self._fsm_updater.fsm_update
sign_message(self.auth_key, fsm_update_message)
2019-08-26 09:10:39 +00:00
self.connector.send_message(fsm_update_message, Scope.BROADCAST, Intent.EVENT)
except KeyError:
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
2019-08-28 14:31:43 +00:00
def handle_trigger(self, message): # pylint: disable=inconsistent-return-statements
2019-08-28 14:46:05 +00:00
if self.fsm.step(message['transition']):
2019-08-08 06:27:41 +00:00
return message
def handle_update(self, message):
# pylint: disable=no-self-use
return message