mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2025-06-29 00:05:12 +00:00
Rework EventHandler situation
This commit is contained in:
@ -2,7 +2,3 @@
|
||||
# All Rights Reserved. See LICENSE file for details.
|
||||
|
||||
from .event_handler_base import EventHandlerBase
|
||||
from .frontend_event_handler_base import FrontendEventHandlerBase
|
||||
from .boradcasting_event_handler import BroadcastingEventHandler
|
||||
from .fsm_aware_event_handler import FSMAwareEventHandler
|
||||
from .tfw_server_connector import TFWServerUplinkConnector, TFWServerConnector
|
||||
|
@ -1,32 +0,0 @@
|
||||
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
||||
# All Rights Reserved. See LICENSE file for details.
|
||||
|
||||
from abc import ABC
|
||||
|
||||
from tfw.networking import Scope
|
||||
from tfw.crypto import message_checksum
|
||||
|
||||
from .event_handler_base import EventHandlerBase
|
||||
|
||||
|
||||
class BroadcastingEventHandler(EventHandlerBase, ABC):
|
||||
# pylint: disable=abstract-method
|
||||
"""
|
||||
Abstract base class for EventHandlers which broadcast responses
|
||||
and intelligently ignore their own broadcasted messages they receive.
|
||||
"""
|
||||
def __init__(self, key):
|
||||
super().__init__(key)
|
||||
self.own_message_hashes = []
|
||||
|
||||
def event_handler_callback(self, message):
|
||||
message_hash = message_checksum(message)
|
||||
|
||||
if message_hash in self.own_message_hashes:
|
||||
self.own_message_hashes.remove(message_hash)
|
||||
return
|
||||
|
||||
response = self.dispatch_handling(message)
|
||||
if response:
|
||||
self.own_message_hashes.append(message_checksum(response))
|
||||
self.server_connector.send_message(response, Scope.BROADCAST)
|
@ -2,24 +2,24 @@
|
||||
# All Rights Reserved. See LICENSE file for details.
|
||||
|
||||
import logging
|
||||
from abc import ABC, abstractmethod
|
||||
from inspect import currentframe
|
||||
from typing import Iterable
|
||||
|
||||
from .tfw_server_connector import TFWServerConnector
|
||||
from tfw.networking import Scope
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class EventHandlerBase(ABC):
|
||||
class EventHandlerBase:
|
||||
"""
|
||||
Abstract base class for all Python based EventHandlers. Useful implementation template
|
||||
for other languages.
|
||||
|
||||
Derived classes must implement the handle_event() method
|
||||
"""
|
||||
def __init__(self, key):
|
||||
self.server_connector = TFWServerConnector()
|
||||
def __init__(self, key, server_connector, scope=Scope.ZMQ):
|
||||
self.server_connector = server_connector
|
||||
self.scope = scope
|
||||
self.keys = []
|
||||
if isinstance(key, str):
|
||||
self.keys.append(key)
|
||||
@ -51,7 +51,7 @@ class EventHandlerBase(ABC):
|
||||
self.send_message(response)
|
||||
|
||||
def send_message(self, message):
|
||||
self.server_connector.send_message(message)
|
||||
self.server_connector.send_message(message, self.scope)
|
||||
|
||||
def check_key(self, message):
|
||||
"""
|
||||
@ -76,7 +76,6 @@ class EventHandlerBase(ABC):
|
||||
"""
|
||||
return self.handle_event(message)
|
||||
|
||||
@abstractmethod
|
||||
def handle_event(self, message):
|
||||
"""
|
||||
Abstract method that implements the handling of messages.
|
||||
|
@ -1,8 +0,0 @@
|
||||
from tfw.networking import Scope
|
||||
|
||||
from .event_handler_base import EventHandlerBase
|
||||
|
||||
|
||||
class FrontendEventHandlerBase(EventHandlerBase): # pylint: disable=abstract-method
|
||||
def send_message(self, message):
|
||||
self.server_connector.send_message(message, Scope.WEBSOCKET)
|
@ -1,45 +0,0 @@
|
||||
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
||||
# All Rights Reserved. See LICENSE file for details.
|
||||
|
||||
import logging
|
||||
|
||||
from tfw.crypto import KeyManager, verify_message
|
||||
|
||||
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_event_log = []
|
||||
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:
|
||||
update_data = message['data']
|
||||
new_state = update_data['current_state']
|
||||
if self.fsm_state != new_state:
|
||||
self.handle_fsm_step(**update_data)
|
||||
self.fsm_state = new_state
|
||||
self.fsm_in_accepted_state = update_data['in_accepted_state']
|
||||
self.fsm_event_log.append(update_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
|
||||
"""
|
@ -1,24 +0,0 @@
|
||||
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
||||
# All Rights Reserved. See LICENSE file for details.
|
||||
|
||||
from abc import ABC
|
||||
|
||||
from .event_handler_base import EventHandlerBase
|
||||
from .fsm_aware import FSMAware
|
||||
|
||||
|
||||
class FSMAwareEventHandler(EventHandlerBase, FSMAware, ABC):
|
||||
# pylint: disable=abstract-method
|
||||
"""
|
||||
Abstract base class for EventHandlers which automatically
|
||||
keep track of the state of the TFW FSM.
|
||||
"""
|
||||
def __init__(self, key):
|
||||
EventHandlerBase.__init__(self, key)
|
||||
FSMAware.__init__(self)
|
||||
self.subscribe('fsm_update')
|
||||
|
||||
def dispatch_handling(self, message):
|
||||
if self.update_fsm_data(message):
|
||||
return None
|
||||
return super().dispatch_handling(message)
|
@ -1,19 +0,0 @@
|
||||
from functools import partial
|
||||
|
||||
from tfw.networking import ServerUplinkConnector, ServerConnector
|
||||
from tfw.config import TFWENV
|
||||
|
||||
|
||||
UPLINK_CONN_ADDR = f'tcp://localhost:{TFWENV.PULL_PORT}'
|
||||
DOWNLINK_CONN_ADDR = f'tcp://localhost:{TFWENV.PUB_PORT}'
|
||||
|
||||
|
||||
TFWServerUplinkConnector = partial(
|
||||
ServerUplinkConnector,
|
||||
connect_addr=UPLINK_CONN_ADDR
|
||||
)
|
||||
TFWServerConnector = partial(
|
||||
ServerConnector,
|
||||
downlink_connect_addr=DOWNLINK_CONN_ADDR,
|
||||
uplink_connect_addr=UPLINK_CONN_ADDR
|
||||
)
|
Reference in New Issue
Block a user