Skip to content

Commit

Permalink
PEP-8ing it
Browse files Browse the repository at this point in the history
  • Loading branch information
a20r committed Feb 6, 2016
2 parents 98838d6 + 55fd8db commit ef62625
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
18 changes: 15 additions & 3 deletions src/client/connection.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

# import zlib
import zlib
import threading
import json
import copy
import struct
from twisted.internet import reactor
from autobahn.twisted.websocket import WebSocketClientProtocol
from autobahn.twisted.websocket import WebSocketClientFactory
Expand All @@ -20,6 +21,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 = struct.unpack('=I', decompressed[:4])
frmt = "%ds" % size[0]
unpacked = struct.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 +55,12 @@ 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 = struct.pack(frmt, payload)
binLen = len(binary)
binary = struct.pack('=I' + frmt, binLen, payload)
compressed = zlib.compress(binary)
return MMClient.send_message(compressed, True)

def updates(self):
payloads = copy.copy(MMClient.updates)
Expand Down
3 changes: 0 additions & 3 deletions src/server/server_node.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#!/usr/bin/env python

# import sys
import ws
import rospy
# from twisted.python import log
from twisted.internet import reactor
from autobahn.twisted.websocket import WebSocketServerFactory

Expand All @@ -12,7 +10,6 @@


def run_server(host, port):
# log.startLogging(sys.stdout)
url = "ws://{}:{}".format(host, port)
factory = WebSocketServerFactory(url, debug=True)
factory.protocol = ws.MMServerProtocol
Expand Down
18 changes: 12 additions & 6 deletions src/server/ws.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

# import zlib
import zlib
import json
import rospy
import time
import common
import struct
from std_msgs.msg import Float32
from autobahn.twisted.websocket import WebSocketServerProtocol

Expand All @@ -17,18 +18,23 @@ def onConnect(self, request):
self.lat_pub = rospy.Publisher("/jammi/latency", Float32, queue_size=2)

def onMessage(self, payload, is_binary):
if not is_binary:
if is_binary:
try:
msg = json.loads(payload)
received_time = time.time()
decompressed = zlib.decompress(payload)
size = struct.unpack('=I', decompressed[:4])
frmt = "%ds" % size[0]
unpacked = struct.unpack('=I' + frmt, decompressed)
msg = json.loads(unpacked[1])
latency = Float32()
latency.data = time.time() - msg["stamp"]
latency.data = received_time - 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)
common.get_client(name).sendMessage(payload, True)
else:
common.get_client(msg["to"]).sendMessage(payload)
common.get_client(msg["to"]).sendMessage(payload, True)
except KeyError:
pass

Expand Down

0 comments on commit ef62625

Please sign in to comment.