-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The CBOR compression is already a huge win over JSON or PNG encoding, but it’s still suboptimal in some situations. This PR adds support for getting messages in their raw binary (ROS-serialized) format. This has benefits in the following cases: - Your application already knows how to parse messages in bag files (e.g. using [rosbag.js](https://github.com/cruise-automation/rosbag.js), which means that now you can use consistent code paths for both bags and live messages. - You want to parse messages as late as possible, or in parallel, e.g. only in the thread or WebWorker that cares about the message. Delaying the parsing of the message means that moving or copying the message to the thread is cheaper when its in binary form, since no serialization between threads is necessary. - You only care about part of the message, and don't need to parse the rest of it. - You really care about performance; no conversion between the ROS binary format and CBOR is done in the rosbridge_sever.
- Loading branch information
1 parent
856987e
commit dc7fcb2
Showing
10 changed files
with
161 additions
and
7 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 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 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 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 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,5 @@ | ||
|
||
--- | ||
string[] topics | ||
string[] types | ||
string[] typedefs_full_text |
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 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 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 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,76 @@ | ||
#!/usr/bin/env python | ||
import json | ||
import io | ||
import os | ||
import rostest | ||
import sys | ||
import threading | ||
import unittest | ||
|
||
import rospy | ||
from std_msgs.msg import String | ||
|
||
from twisted.internet import reactor | ||
from autobahn.twisted.websocket import (WebSocketClientProtocol, | ||
WebSocketClientFactory) | ||
|
||
from rosbridge_library.util.cbor import loads as decode_cbor | ||
|
||
from twisted.python import log | ||
log.startLogging(sys.stderr) | ||
|
||
TOPIC = '/b_topic' | ||
STRING = 'B' * 10000 | ||
WARMUP_DELAY = 1.0 # seconds | ||
TIME_LIMIT = 5.0 # seconds | ||
|
||
|
||
class TestWebsocketCborRaw(unittest.TestCase): | ||
def test_cbor_raw(self): | ||
test_client_received = [] | ||
class TestClientProtocol(WebSocketClientProtocol): | ||
def onOpen(self): | ||
self.sendMessage(json.dumps({ | ||
'op': 'subscribe', | ||
'topic': TOPIC, | ||
'compression': 'cbor-raw', | ||
})) | ||
|
||
def onMessage(self, payload, binary): | ||
test_client_received.append(payload) | ||
|
||
protocol = os.environ.get('PROTOCOL') | ||
port = rospy.get_param('/rosbridge_websocket/actual_port') | ||
url = protocol + '://127.0.0.1:' + str(port) | ||
|
||
factory = WebSocketClientFactory(url) | ||
factory.protocol = TestClientProtocol | ||
reactor.connectTCP('127.0.0.1', port, factory) | ||
|
||
pub = rospy.Publisher(TOPIC, String, queue_size=1) | ||
def publish_timer(): | ||
rospy.sleep(WARMUP_DELAY) | ||
pub.publish(String(STRING)) | ||
rospy.sleep(TIME_LIMIT) | ||
reactor.stop() | ||
reactor.callInThread(publish_timer) | ||
reactor.run() | ||
|
||
self.assertEqual(len(test_client_received), 1) | ||
websocket_message = decode_cbor(test_client_received[0]) | ||
self.assertEqual(websocket_message['topic'], TOPIC) | ||
buff = io.BytesIO() | ||
String(STRING).serialize(buff) | ||
self.assertEqual(websocket_message['msg']['bytes'], buff.getvalue()) | ||
|
||
|
||
PKG = 'rosbridge_server' | ||
NAME = 'test_websocket_cbor_raw' | ||
|
||
if __name__ == '__main__': | ||
rospy.init_node(NAME) | ||
|
||
while not rospy.is_shutdown() and not rospy.has_param('/rosbridge_websocket/actual_port'): | ||
rospy.sleep(1.0) | ||
|
||
rostest.rosrun(PKG, NAME, TestWebsocketCborRaw) |
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,7 @@ | ||
<launch> | ||
<env name="PROTOCOL" value="ws" /> | ||
<node name="rosbridge_websocket" pkg="rosbridge_server" type="rosbridge_websocket.py"> | ||
<param name="port" value="0" /> | ||
</node> | ||
<test test-name="test_cbor_raw" pkg="rosbridge_server" type="test_cbor_raw.py" /> | ||
</launch> |