mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-25 09:01:33 +00:00
Update FSM related classes according to the new API
This commit is contained in:
parent
d31a850a4e
commit
dded7fd65c
@ -1,6 +1,6 @@
|
||||
import logging
|
||||
|
||||
from tfw.internals.crypto import KeyManager, sign_message, verify_message
|
||||
from tfw.internals.crypto import KeyManager, sign_message
|
||||
from tfw.internals.networking import Scope
|
||||
|
||||
from .fsm_updater import FSMUpdater
|
||||
@ -9,57 +9,30 @@ LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class FSMHandler:
|
||||
keys = ['fsm']
|
||||
"""
|
||||
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.
|
||||
"""
|
||||
keys = ['fsm.step', 'fsm.update']
|
||||
def __init__(self, *, fsm_type):
|
||||
self.fsm = fsm_type()
|
||||
self._fsm_updater = FSMUpdater(self.fsm)
|
||||
self.auth_key = KeyManager().auth_key
|
||||
|
||||
self.command_handlers = {
|
||||
'trigger': self.handle_trigger,
|
||||
'update': self.handle_update
|
||||
'fsm.step' : self.handle_step,
|
||||
'fsm.update' : self.handle_update
|
||||
}
|
||||
|
||||
def handle_event(self, message, connector):
|
||||
try:
|
||||
message = self.command_handlers[message['data']['command']](message)
|
||||
message = self.command_handlers[message['key']](message)
|
||||
if message:
|
||||
fsm_update_message = self._fsm_updater.fsm_update
|
||||
sign_message(self.auth_key, message)
|
||||
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_trigger(self, message):
|
||||
"""
|
||||
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']
|
||||
"""
|
||||
trigger = message['data']['value']
|
||||
if self.fsm.step(trigger):
|
||||
return message
|
||||
return None
|
||||
def handle_step(self, message):
|
||||
return message if self.fsm.step(message['trigger']) else None
|
||||
|
||||
def handle_update(self, message):
|
||||
"""
|
||||
Does nothing, but triggers an 'fsm_update' message.
|
||||
"""
|
||||
# pylint: disable=no-self-use
|
||||
return message
|
||||
|
@ -5,7 +5,7 @@ class FSMUpdater:
|
||||
@property
|
||||
def fsm_update(self):
|
||||
return {
|
||||
'key': 'fsm_update',
|
||||
'key': 'fsm.announce',
|
||||
**self.fsm_update_data
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@ LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class FSMAware:
|
||||
keys = ['fsm_update']
|
||||
keys = ['fsm.announce']
|
||||
"""
|
||||
Base class for stuff that has to be aware of the framework FSM.
|
||||
This is done by processing 'fsm_update' messages.
|
||||
@ -18,7 +18,7 @@ class FSMAware:
|
||||
self._auth_key = KeyManager().auth_key
|
||||
|
||||
def process_message(self, message):
|
||||
if message['key'] == 'fsm_update':
|
||||
if message['key'] == 'fsm.announce':
|
||||
if verify_message(self._auth_key, message):
|
||||
self._handle_fsm_update(message)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user