Use message factory for slam streamer

This commit is contained in:
Piv
2020-02-27 20:45:15 +10:30
parent a5519dd651
commit 9878aaf6a4
3 changed files with 25 additions and 42 deletions

View File

@@ -1,25 +1,7 @@
import zmq
class MessageFactory():
def getZmqPubSubStreamer(self, topic):
'''
Not thread-safe. Always get this inside the thread/process where you intend
to use it.
'''
return ZmqPubSubStreamer(topic)
class Streamer():
def send_message(self, message_bytes):
raise NotImplementedError
def send_message_topic(self, topic, message_bytes):
raise NotImplementedError
class ZmqPubSubStreamer(Streamer):
class ZmqPubSubStreamer:
'''
Not thread-safe. Always get this inside the thread/process where you intend
to use it.
@@ -29,7 +11,9 @@ class ZmqPubSubStreamer(Streamer):
# Should create the socket here always, since zmq is not thread safe.
# Hopefully whoever uses this is not stupid enough to create it then
# pass it into a thread.
pass
self._socket = zmq.Context.instance().socket(zmq.PUB)
print('Starting socket with address: ' + 'tcp://*:' + str(port))
self._socket.bind("tcp://*:" + str(port))
def send_message(self, message):
'''
@@ -37,15 +21,23 @@ class ZmqPubSubStreamer(Streamer):
----
message: A message type that has the serialise() method.
'''
pass
self.send_message_topic("", message)
def send_message_topic(self, topic, message):
pass
self._socket.send_multipart([bytes(topic), message.serialise()])
class BluetoothStreamer(Streamer):
class BluetoothStreamer:
def __init__(self):
pass
def send_message(self, message_bytes):
pass
def getZmqPubSubStreamer(port):
'''
Not thread-safe. Always get this inside the thread/process where you intend
to use it.
'''
return ZmqPubSubStreamer(port)