-
Notifications
You must be signed in to change notification settings - Fork 3
/
__init__.py
55 lines (40 loc) · 1.35 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import logging
from abc import ABC, abstractmethod
from concurrent.futures import ThreadPoolExecutor
logger = logging.getLogger('root')
class Connection(ABC):
def __init__(self, host='localhost', port='8080', user=None, password=None):
self.host = host
self.port = port
self.user = user
self.password = password
self.connect()
@abstractmethod
def connect(self) -> None:
pass
@abstractmethod
def get_connection(self):
pass
@abstractmethod
def is_connected(self) -> bool:
pass
@abstractmethod
def get_connection_attributes(self):
pass
class Publisher(ABC):
def __init__(self, connection: Connection, publisher_thread_pool: ThreadPoolExecutor):
self.connection = connection
self.publisher_thread_pool = publisher_thread_pool
if connection.is_connected():
logger.debug(f'Using the connection {connection.__class__.__name__} for publishing.')
else:
raise ConnectionError(f'Connection failed: {connection.__class__.__name__}')
@abstractmethod
def _publish(self, msgs, topic: str) -> None:
pass
def publish(self, msgs, topic: str):
self.publisher_thread_pool.submit(self._publish, msgs, topic)
class Subscriber(ABC):
@abstractmethod
def subscribe(self):
pass