2018-04-03 12:49:14 +00:00
|
|
|
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
|
2018-03-27 15:49:32 +00:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
2018-04-13 17:57:04 +00:00
|
|
|
from tfw.networking import deserialize_tfw_msg
|
2018-04-06 13:21:45 +00:00
|
|
|
from tfw.networking.event_handlers import ServerConnector
|
2018-01-25 14:24:42 +00:00
|
|
|
|
2017-11-27 17:20:09 +00:00
|
|
|
|
2018-03-27 15:49:32 +00:00
|
|
|
class EventHandlerBase(ABC):
|
2018-02-21 14:28:16 +00:00
|
|
|
def __init__(self, key):
|
2018-01-26 14:16:34 +00:00
|
|
|
self.server_connector = ServerConnector()
|
2018-02-21 14:28:16 +00:00
|
|
|
self.key = key
|
2018-01-25 16:04:34 +00:00
|
|
|
self.subscriptions = set()
|
2018-02-21 14:28:16 +00:00
|
|
|
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)
|
2018-01-17 13:26:16 +00:00
|
|
|
|
|
|
|
def event_handler_callback(self, msg_parts):
|
2018-04-13 17:57:04 +00:00
|
|
|
message = deserialize_tfw_msg(*msg_parts)
|
2018-04-13 18:45:34 +00:00
|
|
|
response = self.dispatch_handling(message)
|
2018-04-16 10:51:01 +00:00
|
|
|
response['key'] = message['key']
|
2018-03-30 15:50:20 +00:00
|
|
|
if response is None:
|
|
|
|
return
|
2018-04-12 13:11:52 +00:00
|
|
|
self.server_connector.send(response)
|
2018-01-17 13:26:16 +00:00
|
|
|
|
2018-03-27 15:49:32 +00:00
|
|
|
@abstractmethod
|
2018-04-13 18:45:34 +00:00
|
|
|
def dispatch_handling(self, message):
|
2018-02-23 13:07:41 +00:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2018-04-13 18:45:34 +00:00
|
|
|
def _dispatch_handling(self, message):
|
2018-03-30 16:11:38 +00:00
|
|
|
# pylint: disable=no-else-return
|
2018-04-13 18:45:34 +00:00
|
|
|
if message['key'] != 'reset':
|
|
|
|
return self.handle_event(message)
|
2018-03-30 15:50:20 +00:00
|
|
|
else:
|
|
|
|
return self.handle_reset(message)
|
2018-02-23 13:07:41 +00:00
|
|
|
|
2018-03-27 15:49:32 +00:00
|
|
|
@abstractmethod
|
2018-04-13 18:45:34 +00:00
|
|
|
def handle_event(self, message):
|
2018-01-17 13:26:16 +00:00
|
|
|
raise NotImplementedError
|
2017-11-27 17:20:09 +00:00
|
|
|
|
2018-03-08 15:11:43 +00:00
|
|
|
def handle_reset(self, message):
|
2018-03-30 16:11:38 +00:00
|
|
|
# pylint: disable=unused-argument,no-self-use
|
2018-01-17 15:33:34 +00:00
|
|
|
return None
|
|
|
|
|
2018-02-13 14:38:46 +00:00
|
|
|
def cleanup(self):
|
|
|
|
pass
|
|
|
|
|
2018-02-21 14:28:16 +00:00
|
|
|
def subscribe(self, key):
|
|
|
|
if key not in self.subscriptions:
|
|
|
|
self.subscriptions.add(key)
|
|
|
|
self.server_connector.subscribe(key)
|
2017-11-27 17:34:26 +00:00
|
|
|
|
2018-02-21 14:28:16 +00:00
|
|
|
def unsubscribe(self, key):
|
2017-11-27 17:34:26 +00:00
|
|
|
try:
|
2018-02-21 14:28:16 +00:00
|
|
|
self.subscriptions.remove(key)
|
|
|
|
self.server_connector.unsubscribe(key)
|
2017-11-27 17:34:26 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def unsubscribe_all(self):
|
|
|
|
for sub in self.subscriptions:
|
2018-02-21 14:28:16 +00:00
|
|
|
self.server_connector.unsubscribe(key=sub)
|
2017-11-27 17:34:26 +00:00
|
|
|
self.subscriptions.clear()
|
2018-02-23 13:07:41 +00:00
|
|
|
|
|
|
|
|
2018-03-27 15:49:32 +00:00
|
|
|
class TriggerlessEventHandler(EventHandlerBase, ABC):
|
2018-03-30 16:11:38 +00:00
|
|
|
# pylint: disable=abstract-method
|
2018-04-13 18:45:34 +00:00
|
|
|
def dispatch_handling(self, message):
|
|
|
|
return self._dispatch_handling(message)
|
2018-02-23 13:07:41 +00:00
|
|
|
|
|
|
|
|
2018-03-27 15:49:32 +00:00
|
|
|
class TriggeredEventHandler(EventHandlerBase, ABC):
|
2018-03-30 16:11:38 +00:00
|
|
|
# pylint: disable=abstract-method
|
2018-02-23 13:07:41 +00:00
|
|
|
def __init__(self, key, trigger):
|
|
|
|
super().__init__(key)
|
|
|
|
self.trigger = trigger
|
|
|
|
|
2018-04-13 18:45:34 +00:00
|
|
|
def dispatch_handling(self, message):
|
2018-02-23 13:07:41 +00:00
|
|
|
if message.get('trigger') == self.trigger:
|
2018-04-13 18:45:34 +00:00
|
|
|
return self._dispatch_handling(message)
|
2018-03-30 16:11:38 +00:00
|
|
|
return None
|