mirror of
				https://github.com/avatao-content/baseimage-tutorial-framework
				synced 2025-11-04 03:32:54 +00:00 
			
		
		
		
	Update FSM related classes according to the new API
This commit is contained in:
		@@ -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
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user