From 55fd8db51c01c4fc05f984f66c2984b022ec6bcc Mon Sep 17 00:00:00 2001 From: Raphael Chang Date: Fri, 5 Feb 2016 19:00:18 -0500 Subject: [PATCH] Binary conversion and zlib compression --- src/client/connection.py | 20 ++++++++++++++++---- src/server/server_node.py | 2 +- src/server/ws.py | 25 +++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/client/connection.py b/src/client/connection.py index 697a080..20d606c 100644 --- a/src/client/connection.py +++ b/src/client/connection.py @@ -1,12 +1,12 @@ -# import zlib +import zlib import threading import json import copy from twisted.internet import reactor from autobahn.twisted.websocket import WebSocketClientProtocol from autobahn.twisted.websocket import WebSocketClientFactory - +from struct import * class MMClient(WebSocketClientProtocol): @@ -20,6 +20,13 @@ def onMessage(self, payload, is_binary): if not is_binary: data = json.loads(payload) MMClient.updates[data["topic"]] = data + else: + decompressed = zlib.decompress(payload) + size = unpack('=I', decompressed[:4]) + frmt = "%ds" % size[0] + unpacked = unpack('=I' + frmt, decompressed) + data = json.loads(unpacked[1]) + MMClient.updates[data["topic"]] = data @staticmethod def send_message(payload, is_binary): @@ -47,8 +54,13 @@ def stop(self): def send_message(self, data): payload = json.dumps(data) - is_binary = False - return MMClient.send_message(payload, is_binary) + frmt = "%ds" % len(payload) + binary = pack(frmt, payload) + binLen = len(binary) + binary = pack('=I' + frmt, binLen, payload) + compressed = zlib.compress(binary) + is_binary = True + return MMClient.send_message(compressed, is_binary) def updates(self): payloads = copy.copy(MMClient.updates) diff --git a/src/server/server_node.py b/src/server/server_node.py index 52f398d..d271c94 100755 --- a/src/server/server_node.py +++ b/src/server/server_node.py @@ -12,7 +12,7 @@ def run_server(host, port): - log.startLogging(sys.stdout) + #log.startLogging(sys.stdout) url = "ws://{}:{}".format(host, port) factory = WebSocketServerFactory(url, debug=True) factory.protocol = ws.MMServerProtocol diff --git a/src/server/ws.py b/src/server/ws.py index c652235..65a34f3 100644 --- a/src/server/ws.py +++ b/src/server/ws.py @@ -1,12 +1,12 @@ -# import zlib +import zlib import json import rospy import time import common from std_msgs.msg import Float32 from autobahn.twisted.websocket import WebSocketServerProtocol - +from struct import * class MMServerProtocol(WebSocketServerProtocol): @@ -31,6 +31,27 @@ def onMessage(self, payload, is_binary): common.get_client(msg["to"]).sendMessage(payload) except KeyError: pass + else: + try: + receivedTime = time.time() + decompressed = zlib.decompress(payload) + size = unpack('=I', decompressed[:4]) + frmt = "%ds" % size[0] + unpacked = unpack('=I' + frmt, decompressed) + msg = json.loads(unpacked[1]) + latency = Float32() + latency.data = receivedTime - msg["stamp"] + self.lat_pub.publish(latency) + if msg["to"] == "*": + for name in common.clients.keys(): + if name != msg["from"]: + common.get_client(name).sendMessage(payload, True) + else: + common.get_client(msg["to"]).sendMessage(payload, True) + except KeyError: + pass + + def onClose(self, was_clean, code, reason): common.remove_client(self.name_of_client)