From ddb47d696cfca5b179b079fec9361a621eb05c9b Mon Sep 17 00:00:00 2001 From: "R. Richard" Date: Tue, 27 Aug 2019 13:15:59 +0200 Subject: [PATCH] Trigger first step in an individual event handler --- tfw/components/frontend/__init__.py | 1 + .../frontend/frontend_ready_handler.py | 39 +++++++++++++++++++ tfw/components/fsm/fsm_handler.py | 12 +----- 3 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 tfw/components/frontend/frontend_ready_handler.py diff --git a/tfw/components/frontend/__init__.py b/tfw/components/frontend/__init__.py index 3dab104..a0bf733 100644 --- a/tfw/components/frontend/__init__.py +++ b/tfw/components/frontend/__init__.py @@ -1,4 +1,5 @@ from .console_logs_handler import ConsoleLogsHandler from .frontend_proxy_handler import FrontendProxyHandler +from .frontend_ready_handler import FrontendReadyHandler from .message_queue_handler import MessageQueueHandler from .message_sender import MessageSender diff --git a/tfw/components/frontend/frontend_ready_handler.py b/tfw/components/frontend/frontend_ready_handler.py new file mode 100644 index 0000000..295d257 --- /dev/null +++ b/tfw/components/frontend/frontend_ready_handler.py @@ -0,0 +1,39 @@ +import logging + +from tfw.internals.crypto import KeyManager, sign_message + +LOG = logging.getLogger(__name__) + + +class FrontendReadyHandler: + keys = ['frontend.ready', 'fsm.update'] + + def __init__(self, initial_trigger): + self.connector = None + self._auth_key = KeyManager().auth_key + self.initial_trigger = initial_trigger + + self.commands = { + 'frontend.ready': self.handle_ready, + 'fsm.update': self.handle_update + } + + def handle_event(self, message, _): + try: + self.commands[message['key']]() + except KeyError: + LOG.error('IGNORING MESSAGE: Invalid message received: %s', message) + + def handle_ready(self): + trigger = { + 'key': 'fsm.step', + 'trigger': self.initial_trigger + } + sign_message(self._auth_key, trigger) + self.connector.send_message(trigger) + + def handle_update(self): + self.stop() + + def stop(self): + pass diff --git a/tfw/components/fsm/fsm_handler.py b/tfw/components/fsm/fsm_handler.py index ae9a171..824b69b 100644 --- a/tfw/components/fsm/fsm_handler.py +++ b/tfw/components/fsm/fsm_handler.py @@ -11,22 +11,17 @@ LOG = logging.getLogger(__name__) class FSMHandler: keys = ['fsm'] - def __init__(self, *, fsm_type, initial_trigger): + def __init__(self, *, fsm_type): self.connector = None 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.update' : self.handle_update } - def start(self): - self.connector.subscribe('frontend.ready') - def handle_event(self, message, _): try: message = self.command_handlers[message['key']](message) @@ -37,11 +32,6 @@ class FSMHandler: 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): # pylint: disable=inconsistent-return-statements if self.fsm.step(message['trigger']): return message