-
Notifications
You must be signed in to change notification settings - Fork 159
Create DDS Transport Protocol #1174
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
1429cad
Add cyclonedds package
Kaweees 0538ab7
CI code cleanup
Kaweees 6eb1286
Create DDS transport
Kaweees b003a0c
Fix broken DDS import path in transports documentation
Kaweees bb2adf8
Add threading import to transport module
Kaweees f98ee87
Refactor DDS service to support multiple DomainParticipants
Kaweees eaad360
Improve DDSTransport type hinting
Kaweees 870f450
Update type hint for DDSTransport.subscribe method
Kaweees 0246eae
Move cyclonedds to separate index
Kaweees f83eebf
Move HIGH_THROUGHPUT_QOS and RELIABLE_QOS into testdata
Kaweees 978f62f
Make DDS an optional transport and update installation documentation
Kaweees af5af9f
Documentation nitpick
Kaweees f189b8f
Don't execute ddspubsub in transport testcases if dds is missing.
Kaweees f0991a6
Add cyclonedds to mypy type checking overrides
Kaweees 8c68e31
Merge branch 'dev' into miguel/dds_transport
Kaweees 7ff72f6
Refactor: type ignore for DDSBenchmarkData and _DDSMessageListener
Kaweees 34820bf
Refactor: combine apt-get update and install commands to prevent stal…
Kaweees 88e0c35
Refactor: Update Dockerfiles to replace 'libgl1-mesa-glx' with 'libgl…
Kaweees ffb2726
pin langchain for now
paul-nechifor 5ca9949
resolve merge
spomichter d689796
uv lock
spomichter 2ddf6cf
resolve merge conflicts
spomichter 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 |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| # 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() | ||
|
|
||
|
|
||
| @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): # type: ignore[misc] | ||
| """Listener for DataReader that dispatches messages to callbacks.""" | ||
|
|
||
| __slots__ = ("_callbacks", "_lock", "_topic") | ||
|
|
||
| def __init__(self, topic: Topic) -> None: | ||
| super().__init__() # type: ignore[no-untyped-call] | ||
| 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() | ||
Kaweees marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| super().stop() | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "DDS", | ||
| "MessageCallback", | ||
| "Policy", | ||
| "Qos", | ||
| "Topic", | ||
| ] | ||
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
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.