2019-07-10 12:26:12 +00:00
|
|
|
class EventHandler:
|
|
|
|
_instances = set()
|
|
|
|
|
2019-07-30 13:17:29 +00:00
|
|
|
def __init__(self, connector):
|
2019-07-10 12:26:12 +00:00
|
|
|
type(self)._instances.add(self)
|
2019-07-30 13:17:29 +00:00
|
|
|
self.connector = connector
|
2019-07-10 12:26:12 +00:00
|
|
|
|
|
|
|
def start(self):
|
2019-07-30 13:17:29 +00:00
|
|
|
self.connector.register_callback(self._event_callback)
|
2019-07-10 12:26:12 +00:00
|
|
|
|
|
|
|
def _event_callback(self, message):
|
2019-08-26 09:10:39 +00:00
|
|
|
if self._validate_message(message):
|
|
|
|
self.handle_event(message, self.connector)
|
|
|
|
|
|
|
|
def _validate_message(self, message):
|
2019-08-28 14:40:23 +00:00
|
|
|
# pylint: disable=unused-argument,no-self-use
|
2019-08-26 09:10:39 +00:00
|
|
|
return True
|
2019-07-10 12:26:12 +00:00
|
|
|
|
2019-07-30 13:17:29 +00:00
|
|
|
def handle_event(self, message, connector):
|
2019-07-10 12:26:12 +00:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def stop_all_instances(cls):
|
|
|
|
for instance in cls._instances:
|
|
|
|
instance.stop()
|
|
|
|
|
|
|
|
def stop(self):
|
2019-07-30 13:17:29 +00:00
|
|
|
self.connector.close()
|
2019-07-10 12:26:12 +00:00
|
|
|
self.cleanup()
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
pass
|