baseimage-tutorial-framework/tfw/components/fsm/fsm_handler.py
2019-08-08 15:05:37 +02:00

41 lines
1.2 KiB
Python

import logging
from tfw.internals.crypto import KeyManager, sign_message
from tfw.internals.networking import Scope
from .fsm_updater import FSMUpdater
LOG = logging.getLogger(__name__)
class FSMHandler:
keys = ['fsm']
def __init__(self, *, fsm_type):
self.fsm = fsm_type()
self._fsm_updater = FSMUpdater(self.fsm)
self.auth_key = KeyManager().auth_key
self.command_handlers = {
'fsm.step' : self.handle_step,
'fsm.announce' : self.handle_announce
}
def handle_event(self, message, connector):
try:
message = self.command_handlers[message['key']](message)
if message:
fsm_update_message = self._fsm_updater.fsm_update
sign_message(self.auth_key, fsm_update_message)
connector.send_message(fsm_update_message, Scope.BROADCAST)
except KeyError:
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
def handle_step(self, message):
if self.fsm.step(message['trigger']):
return message
def handle_announce(self, message):
# pylint: disable=no-self-use
return message