-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
880dde3
commit d1e626a
Showing
4 changed files
with
147 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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,69 @@ | ||
import zmq | ||
|
||
from enrgdaq.daq.base import DAQJob | ||
from enrgdaq.daq.models import DAQJobConfig | ||
|
||
|
||
class DAQJobRemoteProxyConfig(DAQJobConfig): | ||
""" | ||
Configuration for DAQJobRemoteProxy. | ||
Attributes: | ||
zmq_xsub_url (str): ZMQ xsub URL. | ||
zmq_xpub_url (str): ZMQ xpub URL. | ||
""" | ||
|
||
zmq_xsub_url: str | ||
zmq_xpub_url: str | ||
|
||
|
||
class DAQJobRemoteProxy(DAQJob): | ||
""" | ||
DAQJobRemoteProxy is a DAQJob that acts as a proxy between two ZMQ sockets. | ||
It uses zmq.proxy to forward messages between xsub and xpub. | ||
pub -> xsub -> xpub -> sub | ||
When you want to the DAQJobRemoteProxy: | ||
- For pub, connect to xsub | ||
- For sub, connect to xpub | ||
Attributes: | ||
config_type (type): Configuration type for the job. | ||
config (DAQJobRemoteProxyConfig): Configuration instance. | ||
""" | ||
|
||
config_type = DAQJobRemoteProxyConfig | ||
config: DAQJobRemoteProxyConfig | ||
|
||
def __init__(self, config: DAQJobRemoteProxyConfig, **kwargs): | ||
super().__init__(config, **kwargs) | ||
|
||
self._zmq_ctx = zmq.Context() | ||
self._xsub_sock = self._zmq_ctx.socket(zmq.XSUB) | ||
self._xsub_sock.bind(config.zmq_xsub_url) | ||
|
||
self._xpub_sock = self._zmq_ctx.socket(zmq.XPUB) | ||
self._xpub_sock.bind(config.zmq_xpub_url) | ||
|
||
self._logger.info( | ||
f"Proxying between {config.zmq_xsub_url} -> {config.zmq_xpub_url}" | ||
) | ||
|
||
def start(self): | ||
""" | ||
Start the ZMQ proxy. | ||
""" | ||
try: | ||
zmq.proxy(self._xsub_sock, self._xpub_sock) | ||
except zmq.ContextTerminated: | ||
pass | ||
|
||
def __del__(self): | ||
""" | ||
Destructor for DAQJobRemoteProxy. | ||
""" | ||
if getattr(self, "_zmq_ctx", None) is not None: | ||
self._zmq_ctx.destroy() | ||
|
||
return super().__del__() |
This file contains 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 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,51 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
|
||
import zmq | ||
|
||
from enrgdaq.daq.jobs.remote_proxy import DAQJobRemoteProxy, DAQJobRemoteProxyConfig | ||
|
||
|
||
class TestDAQJobRemoteProxy(unittest.TestCase): | ||
@patch("enrgdaq.daq.jobs.remote_proxy.zmq.Context") | ||
def setUp(self, MockZmqContext): | ||
self.mock_context = MockZmqContext.return_value | ||
self.config = DAQJobRemoteProxyConfig( | ||
daq_job_type="remote_proxy", | ||
zmq_xsub_url="tcp://localhost:5557", | ||
zmq_xpub_url="tcp://localhost:5558", | ||
) | ||
self.daq_job_remote_proxy = DAQJobRemoteProxy(self.config) | ||
|
||
def test_initialization(self): | ||
self.mock_context.socket.assert_any_call(zmq.XSUB) | ||
self.mock_context.socket.assert_any_call(zmq.XPUB) | ||
self.mock_context.socket.return_value.bind.assert_any_call( | ||
"tcp://localhost:5557" | ||
) | ||
self.mock_context.socket.return_value.bind.assert_any_call( | ||
"tcp://localhost:5558" | ||
) | ||
self.assertEqual(self.daq_job_remote_proxy.config, self.config) | ||
self.assertEqual( | ||
self.daq_job_remote_proxy._xsub_sock, self.mock_context.socket.return_value | ||
) | ||
self.assertEqual( | ||
self.daq_job_remote_proxy._xpub_sock, self.mock_context.socket.return_value | ||
) | ||
|
||
@patch("enrgdaq.daq.jobs.remote_proxy.zmq.proxy") | ||
def test_start(self, mock_zmq_proxy): | ||
self.daq_job_remote_proxy.start() | ||
mock_zmq_proxy.assert_called_once_with( | ||
self.daq_job_remote_proxy._xsub_sock, | ||
self.daq_job_remote_proxy._xpub_sock, | ||
) | ||
|
||
def test_destructor(self): | ||
del self.daq_job_remote_proxy | ||
self.mock_context.destroy.assert_called_once() | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |