mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-15 06:27:18 +00:00
51 lines
1.5 KiB
Python
51 lines
1.5 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, initial_trigger):
|
|
self.fsm = fsm_type()
|
|
self._fsm_updater = FSMUpdater(self.fsm)
|
|
self.auth_key = KeyManager().auth_key
|
|
self.initial_trigger = initial_trigger
|
|
|
|
self.command_handlers = {
|
|
'frontend.ready': self.handle_ready,
|
|
'fsm.step' : self.handle_step,
|
|
'fsm.announce' : self.handle_announce
|
|
}
|
|
|
|
def start(self):
|
|
self.connector.subscribe('frontend.ready')
|
|
|
|
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_ready(self, message):
|
|
self.fsm.step(self.initial_trigger)
|
|
self.connector.unsubscribe('frontend.ready')
|
|
return 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
|