-
Notifications
You must be signed in to change notification settings - Fork 550
[DRAFT] Create Zenoh Transport Protocol #1296
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| # 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 dimos.protocol.pubsub.spec import PubSub | ||
| from dimos.protocol.service.zenohservice import ZenohService | ||
| from dimos.utils.logging_config import setup_logger | ||
|
|
||
| if TYPE_CHECKING: | ||
| import zenoh | ||
|
|
||
| logger = setup_logger() | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Topic: | ||
| """Represents a Zenoh topic (key expression).""" | ||
|
|
||
| name: str | ||
|
|
||
| def __str__(self) -> str: | ||
| return self.name | ||
|
|
||
|
|
||
| MessageCallback: TypeAlias = Callable[[Any, Topic], None] | ||
|
|
||
|
|
||
| class ZenohPubSub(ZenohService, PubSub[Topic, Any]): | ||
| def __init__(self, **kwargs: Any) -> None: | ||
| super().__init__(**kwargs) | ||
| self._publishers: dict[Topic, zenoh.Publisher] = {} | ||
| self._publisher_lock = threading.Lock() | ||
| self._subscribers: list[zenoh.Subscriber] = [] | ||
| self._subscriber_lock = threading.Lock() | ||
|
|
||
| def _get_publisher(self, topic: Topic) -> zenoh.Publisher: | ||
| """Get or create a Publisher for the given topic.""" | ||
| with self._publisher_lock: | ||
| if topic not in self._publishers: | ||
| self._publishers[topic] = self.session.declare_publisher(topic.name) | ||
| return self._publishers[topic] | ||
|
|
||
| def publish(self, topic: Topic, message: bytes | str) -> None: | ||
| """Publish a message to a Zenoh topic.""" | ||
| publisher = self._get_publisher(topic) | ||
| try: | ||
| publisher.put(message) | ||
| except Exception as e: | ||
| logger.error(f"Error publishing to topic {topic}: {e}", exc_info=True) | ||
|
|
||
| def subscribe(self, topic: Topic, callback: MessageCallback) -> Callable[[], None]: | ||
| """Subscribe to a Zenoh topic with a callback. | ||
|
|
||
| Each call declares its own Zenoh subscriber (Zenoh spawns a | ||
| background thread per callback handler). Unsubscribe undeclares it. | ||
| """ | ||
|
|
||
| def on_sample(sample: zenoh.Sample) -> None: | ||
| callback(sample.payload.to_bytes(), topic) | ||
|
|
||
| sub = self.session.declare_subscriber(topic.name, on_sample) | ||
| with self._subscriber_lock: | ||
| self._subscribers.append(sub) | ||
|
|
||
| def unsubscribe() -> None: | ||
| sub.undeclare() | ||
| with self._subscriber_lock: | ||
| try: | ||
| self._subscribers.remove(sub) | ||
| except ValueError: | ||
| pass | ||
|
|
||
| return unsubscribe | ||
|
|
||
| def stop(self) -> None: | ||
| """Stop the Zenoh pub/sub and clean up resources.""" | ||
| with self._subscriber_lock: | ||
| for subscriber in self._subscribers: | ||
| subscriber.undeclare() | ||
| self._subscribers.clear() | ||
| with self._publisher_lock: | ||
| for publisher in self._publishers.values(): | ||
| publisher.undeclare() | ||
| self._publishers.clear() | ||
| super().stop() | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "MessageCallback", | ||
| "Topic", | ||
| "ZenohPubSub", | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| # 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 dataclasses import dataclass, field | ||
| import json | ||
| import threading | ||
| from typing import Any | ||
|
|
||
| import zenoh | ||
|
|
||
| from dimos.protocol.service.spec import Service | ||
| from dimos.utils.logging_config import setup_logger | ||
|
|
||
| logger = setup_logger() | ||
|
|
||
| _sessions: dict[str, zenoh.Session] = {} | ||
| _sessions_lock = threading.Lock() | ||
|
|
||
|
|
||
| @dataclass | ||
| class ZenohConfig: | ||
| """Configuration for Zenoh service.""" | ||
|
|
||
| mode: str = "peer" | ||
| connect: list[str] = field(default_factory=list) | ||
| listen: list[str] = field(default_factory=list) | ||
|
|
||
| @property | ||
| def session_key(self) -> str: | ||
| """Produce a hashable key for singleton session lookup.""" | ||
| return f"{self.mode}|{json.dumps(sorted(self.connect))}|{json.dumps(sorted(self.listen))}" | ||
|
|
||
|
|
||
| class ZenohService(Service[ZenohConfig]): | ||
| default_config = ZenohConfig | ||
|
|
||
| def __init__(self, **kwargs: Any) -> None: | ||
| super().__init__(**kwargs) | ||
|
|
||
| def start(self) -> None: | ||
| """Start the Zenoh service.""" | ||
| key = self.config.session_key | ||
| with _sessions_lock: | ||
| if key not in _sessions: | ||
| config = zenoh.Config() | ||
| config.insert_json5("mode", json.dumps(self.config.mode)) | ||
| if self.config.connect: | ||
| config.insert_json5("connect/endpoints", json.dumps(self.config.connect)) | ||
| if self.config.listen: | ||
| config.insert_json5("listen/endpoints", json.dumps(self.config.listen)) | ||
| _sessions[key] = zenoh.open(config) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Zenoh sessions in Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| logger.info(f"Zenoh service started in {self.config.mode} mode") | ||
| super().start() | ||
|
|
||
| def stop(self) -> None: | ||
| """Stop the Zenoh service.""" | ||
| super().stop() | ||
|
|
||
| @property | ||
| def session(self) -> zenoh.Session: | ||
| """Get the Zenoh Session instance for this service's config.""" | ||
| key = self.config.session_key | ||
| if key not in _sessions: | ||
| raise RuntimeError("Zenoh session not initialized") | ||
| return _sessions[key] | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "ZenohConfig", | ||
| "ZenohService", | ||
| ] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ dependencies = [ | |
| # Transport Protocols | ||
| "dimos-lcm", | ||
| "PyTurboJPEG==1.8.2", | ||
| "eclipse-zenoh>=1.7.2", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check that
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check if Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| # Core | ||
| "numpy>=1.26.4", | ||
| "scipy>=1.15.1", | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Zenoh sessions in
_sessionsdict are never closed - sessions accumulate without cleanup. Consider adding reference counting or explicit cleanup in stop().