hugvey/hugvey/communication.py

29 lines
910 B
Python
Raw Normal View History

2019-01-15 21:40:44 +01:00
import json
import logging
mainLogger = logging.getLogger("hugvey")
2019-01-15 21:40:44 +01:00
# hyper verbose log level. Have it here, becase it needs to be _somewhere_
LOG_BS = 5
2019-01-15 21:40:44 +01:00
def getTopic(hugvey_id):
return "hv{}".format(hugvey_id)
2019-01-16 09:00:49 +01:00
def zmqSend(socket, hugvey_id, msg):
2019-03-25 17:45:07 +01:00
log = mainLogger.getChild("{}".format(hugvey_id)).getChild("communication")
log.debug("SEND: {}".format(msg))
2019-01-15 21:40:44 +01:00
msgData = json.dumps(msg)
topic = getTopic(hugvey_id)
log.debug("Send 0mq to {} containing {}".format(topic, msg))
2019-01-16 09:00:49 +01:00
socket.send_multipart([topic.encode(), msgData.encode()])
2019-01-15 21:40:44 +01:00
async def zmqReceive(socket):
topic, msg = await socket.recv_multipart()
hugvey_id = topic.decode()[2:]
2019-03-25 17:45:07 +01:00
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())