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
|
2019-05-02 12:27:48 +00:00
|
|
|
from inspect import currentframe
|
2019-05-06 15:11:56 +00:00
|
|
|
from typing import Iterable
|
2018-03-27 15:49:32 +00:00
|
|
|
|
2019-05-27 12:09:13 +00:00
|
|
|
from tfw.networking import ServerConnector
|
2018-06-29 20:02:26 +00:00
|
|
|
from tfw.config.logs import logging
|
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
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-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
Abstract base class for all Python based EventHandlers. Useful implementation template
|
|
|
|
for other languages.
|
|
|
|
|
|
|
|
Derived classes must implement the handle_event() method
|
|
|
|
"""
|
2018-02-21 14:28:16 +00:00
|
|
|
def __init__(self, key):
|
2018-01-26 14:16:34 +00:00
|
|
|
self.server_connector = ServerConnector()
|
2019-05-06 15:11:56 +00:00
|
|
|
self.keys = []
|
|
|
|
if isinstance(key, str):
|
|
|
|
self.keys.append(key)
|
|
|
|
elif isinstance(key, Iterable):
|
|
|
|
self.keys = list(key)
|
|
|
|
|
2018-07-14 18:06:34 +00:00
|
|
|
self.subscribe(*self.keys)
|
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
|
|
|
|
2018-07-14 18:06:34 +00:00
|
|
|
@property
|
|
|
|
def key(self):
|
|
|
|
"""
|
|
|
|
Returns the oldest key this EventHandler was subscribed to.
|
|
|
|
"""
|
|
|
|
return self.keys[0]
|
|
|
|
|
2018-06-29 09:50:36 +00:00
|
|
|
def event_handler_callback(self, message):
|
2018-04-18 18:07:41 +00:00
|
|
|
"""
|
|
|
|
Callback that is invoked when receiving a message.
|
2018-04-18 18:16:56 +00:00
|
|
|
Dispatches messages to handler methods and sends
|
|
|
|
a response back in case the handler returned something.
|
2018-04-18 18:07:41 +00:00
|
|
|
This is subscribed in __init__().
|
|
|
|
"""
|
2018-07-12 13:17:39 +00:00
|
|
|
if not self.check_key(message):
|
|
|
|
return
|
|
|
|
|
2018-04-13 18:45:34 +00:00
|
|
|
response = self.dispatch_handling(message)
|
2018-04-18 18:16:56 +00:00
|
|
|
if response:
|
2019-05-26 16:26:33 +00:00
|
|
|
self.send_message(response)
|
|
|
|
|
|
|
|
def send_message(self, message):
|
|
|
|
self.server_connector.send_message(message)
|
2018-01-17 13:26:16 +00:00
|
|
|
|
2018-07-12 13:17:39 +00:00
|
|
|
def check_key(self, message):
|
|
|
|
"""
|
|
|
|
Checks whether the message is intended for this
|
|
|
|
EventHandler.
|
|
|
|
|
|
|
|
This is necessary because ZMQ handles PUB - SUB
|
|
|
|
connetions with pattern matching (e.g. someone
|
|
|
|
subscribed to 'fsm' will receive 'fsm_update'
|
|
|
|
messages as well.
|
|
|
|
"""
|
2019-05-05 18:57:22 +00:00
|
|
|
if '' in self.keys:
|
|
|
|
return True
|
2018-07-14 18:06:34 +00:00
|
|
|
return message['key'] in self.keys
|
2018-07-12 13:17:39 +00:00
|
|
|
|
2018-04-13 18:45:34 +00:00
|
|
|
def dispatch_handling(self, message):
|
2018-04-18 18:16:56 +00:00
|
|
|
"""
|
|
|
|
Used to dispatch messages to their specific handlers.
|
2018-06-29 20:02:26 +00:00
|
|
|
|
|
|
|
:param message: the message received
|
|
|
|
:returns: the message to send back
|
2018-04-18 18:16:56 +00:00
|
|
|
"""
|
2018-07-13 13:42:18 +00:00
|
|
|
return self.handle_event(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-04-18 18:07:41 +00:00
|
|
|
"""
|
|
|
|
Abstract method that implements the handling of messages.
|
|
|
|
|
|
|
|
:param message: the message received
|
2018-06-29 20:02:26 +00:00
|
|
|
:returns: the message to send back
|
2018-04-18 18:07:41 +00:00
|
|
|
"""
|
2018-01-17 13:26:16 +00:00
|
|
|
raise NotImplementedError
|
2017-11-27 17:20:09 +00:00
|
|
|
|
2018-04-18 18:07:41 +00:00
|
|
|
def subscribe(self, *keys):
|
|
|
|
"""
|
|
|
|
Subscribe this EventHandler to receive events for given keys.
|
|
|
|
Note that you can subscribe to the same key several times in which
|
|
|
|
case you will need to unsubscribe multiple times in order to stop
|
|
|
|
receiving events.
|
2018-07-15 15:30:19 +00:00
|
|
|
|
2018-04-18 18:07:41 +00:00
|
|
|
:param keys: list of keys to subscribe to
|
|
|
|
"""
|
|
|
|
for key in keys:
|
2018-02-21 14:28:16 +00:00
|
|
|
self.server_connector.subscribe(key)
|
2018-07-14 18:06:34 +00:00
|
|
|
self.keys.append(key)
|
2017-11-27 17:34:26 +00:00
|
|
|
|
2018-04-18 18:07:41 +00:00
|
|
|
def unsubscribe(self, *keys):
|
|
|
|
"""
|
|
|
|
Unsubscribe this eventhandler from the given keys.
|
|
|
|
|
|
|
|
:param keys: list of keys to unsubscribe from
|
|
|
|
"""
|
|
|
|
for key in keys:
|
2018-02-21 14:28:16 +00:00
|
|
|
self.server_connector.unsubscribe(key)
|
2018-07-14 18:06:34 +00:00
|
|
|
self.keys.remove(key)
|
2017-11-27 17:34:26 +00:00
|
|
|
|
2019-05-20 09:06:57 +00:00
|
|
|
def stop(self):
|
|
|
|
self.server_connector.close()
|
|
|
|
self.cleanup()
|
|
|
|
|
2018-04-18 18:07:41 +00:00
|
|
|
def cleanup(self):
|
|
|
|
"""
|
|
|
|
Perform cleanup actions such as releasing database
|
|
|
|
connections and stuff like that.
|
|
|
|
"""
|
2018-02-23 13:07:41 +00:00
|
|
|
|
2019-05-02 12:27:48 +00:00
|
|
|
@classmethod
|
|
|
|
def get_local_instances(cls):
|
|
|
|
frame = currentframe()
|
|
|
|
if frame is None:
|
|
|
|
raise EnvironmentError('inspect.currentframe() is not supported!')
|
|
|
|
|
|
|
|
locals_values = frame.f_back.f_locals.values()
|
|
|
|
return {
|
|
|
|
instance for instance in locals_values
|
|
|
|
if isinstance(instance, cls)
|
|
|
|
}
|