-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move everything zmq related to it's own backend
- Loading branch information
Showing
11 changed files
with
290 additions
and
205 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
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,22 @@ | ||
from posttroll.address_receiver import default_publish_port | ||
from posttroll.backends.zmq import get_context | ||
from zmq import REP, LINGER | ||
|
||
class SimpleReceiver(object): | ||
|
||
""" Simple listing on port for address messages.""" | ||
|
||
def __init__(self, port=None): | ||
self._port = port or default_publish_port | ||
self._socket = get_context().socket(REP) | ||
self._socket.bind("tcp://*:" + str(port)) | ||
|
||
def __call__(self): | ||
message = self._socket.recv_string() | ||
self._socket.send_string("ok") | ||
return message, None | ||
|
||
def close(self): | ||
"""Close the receiver.""" | ||
self._socket.setsockopt(LINGER, 1) | ||
self._socket.close() |
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 threading | ||
from posttroll.backends.zmq import get_context | ||
|
||
from zmq import LINGER, NOBLOCK, REQ, ZMQError | ||
|
||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class UnsecureZMQDesignatedReceiversSender: | ||
"""Sends message to multiple *receivers* on *port*.""" | ||
|
||
def __init__(self, default_port, receivers): | ||
self.default_port = default_port | ||
|
||
self.receivers = receivers | ||
self._shutdown_event = threading.Event() | ||
|
||
def __call__(self, data): | ||
"""Send data.""" | ||
for receiver in self.receivers: | ||
self._send_to_address(receiver, data) | ||
|
||
def _send_to_address(self, address, data, timeout=10): | ||
"""Send data to *address* and *port* without verification of response.""" | ||
# Socket to talk to server | ||
socket = get_context().socket(REQ) | ||
try: | ||
socket.setsockopt(LINGER, timeout * 1000) | ||
if address.find(":") == -1: | ||
socket.connect("tcp://%s:%d" % (address, self.default_port)) | ||
else: | ||
socket.connect("tcp://%s" % address) | ||
socket.send_string(data) | ||
while not self._shutdown_event.is_set(): | ||
try: | ||
message = socket.recv_string(NOBLOCK) | ||
except ZMQError: | ||
self._shutdown_event.wait(.1) | ||
continue | ||
if message != "ok": | ||
logger.warn("invalid acknowledge received: %s" % message) | ||
break | ||
|
||
finally: | ||
socket.close() | ||
|
||
def close(self): | ||
"""Close the sender.""" | ||
self._shutdown_event.set() |
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,90 @@ | ||
"""ZMQ implexentation of ns.""" | ||
|
||
import logging | ||
from threading import Lock | ||
|
||
from zmq import LINGER, NOBLOCK, POLLIN, REP, REQ, Poller | ||
|
||
from posttroll.backends.zmq import get_context | ||
from posttroll.message import Message | ||
from posttroll.ns import PORT, get_active_address | ||
|
||
logger = logging.getLogger("__name__") | ||
|
||
nslock = Lock() | ||
|
||
|
||
def unsecure_zmq_get_pub_address(name, timeout=10, nameserver="localhost"): | ||
"""Get the address of the publisher. | ||
For a given publisher *name* from the nameserver on *nameserver* (localhost by default). | ||
""" | ||
# Socket to talk to server | ||
socket = get_context().socket(REQ) | ||
try: | ||
socket.setsockopt(LINGER, int(timeout * 1000)) | ||
socket.connect("tcp://" + nameserver + ":" + str(PORT)) | ||
logger.debug("Connecting to %s", | ||
"tcp://" + nameserver + ":" + str(PORT)) | ||
poller = Poller() | ||
poller.register(socket, POLLIN) | ||
|
||
message = Message("/oper/ns", "request", {"service": name}) | ||
socket.send_string(str(message)) | ||
|
||
# Get the reply. | ||
sock = poller.poll(timeout=timeout * 1000) | ||
if sock: | ||
if sock[0][0] == socket: | ||
message = Message.decode(socket.recv_string(NOBLOCK)) | ||
return message.data | ||
else: | ||
raise TimeoutError("Didn't get an address after %d seconds." | ||
% timeout) | ||
finally: | ||
socket.close() | ||
|
||
|
||
class UnsecureZMQNameServer: | ||
"""The name server.""" | ||
|
||
def __init__(self): | ||
"""Set up the nameserver.""" | ||
self.loop = True | ||
self.listener = None | ||
|
||
def run(self, arec): | ||
"""Run the listener and answer to requests.""" | ||
port = PORT | ||
|
||
try: | ||
with nslock: | ||
self.listener = get_context().socket(REP) | ||
self.listener.bind("tcp://*:" + str(port)) | ||
logger.debug("Listening on port %s", str(port)) | ||
poller = Poller() | ||
poller.register(self.listener, POLLIN) | ||
while self.loop: | ||
with nslock: | ||
socks = dict(poller.poll(1000)) | ||
if socks: | ||
if socks.get(self.listener) == POLLIN: | ||
msg = self.listener.recv_string() | ||
else: | ||
continue | ||
logger.debug("Replying to request: " + str(msg)) | ||
msg = Message.decode(msg) | ||
active_address = get_active_address(msg.data["service"], arec) | ||
self.listener.send_unicode(str(active_address)) | ||
except KeyboardInterrupt: | ||
# Needed to stop the nameserver. | ||
pass | ||
finally: | ||
self.stop() | ||
|
||
def stop(self): | ||
"""Stop the name server.""" | ||
self.listener.setsockopt(LINGER, 1) | ||
self.loop = False | ||
with nslock: | ||
self.listener.close() |
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
Oops, something went wrong.