Trigger first step in an individual event handler

This commit is contained in:
R. Richard 2019-08-27 13:15:59 +02:00 committed by therealkrispet
parent 1f813a2138
commit ddb47d696c
3 changed files with 41 additions and 11 deletions

View File

@ -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

View File

@ -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

View File

@ -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