diff options
author | 2016-08-03 16:21:12 +0300 | |
---|---|---|
committer | 2016-08-03 16:21:12 +0300 | |
commit | 0f863b48e742ecd6b6dd522803e95a528024bbc9 (patch) | |
tree | 81798d3fb9d40ee41efbb41d69170c752a5c3a5f /scripts/automation/trex_control_plane/stl/trex_stl_lib/utils | |
parent | 3159743120d9e1033c5ed809c1031b814204fd8f (diff) | |
parent | 0ccbb8ff779d4e905fc4fea5d2570f6e72821b0e (diff) |
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'scripts/automation/trex_control_plane/stl/trex_stl_lib/utils')
-rw-r--r-- | scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/zipmsg.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/zipmsg.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/zipmsg.py new file mode 100644 index 00000000..397ada16 --- /dev/null +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/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 + |