-
Notifications
You must be signed in to change notification settings - Fork 150
Create DDS Transport Protocol #1144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
51 commits
Select commit
Hold shift + click to select a range
bb0e818
Create DDSPubSubBase, DDSTopic
Kaweees ee0c529
Create PickleDDS
Kaweees f5c7ee8
Fix hash/equality inconsistency in DDSTopic
Kaweees 48f548e
Add DDSMsg
Kaweees 5779803
Create DDSTransport
Kaweees 8f6f318
Add broadcast and subscribe methods to DDSTransport
Kaweees d259ac5
Create DDSService
Kaweees a8167a3
Add CycloneDDS package
Kaweees 6256dd5
Remove unnecessary attributes
Kaweees d6620c0
Add threading and serialization methods to DDSService
Kaweees 90b14bd
Ensure broadcast and subscribe methods initialize DDS if not started
Kaweees 23e3c2e
Add Transport benchmarking capabilities to CycloneDDS (#1055)
Kaweees beb875b
Fix DDS segmentation fault using bytearray for binary data storage
Kaweees 7d9390a
Refactor DDS PubSub implementation to use CycloneDDS Topic
Kaweees 60b318c
Remove DDS pickling
Kaweees cb07796
Remove unused encoding/decoding methods and use a sequence of uint8 f…
Kaweees 5be358e
Restore double-checked locking pattern for performance
Kaweees a5cc633
Encode DDS payloads as an array of bytes without string encoding to m…
Kaweees c130f64
Consolidate locking logic and remove redundant checks
Kaweees 6cdc21f
Remove DDS transport pickling method
Kaweees eaa6eb4
Removed unused DDSMsg class, use IdlStruct instead
Kaweees ef9e5fb
Enhance error handling in DDS message listener
Kaweees 3842d48
Add threading lock to DDSTransport for safe start checks
Kaweees 7853979
Thread safe reader creation after callback is registered
Kaweees dbfee76
Add DDS transport documentation and example usage
Kaweees 61c4fae
Move DDS benchmarks to testdata.py
Kaweees b02301c
Move _participant singleton from a class variable to a module-level v…
Kaweees 9f949f6
Remove list casting for dds message benchmarks
Kaweees aefec92
Merge remote-tracking branch 'origin/dev' into miguel/dds_transport
Kaweees 94686a9
CI code cleanup
Kaweees 22ba09c
Add qos configutation option for DDS transport
Kaweees 201a734
Update buffer sizes in system configurator
Kaweees fcc4d13
Add high-throughput and reliable QoS presets for DDS PubSub
Kaweees bf6196f
Add back DDS testcases generation
Kaweees 8da904d
Rename typename to data_type and use participant property
Kaweees 2a4bc90
Fix DDS Transport documentation example
Kaweees 0e5c2f5
Add locks to dds start/stop
Kaweees cfea1d0
CI code cleanup
Kaweees 1347ac2
Add thread safety to _DDSMessageListener.
Kaweees f9fdd1a
Update dimos/protocol/pubsub/ddspubsub.py
Kaweees 225eb68
Update dimos/protocol/pubsub/ddspubsub.py
Kaweees 9f763ce
Update dimos/protocol/pubsub/ddspubsub.py
Kaweees 79060be
Refactor DDS transport and service classes for improved thread safety…
Kaweees 7e06cb1
CI code cleanup
Kaweees 2a5bfa3
Merge branch 'dev' into miguel/dds_transport
Kaweees cbf2b92
CI code cleanup
Kaweees ba0adcd
Remove duplicate imports
Kaweees d720b2c
Improve resource cleanup for DDS class
Kaweees 7ebfe9b
Remove extra imports
Kaweees 5031a51
Enhance type hints
Kaweees f3b7352
Refactor DDS transport methods to ensure proper start/stop logic
Kaweees File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| import dimos.protocol.pubsub.ddspubsub as dds | ||
| import dimos.protocol.pubsub.impl.lcmpubsub as lcm | ||
| from dimos.protocol.pubsub.impl.memory import Memory | ||
| from dimos.protocol.pubsub.spec import PubSub | ||
|
|
||
| __all__ = [ | ||
| "Memory", | ||
| "PubSub", | ||
| "dds", | ||
| "lcm", | ||
| ] |
This file contains hidden or 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 hidden or 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,178 @@ | ||
| # Copyright 2025-2026 Dimensional Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
| import threading | ||
| from typing import TYPE_CHECKING, Any, TypeAlias | ||
|
|
||
| from cyclonedds.core import Listener | ||
| from cyclonedds.pub import DataWriter as DDSDataWriter | ||
| from cyclonedds.qos import Policy, Qos | ||
| from cyclonedds.sub import DataReader as DDSDataReader | ||
| from cyclonedds.topic import Topic as DDSTopic | ||
|
|
||
| from dimos.protocol.pubsub.spec import PubSub | ||
| from dimos.protocol.service.ddsservice import DDSService | ||
| from dimos.utils.logging_config import setup_logger | ||
|
|
||
| if TYPE_CHECKING: | ||
| from cyclonedds.idl import IdlStruct | ||
|
|
||
| logger = setup_logger() | ||
|
|
||
|
|
||
| # High-throughput QoS preset | ||
| HIGH_THROUGHPUT_QOS = Qos( | ||
| Policy.Reliability.BestEffort, | ||
| Policy.History.KeepLast(depth=1), | ||
| Policy.Durability.Volatile, | ||
| ) | ||
|
|
||
| # Reliable QoS preset | ||
| RELIABLE_QOS = Qos( | ||
| Policy.Reliability.Reliable(max_blocking_time=0), | ||
| Policy.History.KeepLast(depth=5000), | ||
| Policy.Durability.Volatile, | ||
| ) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Topic: | ||
| """Represents a DDS topic.""" | ||
|
|
||
| name: str | ||
| data_type: type[IdlStruct] | ||
|
|
||
| def __str__(self) -> str: | ||
| return f"{self.name}#{self.data_type.__name__}" | ||
|
|
||
|
|
||
| MessageCallback: TypeAlias = Callable[[Any, Topic], None] | ||
|
|
||
|
|
||
| class _DDSMessageListener(Listener): | ||
| """Listener for DataReader that dispatches messages to callbacks.""" | ||
|
|
||
| __slots__ = ("_callbacks", "_lock", "_topic") | ||
|
|
||
| def __init__(self, topic: Topic) -> None: | ||
| super().__init__() | ||
| self._topic = topic | ||
| self._callbacks: tuple[MessageCallback, ...] = () | ||
| self._lock = threading.Lock() | ||
|
|
||
| def add_callback(self, callback: MessageCallback) -> None: | ||
| """Add a callback to the listener.""" | ||
| with self._lock: | ||
| self._callbacks = (*self._callbacks, callback) | ||
|
|
||
| def remove_callback(self, callback: MessageCallback) -> None: | ||
| """Remove a callback from the listener.""" | ||
| with self._lock: | ||
| self._callbacks = tuple(cb for cb in self._callbacks if cb is not callback) | ||
|
|
||
| def on_data_available(self, reader: DDSDataReader[Any]) -> None: | ||
| """Called when data is available on the reader.""" | ||
| try: | ||
| samples = reader.take() | ||
| except Exception as e: | ||
| logger.error(f"Error reading from topic {self._topic}: {e}", exc_info=True) | ||
| return | ||
| for sample in samples: | ||
| if sample is not None: | ||
| for callback in self._callbacks: | ||
| try: | ||
| callback(sample, self._topic) | ||
| except Exception as e: | ||
| logger.error(f"Callback error on topic {self._topic}: {e}", exc_info=True) | ||
|
|
||
|
|
||
| class DDS(DDSService, PubSub[Topic, Any]): | ||
| def __init__(self, qos: Qos | None = None, **kwargs: Any) -> None: | ||
| super().__init__(**kwargs) | ||
| self._qos = qos | ||
| self._writers: dict[Topic, DDSDataWriter[Any]] = {} | ||
| self._writer_lock = threading.Lock() | ||
| self._readers: dict[Topic, DDSDataReader[Any]] = {} | ||
| self._reader_lock = threading.Lock() | ||
| self._listeners: dict[Topic, _DDSMessageListener] = {} | ||
|
|
||
| @property | ||
| def qos(self) -> Qos | None: | ||
| """Get the QoS settings.""" | ||
| return self._qos | ||
|
|
||
| def _get_writer(self, topic: Topic) -> DDSDataWriter[Any]: | ||
| """Get or create a DataWriter for the given topic.""" | ||
| with self._writer_lock: | ||
| if topic not in self._writers: | ||
| dds_topic = DDSTopic(self.participant, topic.name, topic.data_type) | ||
| self._writers[topic] = DDSDataWriter(self.participant, dds_topic, qos=self._qos) | ||
| return self._writers[topic] | ||
|
|
||
| def publish(self, topic: Topic, message: Any) -> None: | ||
| """Publish a message to a DDS topic.""" | ||
| writer = self._get_writer(topic) | ||
| try: | ||
| writer.write(message) | ||
| except Exception as e: | ||
| logger.error(f"Error publishing to topic {topic}: {e}", exc_info=True) | ||
|
|
||
| def _get_listener(self, topic: Topic) -> _DDSMessageListener: | ||
| """Get or create a listener and reader for the given topic.""" | ||
| with self._reader_lock: | ||
| if topic not in self._readers: | ||
| dds_topic = DDSTopic(self.participant, topic.name, topic.data_type) | ||
| listener = _DDSMessageListener(topic) | ||
| self._readers[topic] = DDSDataReader( | ||
| self.participant, dds_topic, qos=self._qos, listener=listener | ||
| ) | ||
| self._listeners[topic] = listener | ||
| return self._listeners[topic] | ||
|
|
||
| def subscribe(self, topic: Topic, callback: MessageCallback) -> Callable[[], None]: | ||
| """Subscribe to a DDS topic with a callback.""" | ||
| listener = self._get_listener(topic) | ||
| listener.add_callback(callback) | ||
| return lambda: self._unsubscribe_callback(topic, callback) | ||
|
|
||
| def _unsubscribe_callback(self, topic: Topic, callback: MessageCallback) -> None: | ||
| """Unsubscribe a callback from a topic.""" | ||
| with self._reader_lock: | ||
| listener = self._listeners.get(topic) | ||
| if listener: | ||
| listener.remove_callback(callback) | ||
|
|
||
| def stop(self) -> None: | ||
| """Stop the DDS service and clean up resources.""" | ||
| with self._reader_lock: | ||
| self._readers.clear() | ||
| self._listeners.clear() | ||
| with self._writer_lock: | ||
| self._writers.clear() | ||
| super().stop() | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "DDS", | ||
| "HIGH_THROUGHPUT_QOS", | ||
| "RELIABLE_QOS", | ||
| "MessageCallback", | ||
| "Policy", | ||
| "Qos", | ||
| "Topic", | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.