mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-06 03:01:21 +00:00
70 lines
1.6 KiB
Python
70 lines
1.6 KiB
Python
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
"""
|
|
TFW JSON message format
|
|
|
|
message:
|
|
{
|
|
"key": string, # addressing
|
|
"data": {...}, # payload
|
|
"trigger": string # FSM trigger
|
|
}
|
|
|
|
ZeroMQ's sub-pub sockets use enveloped messages
|
|
(http://zguide.zeromq.org/page:all#Pub-Sub-Message-Envelopes)
|
|
and TFW also uses them internally. This means that on ZMQ sockets
|
|
we always send the messages key separately and then the actual
|
|
message (which contains the key as well) like so:
|
|
|
|
socket.send_multipart([message['key'], message])
|
|
|
|
The purpose of this module is abstracting away this low level behaviour.
|
|
"""
|
|
|
|
import json
|
|
|
|
|
|
def validate_message(message):
|
|
return 'key' in message
|
|
|
|
|
|
def serialize_tfw_msg(message):
|
|
return _serialize_all(message['key'], message)
|
|
|
|
|
|
def deserialize_tfw_msg(*args):
|
|
return _deserialize_all(*args)[1]
|
|
|
|
|
|
def _serialize_all(*args):
|
|
return tuple(_serialize_single(arg) for arg in args)
|
|
|
|
|
|
def _deserialize_all(*args):
|
|
return tuple(_deserialize_single(arg) for arg in args)
|
|
|
|
|
|
def _serialize_single(data):
|
|
if not isinstance(data, str):
|
|
data = json.dumps(data)
|
|
return _encode_if_needed(data)
|
|
|
|
|
|
def _deserialize_single(data):
|
|
try:
|
|
return json.loads(data)
|
|
except ValueError:
|
|
return _decode_if_needed(data)
|
|
|
|
|
|
def _encode_if_needed(value):
|
|
if isinstance(value, str):
|
|
value = value.encode('utf-8')
|
|
return value
|
|
|
|
|
|
def _decode_if_needed(value):
|
|
if isinstance(value, (bytes, bytearray)):
|
|
value = value.decode('utf-8')
|
|
return value
|