diff options
author | Yaroslav Brustinov <ybrustin@cisco.com> | 2016-08-03 17:24:59 +0300 |
---|---|---|
committer | Yaroslav Brustinov <ybrustin@cisco.com> | 2016-08-03 17:24:59 +0300 |
commit | d1b0fd96d72b7daaa88e21b619d04084c7617ad1 (patch) | |
tree | 092b65bb5ed34188731726c627ccdcd6896261ce /scripts/automation/trex_control_plane/server/zipmsg.py | |
parent | 775616cd5a8b06c115b5faaf499cbc74fcb8dcc2 (diff) |
stf daemon: add support for zipped zmq
Diffstat (limited to 'scripts/automation/trex_control_plane/server/zipmsg.py')
-rw-r--r-- | scripts/automation/trex_control_plane/server/zipmsg.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/scripts/automation/trex_control_plane/server/zipmsg.py b/scripts/automation/trex_control_plane/server/zipmsg.py new file mode 100644 index 00000000..397ada16 --- /dev/null +++ b/scripts/automation/trex_control_plane/server/zipmsg.py @@ -0,0 +1,32 @@ +import zlib +import struct + +class ZippedMsg: + + MSG_COMPRESS_THRESHOLD = 256 + MSG_COMPRESS_HEADER_MAGIC = 0xABE85CEA + + def check_threshold (self, msg): + return len(msg) >= self.MSG_COMPRESS_THRESHOLD + + def compress (self, msg): + # compress + compressed = zlib.compress(msg) + new_msg = struct.pack(">II", self.MSG_COMPRESS_HEADER_MAGIC, len(msg)) + compressed + return new_msg + + + def decompress (self, msg): + if len(msg) < 8: + return None + + t = struct.unpack(">II", msg[:8]) + if (t[0] != self.MSG_COMPRESS_HEADER_MAGIC): + return None + + x = zlib.decompress(msg[8:]) + if len(x) != t[1]: + return None + + return x + |