# 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)