def __init__(self, srv_address, context=None, retry_timeout=-1, **kwargs): # Get parameters srv_param = parse_address(srv_address) cli_param = dict() for k in ['protocol', 'host', 'port']: cli_param[k] = kwargs.pop(k, srv_param[k]) context = context or _global_context # Create new address for the frontend if cli_param['protocol'] in ['inproc', 'ipc']: cli_param['host'] = str(uuid.uuid4()) cli_address = format_address(cli_param['protocol'], cli_param['host']) self.cli_socket = context.socket(zmq.ROUTER) self.cli_address = bind_socket(self.cli_socket, cli_address, retry_timeout=retry_timeout) self.cli_socket.setsockopt(zmq.LINGER, 0) CommBase.register_comm('ZMQComm', 'ROUTER_server_' + self.cli_address, self.cli_socket) # Bind backend self.srv_socket = context.socket(zmq.DEALER) self.srv_socket.setsockopt(zmq.LINGER, 0) self.srv_address = bind_socket(self.srv_socket, srv_address, retry_timeout=retry_timeout) CommBase.register_comm('ZMQComm', 'DEALER_server_' + self.srv_address, self.srv_socket) # Set up poller # self.poller = zmq.Poller() # self.poller.register(frontend, zmq.POLLIN) self.reply_socket = None # Set name super(ZMQProxy, self).__init__(self.srv_address, self.cli_address, **kwargs) self.name = 'ZMQProxy.%s' % srv_address
def backlog_thread(self): r"""tools.CisThread: Thread that will handle sinding or receiving backlogged messages.""" if self._backlog_thread is None: if self.direction == 'send': self._backlog_thread = CommBase.CommThreadLoop( self, target=self.run_backlog_send, suffix='SendBacklog') else: self._backlog_thread = CommBase.CommThreadLoop( self, target=self.run_backlog_recv, suffix='RecvBacklog') return self._backlog_thread
def close_sockets(self): r"""Close the sockets.""" self.debug('Closing sockets') if self.cli_socket: self.cli_socket.close() self.cli_socket = None if self.srv_socket: self.srv_socket.close() self.srv_socket = None CommBase.unregister_comm('ZMQComm', 'ROUTER_server_' + self.cli_address) CommBase.unregister_comm('ZMQComm', 'DEALER_server_' + self.srv_address)
def remove_queue(mq): r"""Remove a sysv_ipc.MessageQueue and unregister it. Args: mq (:class:`sysv_ipc.MessageQueue`) Message queue. Raises: KeyError: If the provided queue is not registered. """ key = str(mq.key) if not CommBase.is_registered('IPCComm', key): raise KeyError("Queue not registered.") CommBase.unregister_comm('IPCComm', key)
def get_queue(qid=None): r"""Create or return a sysv_ipc.MessageQueue and register it. Args: qid (int, optional): If provided, ID for existing queue that should be returned. Defaults to None and a new queue is returned. Returns: :class:`sysv_ipc.MessageQueue`: Message queue. """ if _ipc_installed: kwargs = dict(max_message_size=tools.get_CIS_MSG_MAX()) if qid is None: kwargs['flags'] = sysv_ipc.IPC_CREX mq = sysv_ipc.MessageQueue(qid, **kwargs) key = str(mq.key) CommBase.register_comm('IPCComm', key, mq) return mq else: # pragma: windows logging.warning("IPC not installed. Queue cannot be returned.") return None
def test_queue(): r"""Test creation/removal of queue.""" mq = IPCComm.get_queue() key = str(mq.key) assert (CommBase.is_registered('IPCComm', key)) CommBase.unregister_comm('IPCComm', key, dont_close=True) nt.assert_raises(KeyError, IPCComm.remove_queue, mq) CommBase.register_comm('IPCComm', key, mq) IPCComm.remove_queue(mq) assert (not CommBase.is_registered('IPCComm', key))
def terminate(self, *args, **kwargs): CommBase.unregister_comm('IPCComm', self.srv_address) super(IPCServer, self).terminate(*args, **kwargs)
def test_registry(): r"""Test registry of comm.""" comm_class = 'CommBase' key = 'key1' value = None assert(not CommBase.is_registered(comm_class, key)) assert(not CommBase.unregister_comm(comm_class, key)) nt.assert_equal(CommBase.get_comm_registry(None), {}) nt.assert_equal(CommBase.get_comm_registry(comm_class), {}) CommBase.register_comm(comm_class, key, value) assert(key in CommBase.get_comm_registry(comm_class)) assert(CommBase.is_registered(comm_class, key)) assert(not CommBase.unregister_comm(comm_class, key, dont_close=True)) CommBase.register_comm(comm_class, key, value) assert(not CommBase.unregister_comm(comm_class, key))