-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add CBOR encoding * Fix value extraction performance regression Extract message values once per message. * Fix typed array tags Was using big-endian tags and encoding little-endian. Always use little-endian for now since Intel is prevalent for desktop. Add some comments to this effect. * Update CBOR protocol documentation More information about draft typed arrays and when to use CBOR. * Fix 64-bit integer CBOR packing Use an actual 64-bit format.
- Loading branch information
Showing
13 changed files
with
382 additions
and
30 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
97 changes: 97 additions & 0 deletions
97
rosbridge_library/src/rosbridge_library/internal/cbor_conversion.py
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,97 @@ | ||
import struct | ||
import sys | ||
|
||
PYTHON2 = sys.version_info < (3, 0) | ||
|
||
from cbor import Tag | ||
|
||
|
||
LIST_TYPES = [list, tuple] | ||
INT_TYPES = ['byte', 'char', 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', 'uint64'] | ||
FLOAT_TYPES = ['float32', 'float64'] | ||
STRING_TYPES = ['string'] | ||
BOOL_TYPES = ['bool'] | ||
TIME_TYPES = ['time', 'duration'] | ||
BOOL_ARRAY_TYPES = ['bool[]'] | ||
BYTESTREAM_TYPES = ['uint8[]', 'char[]'] | ||
|
||
# Typed array tags according to <https://tools.ietf.org/html/draft-ietf-cbor-array-tags-00> | ||
# Always encode to little-endian variant, for now. | ||
TAGGED_ARRAY_FORMATS = { | ||
'uint16[]': (69, '<{}H'), | ||
'uint32[]': (70, '<{}I'), | ||
'uint64[]': (71, '<{}Q'), | ||
'byte[]': (72, '{}b'), | ||
'int8[]': (72, '{}b'), | ||
'int16[]': (77, '<{}h'), | ||
'int32[]': (78, '<{}i'), | ||
'int64[]': (79, '<{}q'), | ||
'float32[]': (85, '<{}f'), | ||
'float64[]': (86, '<{}d'), | ||
} | ||
|
||
|
||
def extract_cbor_values(msg): | ||
"""Extract a dictionary of CBOR-friendly values from a ROS message. | ||
Primitive values will be casted to specific Python primitives. | ||
Typed arrays will be tagged and packed into byte arrays. | ||
""" | ||
out = {} | ||
for slot, slot_type in zip(msg.__slots__, msg._slot_types): | ||
val = getattr(msg, slot) | ||
|
||
if PYTHON2: | ||
slot = unicode(slot) | ||
|
||
# string | ||
if slot_type in STRING_TYPES: | ||
out[slot] = unicode(val) if PYTHON2 else str(val) | ||
|
||
# bool | ||
elif slot_type in BOOL_TYPES: | ||
out[slot] = bool(val) | ||
|
||
# integers | ||
elif slot_type in INT_TYPES: | ||
out[slot] = int(val) | ||
|
||
# floats | ||
elif slot_type in FLOAT_TYPES: | ||
out[slot] = float(val) | ||
|
||
# time/duration | ||
elif slot_type in TIME_TYPES: | ||
out[slot] = { | ||
'secs': int(val.secs), | ||
'nsecs': int(val.nsecs), | ||
} | ||
|
||
# byte array | ||
elif slot_type in BYTESTREAM_TYPES: | ||
if PYTHON2: | ||
out[slot] = bytes(bytearray(val)) | ||
else: | ||
out[slot] = bytes(val) | ||
|
||
# bool array | ||
elif slot_type in BOOL_ARRAY_TYPES: | ||
out[slot] = [bool(i) for i in val] | ||
|
||
# numeric arrays | ||
elif slot_type in TAGGED_ARRAY_FORMATS: | ||
tag, fmt = TAGGED_ARRAY_FORMATS[slot_type] | ||
fmt_to_length = fmt.format(len(val)) | ||
packed = struct.pack(fmt_to_length, *val) | ||
out[slot] = Tag(tag=tag, value=packed) | ||
|
||
# array of messages | ||
elif type(val) in LIST_TYPES: | ||
out[slot] = [extract_cbor_values(i) for i in val] | ||
|
||
# message | ||
else: | ||
out[slot] = extract_cbor_values(val) | ||
|
||
return out |
24 changes: 24 additions & 0 deletions
24
rosbridge_library/src/rosbridge_library/internal/outgoing_message.py
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,24 @@ | ||
from rosbridge_library.internal.message_conversion import extract_values as extract_json_values | ||
from rosbridge_library.internal.cbor_conversion import extract_cbor_values | ||
|
||
|
||
class OutgoingMessage: | ||
"""A message wrapper for caching encoding operations.""" | ||
def __init__(self, message): | ||
self._message = message | ||
self._json_values = None | ||
self._cbor_values = None | ||
|
||
@property | ||
def message(self): | ||
return self._message | ||
|
||
def get_json_values(self): | ||
if self._json_values is None: | ||
self._json_values = extract_json_values(self._message) | ||
return self._json_values | ||
|
||
def get_cbor_values(self): | ||
if self._cbor_values is None: | ||
self._cbor_values = extract_cbor_values(self._message) | ||
return self._cbor_values |
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
Oops, something went wrong.