baseimage-tutorial-framework/lib/tfw/event_handler_base.py

71 lines
2.1 KiB
Python
Raw Normal View History

# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details.
from abc import ABC, abstractmethod
from tfw.networking import deserialize_tfw_msg
from tfw.networking.event_handlers import ServerConnector
class EventHandlerBase(ABC):
def __init__(self, key):
2018-01-26 14:16:34 +00:00
self.server_connector = ServerConnector()
self.key = key
2018-01-25 16:04:34 +00:00
self.subscriptions = set()
self.subscribe(self.key)
2018-01-25 16:04:34 +00:00
self.subscribe('reset')
2018-01-26 14:16:34 +00:00
self.server_connector.register_callback(self.event_handler_callback)
def event_handler_callback(self, msg_parts):
message = deserialize_tfw_msg(*msg_parts)
response = self.dispatch_handling(message)
2018-04-16 10:51:01 +00:00
response['key'] = message['key']
if response is None:
return
self.server_connector.send(response)
def dispatch_handling(self, message):
if message['key'] != 'reset':
return self.handle_event(message)
return self.handle_reset(message)
@abstractmethod
def handle_event(self, message):
raise NotImplementedError
def handle_reset(self, message):
# pylint: disable=unused-argument,no-self-use
return None
2018-02-13 14:38:46 +00:00
def cleanup(self):
pass
def subscribe(self, key):
if key not in self.subscriptions:
self.subscriptions.add(key)
self.server_connector.subscribe(key)
def unsubscribe(self, key):
try:
self.subscriptions.remove(key)
self.server_connector.unsubscribe(key)
except KeyError:
pass
def unsubscribe_all(self):
for sub in self.subscriptions:
self.server_connector.unsubscribe(key=sub)
self.subscriptions.clear()
class TriggeredEventHandler(EventHandlerBase, ABC):
# pylint: disable=abstract-method
def __init__(self, key, trigger):
super().__init__(key)
self.trigger = trigger
def dispatch_handling(self, message):
if message.get('trigger') == self.trigger:
return super().dispatch_handling(message)
return None