mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-25 13:21:32 +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
|
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 tfw.internals.networking import Scope
|
||||||
|
|
||||||
from .fsm_updater import FSMUpdater
|
from .fsm_updater import FSMUpdater
|
||||||
@ -9,57 +9,30 @@ LOG = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class FSMHandler:
|
class FSMHandler:
|
||||||
keys = ['fsm']
|
keys = ['fsm.step', 'fsm.update']
|
||||||
"""
|
|
||||||
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.
|
|
||||||
"""
|
|
||||||
def __init__(self, *, fsm_type):
|
def __init__(self, *, fsm_type):
|
||||||
self.fsm = fsm_type()
|
self.fsm = fsm_type()
|
||||||
self._fsm_updater = FSMUpdater(self.fsm)
|
self._fsm_updater = FSMUpdater(self.fsm)
|
||||||
self.auth_key = KeyManager().auth_key
|
self.auth_key = KeyManager().auth_key
|
||||||
|
|
||||||
self.command_handlers = {
|
self.command_handlers = {
|
||||||
'trigger': self.handle_trigger,
|
'fsm.step' : self.handle_step,
|
||||||
'update': self.handle_update
|
'fsm.update' : self.handle_update
|
||||||
}
|
}
|
||||||
|
|
||||||
def handle_event(self, message, connector):
|
def handle_event(self, message, connector):
|
||||||
try:
|
try:
|
||||||
message = self.command_handlers[message['data']['command']](message)
|
message = self.command_handlers[message['key']](message)
|
||||||
if message:
|
if message:
|
||||||
fsm_update_message = self._fsm_updater.fsm_update
|
fsm_update_message = self._fsm_updater.fsm_update
|
||||||
sign_message(self.auth_key, message)
|
|
||||||
sign_message(self.auth_key, fsm_update_message)
|
sign_message(self.auth_key, fsm_update_message)
|
||||||
connector.send_message(fsm_update_message, Scope.BROADCAST)
|
connector.send_message(fsm_update_message, Scope.BROADCAST)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
||||||
|
|
||||||
def handle_trigger(self, message):
|
def handle_step(self, message):
|
||||||
"""
|
return message if self.fsm.step(message['trigger']) else None
|
||||||
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_update(self, message):
|
def handle_update(self, message):
|
||||||
"""
|
|
||||||
Does nothing, but triggers an 'fsm_update' message.
|
|
||||||
"""
|
|
||||||
# pylint: disable=no-self-use
|
# pylint: disable=no-self-use
|
||||||
return message
|
return message
|
||||||
|
@ -5,7 +5,7 @@ class FSMUpdater:
|
|||||||
@property
|
@property
|
||||||
def fsm_update(self):
|
def fsm_update(self):
|
||||||
return {
|
return {
|
||||||
'key': 'fsm_update',
|
'key': 'fsm.announce',
|
||||||
**self.fsm_update_data
|
**self.fsm_update_data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ LOG = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class FSMAware:
|
class FSMAware:
|
||||||
keys = ['fsm_update']
|
keys = ['fsm.announce']
|
||||||
"""
|
"""
|
||||||
Base class for stuff that has to be aware of the framework FSM.
|
Base class for stuff that has to be aware of the framework FSM.
|
||||||
This is done by processing 'fsm_update' messages.
|
This is done by processing 'fsm_update' messages.
|
||||||
@ -18,7 +18,7 @@ class FSMAware:
|
|||||||
self._auth_key = KeyManager().auth_key
|
self._auth_key = KeyManager().auth_key
|
||||||
|
|
||||||
def process_message(self, message):
|
def process_message(self, message):
|
||||||
if message['key'] == 'fsm_update':
|
if message['key'] == 'fsm.announce':
|
||||||
if verify_message(self._auth_key, message):
|
if verify_message(self._auth_key, message):
|
||||||
self._handle_fsm_update(message)
|
self._handle_fsm_update(message)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user