hugvey/hugvey/communication.py
2019-03-25 17:45:07 +01:00

28 lines
910 B
Python

import json
import logging
mainLogger = logging.getLogger("hugvey")
# hyper verbose log level. Have it here, becase it needs to be _somewhere_
LOG_BS = 5
def getTopic(hugvey_id):
return "hv{}".format(hugvey_id)
def zmqSend(socket, hugvey_id, msg):
log = mainLogger.getChild("{}".format(hugvey_id)).getChild("communication")
log.debug("SEND: {}".format(msg))
msgData = json.dumps(msg)
topic = getTopic(hugvey_id)
log.debug("Send 0mq to {} containing {}".format(topic, msg))
socket.send_multipart([topic.encode(), msgData.encode()])
async def zmqReceive(socket):
topic, msg = await socket.recv_multipart()
hugvey_id = topic.decode()[2:]
mainLogger.getChild("{}".format(hugvey_id)).getChild("communication").debug(
"Received 0mq messages for Hugvey #{} containing {}".format(hugvey_id, msg.decode())
)
return int(hugvey_id), json.loads(msg.decode())