From 5715c57ebcebd7b998cd9ccfc723d8c164035c60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 25 Jul 2018 15:31:03 +0200 Subject: [PATCH] Move FSMAware to a separate file in networking --- lib/tfw/event_handler_base.py | 34 ++----------------------- lib/tfw/networking/__init__.py | 1 + lib/tfw/networking/fsm_aware.py | 45 +++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 lib/tfw/networking/fsm_aware.py diff --git a/lib/tfw/event_handler_base.py b/lib/tfw/event_handler_base.py index 4c7ad06..15d46bc 100644 --- a/lib/tfw/event_handler_base.py +++ b/lib/tfw/event_handler_base.py @@ -3,8 +3,9 @@ from abc import ABC, abstractmethod +from tfw.networking import FSMAware from tfw.networking.event_handlers import ServerConnector -from tfw.crypto import message_checksum, KeyManager, verify_message +from tfw.crypto import message_checksum from tfw.config.logs import logging LOG = logging.getLogger(__name__) @@ -106,37 +107,6 @@ class EventHandlerBase(ABC): pass -class FSMAware: - def __init__(self): - self.fsm_state = None - self.fsm_in_accepted_state = False - self._auth_key = KeyManager().auth_key - - def update_fsm_data(self, message): - if message['key'] == 'fsm_update' and verify_message(self._auth_key, message): - self._handle_fsm_update(message) - return True - return False - - def _handle_fsm_update(self, message): - try: - new_state = message['data']['current_state'] - if self.fsm_state != new_state: - self.handle_fsm_step(**(message['data'])) - self.fsm_state = new_state - self.fsm_in_accepted_state = message['data']['in_accepted_state'] - except KeyError: - LOG.error('Invalid fsm_update message received!') - - def handle_fsm_step(self, **kwargs): - """ - Called in case the TFW FSM has stepped. - - :param kwargs: fsm_update 'data' field - """ - pass - - class FSMAwareEventHandler(EventHandlerBase, FSMAware, ABC): # pylint: disable=abstract-method """ diff --git a/lib/tfw/networking/__init__.py b/lib/tfw/networking/__init__.py index c1f3eb4..0f038b0 100644 --- a/lib/tfw/networking/__init__.py +++ b/lib/tfw/networking/__init__.py @@ -7,3 +7,4 @@ from .zmq_connector_base import ZMQConnectorBase from .message_sender import MessageSender from .event_handlers.server_connector import ServerUplinkConnector as TFWServerConnector from .server.tfw_server import TFWServer +from .fsm_aware import FSMAware diff --git a/lib/tfw/networking/fsm_aware.py b/lib/tfw/networking/fsm_aware.py new file mode 100644 index 0000000..e666d8c --- /dev/null +++ b/lib/tfw/networking/fsm_aware.py @@ -0,0 +1,45 @@ +# Copyright (C) 2018 Avatao.com Innovative Learning Kft. +# All Rights Reserved. See LICENSE file for details. + +from tfw.crypto import KeyManager, verify_message + +from tfw.config.logs import logging + +LOG = logging.getLogger(__name__) + + +class FSMAware: + """ + Base class for stuff that has to be aware of the framework FSM. + This is done by processing 'fsm_update' messages. + """ + def __init__(self): + self.fsm_state = None + self.fsm_in_accepted_state = False + self.fsm_last_update = None + self._auth_key = KeyManager().auth_key + + def update_fsm_data(self, message): + if message['key'] == 'fsm_update' and verify_message(self._auth_key, message): + self._handle_fsm_update(message) + return True + return False + + def _handle_fsm_update(self, message): + try: + new_state = message['data']['current_state'] + if self.fsm_state != new_state: + self.handle_fsm_step(**(message['data'])) + self.fsm_state = new_state + self.fsm_in_accepted_state = message['data']['in_accepted_state'] + self.fsm_last_update = message['data'] + except KeyError: + LOG.error('Invalid fsm_update message received!') + + def handle_fsm_step(self, **kwargs): + """ + Called in case the TFW FSM has stepped. + + :param kwargs: fsm_update 'data' field + """ + pass