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-07-30 13:17:29 +00:00
|
|
|
self.handle_event(message, self.connector)
|
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
|