Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions libby/cyclonedds_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ReadCondition, SampleState, ViewState, WaitSet)
from cyclonedds.domain import Domain, DomainParticipant
from cyclonedds.idl import IdlStruct, types
from cyclonedds.idl._machinery import BytesMachine
from cyclonedds.pub import DataWriter, Publisher
from cyclonedds.sub import DataReader, Subscriber
from cyclonedds.topic import Topic
Expand Down Expand Up @@ -61,6 +62,21 @@ class DdsEnvelope(IdlStruct, typename="libby.Envelope"): # pylint: disable=too-
binary: types.sequence[types.uint8]


def _write_binary_in_one_copy() -> None:
"""Serialize ``binary`` with ``BytesMachine``, which emits the same CDR as ``sequence<octet>``.

cyclonedds-python packs an octet sequence element by element (8.75 ms per
512 KB) and cannot build a DDS type object for a ``bytes`` field.
"""
idl = DdsEnvelope.__idl__ # pylint: disable=no-member # set by IdlStruct's metaclass
idl.populate()
for machine in (idl.v1_machine, idl.v2_machine):
machine.members_machines["binary"] = BytesMachine()


_write_binary_in_one_copy()


def to_sample(msg: Message) -> DdsEnvelope:
"""Encode a bamboo message as a DDS sample."""
env = msg.env
Expand Down Expand Up @@ -93,8 +109,7 @@ def to_message(sample: DdsEnvelope) -> Message:
traceparent=sample.traceparent or None,
tracestate=sample.tracestate or None,
)
# An octet sequence reads back as a list of ints
return Message(envelope, bytes(sample.binary) if sample.binary else None)
return Message(envelope, sample.binary or None)


def _qos(*partitions: str) -> Qos:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_cyclonedds_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from bamboo import Envelope, Message, MessageBuilder

try:
from cyclonedds.idl import IdlStruct
from cyclonedds.idl._machinery import BytesMachine
from libby.cyclonedds_transport import (CycloneDdsTransport, DdsEnvelope, _matched_readers,
to_message, to_sample)
except ImportError: # cyclonedds is an optional extra
Expand Down Expand Up @@ -61,6 +63,23 @@ def test_sample_type_covers_every_envelope_field(self):
sample_fields = {field.name for field in dataclasses.fields(DdsEnvelope)}
self.assertEqual(sample_fields, envelope_fields | {"binary"})

def test_binary_keeps_the_octet_sequence_wire_format(self):
"""Write ``binary`` in one copy, byte-identical to a plain ``sequence<octet>``.

Fails if a cyclonedds upgrade moves the internals the swap relies on.
"""
idl = DdsEnvelope.__idl__ # pylint: disable=no-member # set by IdlStruct's metaclass
self.assertIsInstance(idl.v2_machine.members_machines["binary"], BytesMachine)
# Same fields, built the default way, so binary uses the element-wise machine
reference = dataclasses.dataclass(type(
"ReferenceEnvelope", (IdlStruct,),
{"__annotations__": dict(DdsEnvelope.__annotations__)},
typename="test.ReferenceEnvelope"))
sample = to_sample(MessageBuilder("hsfei.client").pub("frames", {})
.binary(bytes(range(256)) * 4).to(None).build())
self.assertEqual(sample.serialize(),
reference(**dataclasses.asdict(sample)).serialize())


@unittest.skipIf(CycloneDdsTransport is None, "cyclonedds not installed")
class DeliveryTests(unittest.TestCase):
Expand Down
Loading