refactored
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
|
||||
|
||||
class MsgContainer:
|
||||
def __init__(self, name: str, timestamp: float = 0, data = b''):
|
||||
self.timestamp : float = timestamp
|
||||
self.name : str= name
|
||||
self.data: bytes = data
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.timestamp}:{self.name}:{self.data}"
|
||||
|
||||
def __repr__(self):
|
||||
return f"{self.timestamp}:{self.name}:{self.data}"
|
||||
@@ -0,0 +1,13 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from msg.container import MsgContainer
|
||||
|
||||
class MsgListener(ABC):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def on_recv(self, msg: MsgContainer):
|
||||
pass
|
||||
|
||||
def is_msg(self, msg: MsgContainer):
|
||||
return False
|
||||
@@ -0,0 +1,15 @@
|
||||
from abc import ABC
|
||||
from msg.container import MsgContainer
|
||||
from msg.listener import MsgListener
|
||||
|
||||
class MsgTalker(ABC):
|
||||
def __init__(self):
|
||||
self.sinks = []
|
||||
|
||||
def register_listener(self, sink: MsgListener):
|
||||
self.sinks.append(sink)
|
||||
|
||||
def call_listener(self, msg: MsgContainer):
|
||||
for sink in self.sinks:
|
||||
if sink.is_msg(msg):
|
||||
sink.on_recv(msg)
|
||||
Reference in New Issue
Block a user