mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-23 01:11:33 +00:00
Implement event handler base class that broadcasts everything
This commit is contained in:
parent
196e753fb9
commit
a6563bcd89
@ -1,6 +1,6 @@
|
|||||||
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
||||||
# All Rights Reserved. See LICENSE file for details.
|
# All Rights Reserved. See LICENSE file for details.
|
||||||
|
|
||||||
from .event_handler_base import EventHandlerBase, TriggeredEventHandler
|
from .event_handler_base import EventHandlerBase, TriggeredEventHandler, BroadcastingEventHandler
|
||||||
from .fsm_base import FSMBase
|
from .fsm_base import FSMBase
|
||||||
from .linear_fsm import LinearFSM
|
from .linear_fsm import LinearFSM
|
||||||
|
@ -2,8 +2,13 @@
|
|||||||
# All Rights Reserved. See LICENSE file for details.
|
# All Rights Reserved. See LICENSE file for details.
|
||||||
|
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
|
from json import dumps
|
||||||
|
from hashlib import md5
|
||||||
|
|
||||||
from tfw.networking.event_handlers import ServerConnector
|
from tfw.networking.event_handlers import ServerConnector
|
||||||
|
from tfw.config.logs import logging
|
||||||
|
|
||||||
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class EventHandlerBase(ABC):
|
class EventHandlerBase(ABC):
|
||||||
@ -28,12 +33,14 @@ class EventHandlerBase(ABC):
|
|||||||
"""
|
"""
|
||||||
response = self.dispatch_handling(message)
|
response = self.dispatch_handling(message)
|
||||||
if response:
|
if response:
|
||||||
response['key'] = message['key']
|
|
||||||
self.server_connector.send(response)
|
self.server_connector.send(response)
|
||||||
|
|
||||||
def dispatch_handling(self, message):
|
def dispatch_handling(self, message):
|
||||||
"""
|
"""
|
||||||
Used to dispatch messages to their specific handlers.
|
Used to dispatch messages to their specific handlers.
|
||||||
|
|
||||||
|
:param message: the message received
|
||||||
|
:returns: the message to send back
|
||||||
"""
|
"""
|
||||||
if message['key'] != 'reset':
|
if message['key'] != 'reset':
|
||||||
return self.handle_event(message)
|
return self.handle_event(message)
|
||||||
@ -45,6 +52,7 @@ class EventHandlerBase(ABC):
|
|||||||
Abstract method that implements the handling of messages.
|
Abstract method that implements the handling of messages.
|
||||||
|
|
||||||
:param message: the message received
|
:param message: the message received
|
||||||
|
:returns: the message to send back
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
@ -54,6 +62,7 @@ class EventHandlerBase(ABC):
|
|||||||
Usually 'reset' events receive some sort of special treatment.
|
Usually 'reset' events receive some sort of special treatment.
|
||||||
|
|
||||||
:param message: the message received
|
:param message: the message received
|
||||||
|
:returns: the message to send back
|
||||||
"""
|
"""
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -100,3 +109,38 @@ class TriggeredEventHandler(EventHandlerBase, ABC):
|
|||||||
if message.get('trigger') == self.trigger:
|
if message.get('trigger') == self.trigger:
|
||||||
return super().dispatch_handling(message)
|
return super().dispatch_handling(message)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
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 = self.hash_message(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(self.hash_message(response))
|
||||||
|
self.server_connector.send(self.make_broadcast_message(response))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def hash_message(message):
|
||||||
|
message_bytes = dumps(message, sort_keys=True).encode()
|
||||||
|
return md5(message_bytes).hexdigest()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def make_broadcast_message(message):
|
||||||
|
return {
|
||||||
|
'key': 'broadcast',
|
||||||
|
'data': message
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user