-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5fb5aa8
commit d210666
Showing
4 changed files
with
115 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Defines the Connection class. | ||
|
||
import zlib | ||
import threading | ||
import json | ||
import copy | ||
import struct | ||
import rospy | ||
import time | ||
import socket | ||
|
||
# Represents a threaded websocket connection to the server. | ||
class Receiver(threading.Thread): | ||
|
||
def __init__(self, socket): | ||
super(Receiver, self).__init__() | ||
self.socket = socket | ||
self.values = dict() | ||
|
||
# Starts the Tornado IOLoop and connects to the websocket. | ||
# Called on thread start. | ||
def run(self): | ||
while True: | ||
data = self.socket.recv(65565) | ||
if data == "HANDSHAKE": | ||
continue | ||
self.process_message(data) | ||
|
||
# Returns the formatted last received message. | ||
def updates(self): | ||
payloads = copy.copy(self.values) | ||
self.values = dict() | ||
return payloads | ||
|
||
# Callback for message receiving. | ||
# Decompresses messages, converts to unicode, | ||
# and converts from JSON to dictionary. | ||
def process_message(self, payload): | ||
try: | ||
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]) | ||
self.values[data["Topic"]] = data | ||
except: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Defines the Connection class. | ||
|
||
import zlib | ||
import threading | ||
import json | ||
import copy | ||
import struct | ||
import rospy | ||
import time | ||
import socket | ||
|
||
# Represents a threaded websocket connection to the server. | ||
class Sender(): | ||
lock = threading.Lock() | ||
|
||
def __init__(self, socket): | ||
self.socket = socket | ||
self.data = None | ||
self.worker = None | ||
self.values = dict() | ||
|
||
# Starts the Tornado IOLoop and connects to the websocket. | ||
# Called on thread start. | ||
def run(self): | ||
self.send_message_cb(self.data) | ||
|
||
# Formats data dictionary as JSON, converts to binary, | ||
# compresses using zlib, and sends to the server. | ||
def send_message_cb(self, data): | ||
payload = json.dumps(data) | ||
frmt = "%ds" % len(payload) | ||
binary = struct.pack(frmt, payload) | ||
binLen = len(binary) | ||
binary = struct.pack('=I' + frmt, binLen, payload) | ||
compressed = zlib.compress(binary) | ||
with Sender.lock: | ||
self.socket.sendall(compressed) | ||
|
||
# Creates callback to send message in IOLoop. | ||
def send_message(self, data): | ||
self.data = data | ||
self.worker = threading.Thread(target = self.run) | ||
self.worker.start() |