-
Notifications
You must be signed in to change notification settings - Fork 3
Add ZeroMQ pub/sub transport support with explicit import structure #19
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
Draft
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/fix-18
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
46c5c74
Initial plan
Copilot 2900dd6
Implement ZeroMQ transport with unit tests
Copilot c632b72
Complete ZeroMQ transport with integration tests and examples
Copilot 044ceed
Merge main branch - resolve conflict in setup.py
Copilot 9ea3789
Address review feedback: remove ZeroMQ from main init, update error m…
Copilot 086daac
Merge branch 'main' of https://github.com/compas-dev/compas_eve into …
Copilot 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,255 @@ | ||
| """ | ||
| ******************************************************************************** | ||
| compas_eve.zeromq | ||
| ******************************************************************************** | ||
| .. currentmodule:: compas_eve.zeromq | ||
| Classes | ||
| ======= | ||
| .. autosummary:: | ||
| :toctree: generated/ | ||
| :nosignatures: | ||
| ZeroMQTransport | ||
| """ | ||
|
|
||
| from ..core import Transport | ||
| from ..event_emitter import EventEmitterMixin | ||
|
|
||
| try: | ||
| import zmq | ||
| except ImportError: | ||
| zmq = None | ||
|
|
||
| __all__ = ["ZeroMQTransport"] | ||
|
|
||
|
|
||
| class ZeroMQTransport(Transport, EventEmitterMixin): | ||
| """ZeroMQ transport allows sending and receiving messages using ZeroMQ pub/sub sockets. | ||
| Parameters | ||
| ---------- | ||
| endpoint : str | ||
| Endpoint for the pub/sub communication, e.g. ``tcp://localhost:5555`` or ``inproc://test``. | ||
| bind_subscriber : bool, optional | ||
| If True, the subscriber socket will bind to the endpoint and publisher will connect. | ||
| If False, the publisher will bind to the endpoint and subscriber will connect. | ||
| Defaults to True for most use cases. | ||
| """ | ||
|
|
||
| def __init__(self, endpoint, bind_subscriber=True, *args, **kwargs): | ||
| if zmq is None: | ||
| raise ImportError("pyzmq is required for ZeroMQ transport. Please install it with: pip install pyzmq") | ||
|
||
|
|
||
| super(ZeroMQTransport, self).__init__(*args, **kwargs) | ||
|
|
||
| self.endpoint = endpoint | ||
| self.bind_subscriber = bind_subscriber | ||
| self._is_connected = False | ||
| self._local_callbacks = {} | ||
|
|
||
| # Create ZeroMQ context and sockets | ||
| self.context = zmq.Context() | ||
| self.pub_socket = self.context.socket(zmq.PUB) | ||
| self.sub_socket = self.context.socket(zmq.SUB) | ||
|
|
||
| # Configure sockets based on bind_subscriber setting | ||
| if self.bind_subscriber: | ||
| # Subscriber binds, publisher connects - good for many publishers, few subscribers | ||
| self.sub_socket.bind(self.endpoint) | ||
| self.pub_socket.connect(self.endpoint) | ||
| else: | ||
| # Publisher binds, subscriber connects - good for one publisher, many subscribers | ||
| self.pub_socket.bind(self.endpoint) | ||
| self.sub_socket.connect(self.endpoint) | ||
|
|
||
| # Set up polling for subscriber | ||
| self.poller = zmq.Poller() | ||
| self.poller.register(self.sub_socket, zmq.POLLIN) | ||
|
|
||
| # Mark as connected (ZeroMQ doesn't have explicit connection state) | ||
| self._is_connected = True | ||
|
|
||
| # Start polling thread for incoming messages | ||
| import threading | ||
| self._polling = True | ||
| self._poll_thread = threading.Thread(target=self._poll_messages) | ||
| self._poll_thread.daemon = True | ||
| self._poll_thread.start() | ||
|
|
||
| # Emit ready event | ||
| self.emit("ready") | ||
|
|
||
| def close(self): | ||
| """Close the ZeroMQ sockets and context.""" | ||
| self._polling = False | ||
| if hasattr(self, '_poll_thread'): | ||
| self._poll_thread.join(timeout=1) | ||
|
|
||
| self.pub_socket.close() | ||
| self.sub_socket.close() | ||
| self.context.term() | ||
|
|
||
| def _poll_messages(self): | ||
| """Poll for incoming messages in a separate thread.""" | ||
| while self._polling: | ||
| try: | ||
| # Poll with timeout to allow thread termination | ||
| socks = dict(self.poller.poll(100)) # 100ms timeout | ||
| if self.sub_socket in socks: | ||
| # Receive topic and message | ||
| topic_bytes = self.sub_socket.recv(zmq.NOBLOCK) | ||
| message_bytes = self.sub_socket.recv(zmq.NOBLOCK) | ||
|
|
||
| topic_name = topic_bytes.decode('utf-8') | ||
| message_str = message_bytes.decode('utf-8') | ||
|
|
||
| # Emit the message event | ||
| event_key = "event:{}".format(topic_name) | ||
| self.emit(event_key, message_str) | ||
|
|
||
| except zmq.Again: | ||
| # No message available, continue polling | ||
| continue | ||
| except Exception as e: | ||
| # Emit error but continue polling | ||
| self.emit("error", e) | ||
|
|
||
| def on_ready(self, callback): | ||
| """Allows to hook-up to the event triggered when the transport is ready. | ||
| Parameters | ||
| ---------- | ||
| callback : function | ||
| Function to invoke when the connection is established. | ||
| """ | ||
| if self._is_connected: | ||
| callback() | ||
| else: | ||
| self.once("ready", callback) | ||
|
|
||
| def publish(self, topic, message): | ||
| """Publish a message to a topic. | ||
| Parameters | ||
| ---------- | ||
| topic : :class:`Topic` | ||
| Instance of the topic to publish to. | ||
| message : :class:`Message` | ||
| Instance of the message to publish. | ||
| """ | ||
| def _callback(**kwargs): | ||
| json_message = topic._message_to_json(message) | ||
|
|
||
| # Send topic and message as separate frames | ||
| self.pub_socket.send_string(topic.name, zmq.SNDMORE) | ||
| self.pub_socket.send_string(json_message) | ||
|
|
||
| self.on_ready(_callback) | ||
|
|
||
| def subscribe(self, topic, callback): | ||
| """Subscribe to a topic. | ||
| Every time a new message is received on the topic, the callback will be invoked. | ||
| Parameters | ||
| ---------- | ||
| topic : :class:`Topic` | ||
| Instance of the topic to subscribe to. | ||
| callback : function | ||
| Callback to invoke whenever a new message arrives. The callback should | ||
| receive only one `msg` argument, e.g. ``lambda msg: print(msg)``. | ||
| Returns | ||
| ------- | ||
| str | ||
| Returns an identifier of the subscription. | ||
| """ | ||
| event_key = "event:{}".format(topic.name) | ||
| subscribe_id = "{}:{}".format(event_key, id(callback)) | ||
|
|
||
| def _local_callback(message_str): | ||
| msg = topic._message_from_json(message_str) | ||
| callback(msg) | ||
|
|
||
| def _subscribe_callback(**kwargs): | ||
| # Subscribe to the topic on ZeroMQ socket | ||
| self.sub_socket.setsockopt_string(zmq.SUBSCRIBE, topic.name) | ||
|
|
||
| # Register local callback for this topic | ||
| self.on(event_key, _local_callback) | ||
|
|
||
| self._local_callbacks[subscribe_id] = _local_callback | ||
|
|
||
| self.on_ready(_subscribe_callback) | ||
|
|
||
| return subscribe_id | ||
|
|
||
| def advertise(self, topic): | ||
| """Announce this code will publish messages to the specified topic. | ||
| This call has no effect on this transport implementation. | ||
| Parameters | ||
| ---------- | ||
| topic : :class:`Topic` | ||
| Instance of the topic to advertise. | ||
| Returns | ||
| ------- | ||
| str | ||
| Advertising identifier. | ||
| """ | ||
| advertise_id = "advertise:{}:{}".format(topic.name, self.id_counter) | ||
| # ZeroMQ does not need explicit advertising | ||
| return advertise_id | ||
|
|
||
| def unadvertise(self, topic): | ||
| """Announce that this code will stop publishing messages to the specified topic. | ||
| This call has no effect on this transport implementation. | ||
| Parameters | ||
| ---------- | ||
| topic : :class:`Topic` | ||
| Instance of the topic to stop publishing messages to. | ||
| """ | ||
| pass | ||
|
|
||
| def unsubscribe_by_id(self, subscribe_id): | ||
| """Unsubscribe from the specified topic based on the subscription id. | ||
| Parameters | ||
| ---------- | ||
| subscribe_id : str | ||
| Identifier of the subscription. | ||
| """ | ||
| ev_type, topic_name, _callback_id = subscribe_id.split(":") | ||
| event_key = "{}:{}".format(ev_type, topic_name) | ||
|
|
||
| callback = self._local_callbacks[subscribe_id] | ||
| self.off(event_key, callback) | ||
|
|
||
| # Unsubscribe from ZeroMQ socket | ||
| self.sub_socket.setsockopt_string(zmq.UNSUBSCRIBE, topic_name) | ||
|
|
||
| del self._local_callbacks[subscribe_id] | ||
|
|
||
| def unsubscribe(self, topic): | ||
| """Unsubscribe from the specified topic. | ||
| Parameters | ||
| ---------- | ||
| topic : :class:`Topic` | ||
| Instance of the topic to unsubscribe from. | ||
| """ | ||
| # Unsubscribe from ZeroMQ socket | ||
| self.sub_socket.setsockopt_string(zmq.UNSUBSCRIBE, topic.name) | ||
|
|
||
| # Remove all local listeners for this topic | ||
| event_key = "event:{}".format(topic.name) | ||
| self.remove_all_listeners(event_key) | ||
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.
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.
Remove all importing of ZeroMQTransport directly into the main
__init__, this should only be an explicit import action by the userThere 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.
Removed all ZeroMQTransport imports from the main init.py. ZeroMQTransport is now only available via explicit import from
compas_eve.zeromq. Commit: 9ea3789