Files
picar/Messaging/message_factory.py
2020-02-27 20:45:15 +10:30

44 lines
1.2 KiB
Python

import zmq
class ZmqPubSubStreamer:
'''
Not thread-safe. Always get this inside the thread/process where you intend
to use it.
'''
def __init__(self, port):
# 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.
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):
'''
Args
----
message: A message type that has the serialise() method.
'''
self.send_message_topic("", message)
def send_message_topic(self, topic, message):
self._socket.send_multipart([bytes(topic), message.serialise()])
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)