Skip to content

Commit

Permalink
Binary conversion and zlib compression
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelchang committed Feb 6, 2016
1 parent 09228e5 commit 55fd8db
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
20 changes: 16 additions & 4 deletions src/client/connection.py
Original file line number Diff line number Diff line change
@@ -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):

Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/server/server_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 23 additions & 2 deletions src/server/ws.py
Original file line number Diff line number Diff line change
@@ -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):

Expand All @@ -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)

0 comments on commit 55fd8db

Please sign in to comment.