コード例 #1
0
ファイル: test_monitor.py プロジェクト: zeromq/pyzmq
    def test_monitor(self):
        """Test monitoring interface for sockets."""
        s_rep = self.context.socket(zmq.REP)
        s_req = self.context.socket(zmq.REQ)
        self.sockets.extend([s_rep, s_req])
        s_req.bind("tcp://127.0.0.1:6666")
        # try monitoring the REP socket
        
        s_rep.monitor("inproc://monitor.rep", zmq.EVENT_CONNECT_DELAYED | zmq.EVENT_CONNECTED | zmq.EVENT_MONITOR_STOPPED)
        # create listening socket for monitor
        s_event = self.context.socket(zmq.PAIR)
        self.sockets.append(s_event)
        s_event.connect("inproc://monitor.rep")
        s_event.linger = 0
        # test receive event for connect event
        s_rep.connect("tcp://127.0.0.1:6666")
        m = recv_monitor_message(s_event)
        if m['event'] == zmq.EVENT_CONNECT_DELAYED:
            self.assertEqual(m['endpoint'], b"tcp://127.0.0.1:6666")
            # test receive event for connected event
            m = recv_monitor_message(s_event)
        self.assertEqual(m['event'], zmq.EVENT_CONNECTED)
        self.assertEqual(m['endpoint'], b"tcp://127.0.0.1:6666")

        # test monitor can be disabled.
        s_rep.disable_monitor()
        m = recv_monitor_message(s_event)
        self.assertEqual(m['event'], zmq.EVENT_MONITOR_STOPPED)
コード例 #2
0
    def provmgr_cb(self, sock, mask):
        if self.pushSock is not None and sock == self.pushSock.monitor:
            self.pushSock.monitorHandler(recv_monitor_message(sock))
            return
        if self.pullSock is not None and sock == self.pullSock.monitor:
            self.pullSock.monitorHandler(recv_monitor_message(sock))
            return
        if self.mgrConnection is not None and sock == self.mgrConnection.monitor:
            self.mgrConnection.monitorHandler(recv_monitor_message(sock))
            return

        while sock.getsockopt(zmq.EVENTS) and zmq.POLLIN:
            try:
                bin = sock.recv(flags=zmq.NOBLOCK)
                msg = HalMessage.DeSerialize(bin)
                if msg.type in self.HalMsgsHandler:
                    handler = self.HalMsgsHandler[msg.type]
                    handler(msg)
            except zmq.ZMQError as e:
                self.logger.debug(
                    "Got an error when trying with non-block read:" + str(e))
                break
            except Exception as e:
                self.logger.warn(
                    "Exception happens when provmgr hal recv socket, reason:%s"
                    % str(e))
                break
コード例 #3
0
ファイル: rpd_fault_manager.py プロジェクト: hujiangyi/or
    def fault_management_cb(self, sock, mask):  # pragma: no cover
        """Fault management callback.

        :param sock: zmq socket
        :param mask: event mask
        :return:

        """

        if self.pushSock is not None and sock == self.pushSock.monitor:
            self.pushSock.monitorHandler(recv_monitor_message(sock))
            return
        if self.pullSock is not None and sock == self.pullSock.monitor:
            self.pullSock.monitorHandler(recv_monitor_message(sock))
            return
        if self.mgrConnection is not None and sock == self.mgrConnection.monitor:
            self.mgrConnection.monitorHandler(recv_monitor_message(sock))
            return

        while sock.getsockopt(zmq.EVENTS) and zmq.POLLIN:
            try:
                bin = sock.recv(flags=zmq.NOBLOCK)
                msg = HalMessage.DeSerialize(bin)
                self.logger.debug("###########Got a zmq msg:%s" % msg.msg)
                if msg.type in self.HalMsgsHandler:
                    handler = self.HalMsgsHandler[msg.type]
                    handler(msg)
            except zmq.ZMQError as e:
                self.logger.debug(
                    "Getting an error when trying with nonblock read:" +
                    str(e))
                break
            except Exception as e:
                self.logger.error("Error happens, reason:%s" % str(e))
                break
コード例 #4
0
ファイル: open_rpd_drv.py プロジェクト: hujiangyi/or
    def openrpd_drv_hal_cb(self, sock, mask):
        self.logger.debug("Driver received hal cb event")
        if self.pushSock is not None and sock == self.pushSock.monitor:
            self.pushSock.monitorHandler(recv_monitor_message(sock))
            return
        if self.pullSock is not None and sock == self.pullSock.monitor:
            self.pullSock.monitorHandler(recv_monitor_message(sock))
            return

        if sock == self.mgrConnection.monitor:
            self.mgrConnection.monitorHandler(recv_monitor_message(sock))
            return

        while sock.getsockopt(zmq.EVENTS) and zmq.POLLIN:
            try:
                bin = sock.recv(flags=zmq.NOBLOCK)
                msg = HalMessage.DeSerialize(bin)
                self.logger.debug("Got a zmq msg:%s type:%s" %
                                  (msg.msg, msg.type))
                if msg.type in self.HalMsgsHandler:
                    handler = self.HalMsgsHandler[msg.type]
                    handler(msg)
            except zmq.ZMQError as e:
                self.logger.debug(
                    "Got an error when trying with nonblock read:" + str(e))
                break
            except Exception as e:
                self.logger.error("Got an un-expected error:%s", str(e))
                break
コード例 #5
0
    def test_monitor(self):
        """Test monitoring interface for sockets."""
        s_rep = self.context.socket(zmq.REP)
        s_req = self.context.socket(zmq.REQ)
        self.sockets.extend([s_rep, s_req])
        s_req.bind("tcp://10.0.0.7:6666")
        # try monitoring the REP socket

        s_rep.monitor(
            "inproc://monitor.rep", zmq.EVENT_CONNECT_DELAYED
            | zmq.EVENT_CONNECTED | zmq.EVENT_MONITOR_STOPPED)
        # create listening socket for monitor
        s_event = self.context.socket(zmq.PAIR)
        self.sockets.append(s_event)
        s_event.connect("inproc://monitor.rep")
        s_event.linger = 0
        # test receive event for connect event
        s_rep.connect("tcp://10.0.0.7:6666")
        m = recv_monitor_message(s_event)
        if m['event'] == zmq.EVENT_CONNECT_DELAYED:
            self.assertEqual(m['endpoint'], b"tcp://10.0.0.7:6666")
            # test receive event for connected event
            m = recv_monitor_message(s_event)
        self.assertEqual(m['event'], zmq.EVENT_CONNECTED)
        self.assertEqual(m['endpoint'], b"tcp://10.0.0.7:6666")

        # test monitor can be disabled.
        s_rep.disable_monitor()
        m = recv_monitor_message(s_event)
        self.assertEqual(m['event'], zmq.EVENT_MONITOR_STOPPED)
コード例 #6
0
ファイル: local_peer.py プロジェクト: pyl1b/p2p0mq
    def monitor(self):
        """ Called on each loop to do monitoring tasks."""
        if not self.zmq_monitor:
            return

        if len(self.zmq_monitor.sockets) == 0:
            if self.receiver is not None and self.receiver.socket is not None:
                if self.sender is not None and self.sender.socket is not None:
                    self.zmq_monitor.register(
                        self.receiver.socket.get_monitor_socket())
                    self.zmq_monitor.register(
                        self.sender.socket.get_monitor_socket())
                    event_map = {}
                    setattr(self, 'event_map', event_map)
                    # print("Event names:")
                    for name in dir(zmq):
                        if name.startswith('EVENT_'):
                            value = getattr(zmq, name)
                            # print("%21s : %4i" % (name, value))
                            event_map[value] = name

        event_map = getattr(self, 'event_map')
        socket_rec = self.zmq_monitor.sockets[0]
        socket_se = self.zmq_monitor.sockets[1]
        socks = dict(self.zmq_monitor.poll(100))
        if socket_rec in socks and socks[socket_rec] & zmq.POLLIN:
            message = recv_monitor_message(socket_rec)
            message.update({'description': event_map[message['event']]})
            logger.log(TRACE_NET, "RECEIVER: %r", message)
        if socket_se in socks and socks[socket_se] & zmq.POLLIN:
            message = recv_monitor_message(socket_se)
            message.update({'description': event_map[message['event']]})
            logger.log(TRACE_NET, "SENDER: %r", message)
コード例 #7
0
ファイル: HalDriver0.py プロジェクト: hujiangyi/or
    def start(self):
        """Start polling the transport socket.

        :return:

        """
        self.logger.debug("Start the driver client poll...")
        self.connectionSetup()

        self.register(self.drvID)
        lastTimeout = time()

        while True:  # Todo we should support quit flag?
            socks = self.poller.poll(self.pollTimeout)
            if time() - lastTimeout > self.pollTimeout / 1000:
                lastTimeout = time()
                # self.logger.debug("Got a timeout event")
                if self.recvNtf:
                    rcp_msg = t_RcpMessage()
                    rcp_msg.RpdDataMessage.RpdDataOperation = t_RpdDataMessage.RPD_CFG_READ
                    rcp_msg.RcpMessageType = t_RcpMessage.RPD_CONFIGURATION
                    rcp_msg.RpdDataMessage.RpdData.CopyFrom(config())
                    payload = rcp_msg.SerializeToString()
                    self.sendCfgMsg(1025, payload)
                    self.recvNtf -= 1

            if not socks:
                continue
            for sock in socks:
                if self.pushSock is not None and sock == self.pushSock.monitor:
                    self.pushSock.monitorHandler(recv_monitor_message(sock))
                    continue
                if self.pullSock is not None and sock == self.pullSock.monitor:
                    self.pullSock.monitorHandler(recv_monitor_message(sock))
                    continue
                if sock == self.mgrConnection.monitor:
                    self.mgrConnection.monitorHandler(
                        recv_monitor_message(sock))
                    continue
                if socks[sock] == HalPoller.POLLIN:
                    try:
                        bin = sock.recv(flags=zmq.NOBLOCK)
                        msg = HalMessage.DeSerialize(bin)
                        self.logger.debug("Got a zmq msg:%s" % msg.msg)
                        if msg.type in self.HalMsgsHandler:
                            handler = self.HalMsgsHandler[msg.type]
                            handler(msg)
                        else:
                            self.logger.warn("Unsupported msg type:%s" %
                                             msg.type)
                    except zmq.ZMQError as e:
                        self.logger.debug(
                            "Geting an error when trying with nonblock read:" +
                            str(e))
                    except Exception as e:
                        self.logger.debug("Geting an error:" + str(e))
                continue
コード例 #8
0
    def start(self):
        """Start polling the transport socket.

        :return:

        """
        self.logger.debug("Start the client poll...")
        self.connectionSetup()

        self.register(self.clientID)
        lastTimeout = time()

        sendOnce = False

        while True:  # Todo we should support quit flag?
            socks = self.poller.poll(self.pollTimeout)

            if time() - lastTimeout > self.pollTimeout / 1000:
                lastTimeout = time()
                self.logger.debug("Got a timeout event")
                if not sendOnce:
                    # self.sendCfgMsg(100, "hello")
                    self.setHalDebugLevel("HalMain", logging.INFO)
                    sendOnce = True

            if not socks:
                continue
            for sock in socks:  # FIXME do we need to continue recv from the monitor interface?
                if self.pushSock is not None and sock == self.pushSock.monitor:
                    self.pushSock.monitorHandler(recv_monitor_message(sock))
                    continue
                if self.pullSock is not None and sock == self.pullSock.monitor:
                    self.pullSock.monitorHandler(recv_monitor_message(sock))
                    continue
                if self.mgrConnection is not None and sock == self.mgrConnection.monitor:
                    self.mgrConnection.monitorHandler(
                        recv_monitor_message(sock))
                    continue
                if socks[sock] == HalPoller.POLLIN:
                    try:
                        bin = sock.recv(flags=zmq.NOBLOCK)
                        msg = HalMessage.DeSerialize(bin)
                        self.logger.debug("Got a zmq msg:%s" % msg.msg)
                        if msg.type in self.HalMsgsHandler:
                            handler = self.HalMsgsHandler[msg.type]
                            handler(msg)
                        else:
                            self.logger.warn("Unsupported msg type:%s" %
                                             msg.type)
                    except zmq.ZMQError as e:
                        self.logger.debug(
                            "Got an error when trying with non-block read:" +
                            str(e))
                    except Exception as e:
                        self.logger.error("Error happens, reason:%s" % str(e))
                continue
コード例 #9
0
ファイル: test_hal_multicast.py プロジェクト: hujiangyi/or
    def connect_to_hal(self):
        self.connectionSetup()
        self.register(self.drvID)
        i = 0
        max_times = 10
        expected_msgs = [
            "HalClientInterestNotificationCfgRsp", "HalClientHelloRsp"
        ]
        while expected_msgs != []:
            socks = self.poller.poll(1000)
            print socks
            i += 1
            if i > max_times:
                self.logger.error("break while due to reach %d times" %
                                  max_times)
                break

            if not socks:
                continue
            for sock in socks:
                if self.pushSock is not None and sock == self.pushSock.monitor:
                    self.pushSock.monitorHandler(recv_monitor_message(sock))
                    continue
                if self.pullSock is not None and sock == self.pullSock.monitor:
                    self.pullSock.monitorHandler(recv_monitor_message(sock))
                    continue
                if self.mgrConnection is not None and sock == self.mgrConnection.monitor:
                    self.mgrConnection.monitorHandler(
                        recv_monitor_message(sock))
                    continue
                if socks[sock] == HalPoller.POLLIN:
                    try:
                        bin = sock.recv(flags=zmq.NOBLOCK)
                        msg = HalMessage.DeSerialize(bin)
                        print msg.msg
                        self.logger.debug("Got a zmq msg:%s" % msg.msg)
                        if msg.msg.MsgType in expected_msgs:
                            expected_msgs.remove(msg.msg.MsgType)
                        if msg.type in self.HalMsgsHandler:
                            handler = self.HalMsgsHandler[msg.type]
                            handler(msg)
                        else:
                            self.logger.error("Unsupported msg type:%s" %
                                              msg.type)
                    except zmq.ZMQError as e:
                        self.logger.debug(
                            "Got an error when trying with non-block read:" +
                            str(e))
                    except Exception as e:
                        self.logger.error("Error happens, reason:%s" % str(e))
                continue
コード例 #10
0
ファイル: probe.py プロジェクト: xinyixiang/parsl
def probe_addresses(addresses, task_port, timeout=2):
    """
    Parameters
    ----------

    addresses: [string]
        List of addresses as strings
    task_port: int
        Task port on the interchange
    timeout: int
        Timeout in seconds

    Returns
    -------
    None or string address
    """
    context = zmq.Context()
    addr_map = {}
    for addr in addresses:
        socket = context.socket(zmq.DEALER)
        socket.setsockopt(zmq.LINGER, 0)
        url = "tcp://{}:{}".format(addr, task_port)
        logger.debug("Trying to connect back on {}".format(url))
        socket.connect(url)
        addr_map[addr] = {
            'sock': socket,
            'mon_sock': socket.get_monitor_socket(events=zmq.EVENT_CONNECTED)
        }

    start_t = time.time()

    first_connected = None
    while time.time() < start_t + timeout and not first_connected:
        for addr in addr_map:
            try:
                recv_monitor_message(addr_map[addr]['mon_sock'], zmq.NOBLOCK)
                first_connected = addr
                logger.info(
                    "Connected to interchange on {}".format(first_connected))
                break
            except zmq.Again:
                pass
            time.sleep(0.01)

    for addr in addr_map:
        addr_map[addr]['sock'].close()

    return first_connected
コード例 #11
0
 def monitor_worker(self, monitor_socket, log_name):
     monitor_socket.linger = 0
     poller = zmq.Poller()
     poller.register(monitor_socket, zmq.POLLIN)
     while True:
         socks = poller.poll(1)
         gevent.sleep(0.1)
         if len(socks) > 0:
             data = recv_monitor_message(monitor_socket)
             event = data['event']
             value = data['value']
             if event == zmq.EVENT_CONNECTED:
                 logger.info('Connected to {0}'.format(log_name))
                 if 'outgoing' in log_name:
                     send_zmq_push('inproc://serverRelay',
                                   '{0}'.format(Messages.PING))
                     own_ip = gevent.socket.gethostbyname(
                         socket.gethostname())
                     send_zmq_push('inproc://serverRelay',
                                   '{0} {1}'.format(Messages.IP, own_ip))
                     send_zmq_push('inproc://serverRelay',
                                   '{0}'.format(Messages.DRONE_CONFIG))
                 elif 'incomming':
                     pass
                 else:
                     assert False
             elif event == zmq.EVENT_DISCONNECTED:
                 logger.warning(
                     'Disconnected from {0}, will reconnect in {1} seconds.'
                     .format(log_name, 5))
         gevent.sleep()
コード例 #12
0
    def handle_monitor_event(self, monitor_sock):
        """
        Monitor external platform socket connections
        :param monitor_sock: socket to monitor
        :return:
        """
        try:
            message = recv_monitor_message(monitor_sock)
            event = message['event']
            instance_name = [
                name for name, instance_info in self._instances.items()
                if instance_info['monitor_socket'] == monitor_sock
            ]

            if event & zmq.EVENT_CONNECTED:
                _log.debug(
                    "CONNECTED to external platform: {}!! Sending MY subscriptions !!"
                    .format(instance_name[0]))
                self._instances[instance_name[0]]['status'] = STATUS_CONNECTED
                self._onconnect_pubsub_handler(instance_name[0])
            elif event & zmq.EVENT_CONNECT_DELAYED:
                # _log.debug("ROUTINGSERVICE socket DELAYED...Lets wait")
                self._instances[
                    instance_name[0]]['status'] = STATUS_CONNECTION_DELAY
            elif event & zmq.EVENT_DISCONNECTED:
                _log.debug("DISCONNECTED from external platform: {}. "
                           "Subscriptions will be resent on reconnect".format(
                               instance_name[0]))
                self._instances[
                    instance_name[0]]['status'] = STATUS_DISCONNECTED
        except ZMQError as exc:
            if exc.errno == ENOTSOCK:
                _log.error("Trying to use a non socket {}".format(exc))
        except KeyError as exc:
            _log.error("Unknown external instance: {}".format(instance_name))
コード例 #13
0
ファイル: routingservice.py プロジェクト: Kisensum/volttron
    def handle_monitor_event(self, monitor_sock):
        """
        Monitor external platform socket connections
        :param monitor_sock: socket to monitor
        :return:
        """
        try:
            message = recv_monitor_message(monitor_sock)
            event = message['event']
            instance_name = [name for name, instance_info in self._instances.items()
                             if instance_info['monitor_socket'] == monitor_sock]

            if event & zmq.EVENT_CONNECTED:
                _log.debug(
                    "CONNECTED to external platform: {}!! Sending MY subscriptions !!".format(instance_name[0]))
                self._instances[instance_name[0]]['status'] = STATUS_CONNECTED
                self._onconnect_pubsub_handler(instance_name[0])
            elif event & zmq.EVENT_CONNECT_DELAYED:
                # _log.debug("ROUTINGSERVICE socket DELAYED...Lets wait")
                self._instances[instance_name[0]]['status'] = STATUS_CONNECTION_DELAY
            elif event & zmq.EVENT_DISCONNECTED:
                _log.debug("DISCONNECTED from external platform: {}. "
                           "Subscriptions will be resent on reconnect".format(instance_name[0]))
                self._instances[instance_name[0]]['status'] = STATUS_DISCONNECTED
        except ZMQError as exc:
            if exc.errno == ENOTSOCK:
                _log.error("Trying to use a non socket {}".format(exc))
        except KeyError as exc:
            _log.error("Unknown external instance: {}".format(instance_name))
コード例 #14
0
ファイル: drone.py プロジェクト: cmsmith1977/beeswarm
 def monitor_worker(self, monitor_socket, log_name):
     monitor_socket.linger = 0
     poller = zmq.Poller()
     poller.register(monitor_socket, zmq.POLLIN)
     while True:
         socks = poller.poll(1)
         gevent.sleep(0.1)
         if len(socks) > 0:
             data = recv_monitor_message(monitor_socket)
             event = data['event']
             value = data['value']
             if event == zmq.EVENT_CONNECTED:
                 logger.info('Connected to {0}'.format(log_name))
                 if 'outgoing' in log_name:
                     send_zmq_push('inproc://serverRelay', '{0}'.format(Messages.PING))
                     own_ip = gevent.socket.gethostbyname(socket.gethostname())
                     send_zmq_push('inproc://serverRelay', '{0} {1}'.format(Messages.IP,
                                                                         own_ip))
                     send_zmq_push('inproc://serverRelay', '{0}'.format(Messages.DRONE_CONFIG))
                 elif 'incomming':
                     pass
                 else:
                     assert False
             elif event == zmq.EVENT_DISCONNECTED:
                 logger.warning('Disconnected from {0}, will reconnect in {1} seconds.'.format(log_name, 5))
         gevent.sleep()
コード例 #15
0
ファイル: server.py プロジェクト: selassid/zchat
 def run(self):
     while True:
         event_type = recv_monitor_message(self._monitor_socket)['event']
         if event_type == zmq.EVENT_ACCEPTED:
             self._client_hadler.client_count += 1
         elif event_type == zmq.EVENT_CLOSED or event_type == zmq.EVENT_DISCONNECTED:
             self._client_hadler.client_count -= 1
コード例 #16
0
ファイル: zmq_tools.py プロジェクト: vikrantmygamma/pupil
    def __init__(self, ctx, url, topics=(), block_until_connected=True, hwm=None):
        self.socket = zmq.Socket(ctx, zmq.SUB)
        assert type(topics) != str

        if hwm is not None:
            self.socket.set_hwm(hwm)

        if block_until_connected:
            # connect node and block until a connecetion has been made
            monitor = self.socket.get_monitor_socket()
            self.socket.connect(url)
            while True:
                status = recv_monitor_message(monitor)
                if status["event"] == zmq.EVENT_CONNECTED:
                    break
                elif status["event"] == zmq.EVENT_CONNECT_DELAYED:
                    pass
                else:
                    raise Exception("ZMQ connection failed")
            self.socket.disable_monitor()
        else:
            self.socket.connect(url)

        for t in topics:
            self.subscribe(t)
コード例 #17
0
ファイル: zmq_tools.py プロジェクト: pupil-labs/pupil
    def __init__(self, ctx, url, topics=(), block_until_connected=True, hwm=None):
        self.socket = zmq.Socket(ctx, zmq.SUB)
        assert type(topics) != str

        if hwm is not None:
            self.socket.set_hwm(hwm)

        if block_until_connected:
            # connect node and block until a connecetion has been made
            monitor = self.socket.get_monitor_socket()
            self.socket.connect(url)
            while True:
                status = recv_monitor_message(monitor)
                if status["event"] == zmq.EVENT_CONNECTED:
                    break
                elif status["event"] == zmq.EVENT_CONNECT_DELAYED:
                    pass
                else:
                    raise Exception("ZMQ connection failed")
            self.socket.disable_monitor()
        else:
            self.socket.connect(url)

        for t in topics:
            self.subscribe(t)
コード例 #18
0
ファイル: port_broker.py プロジェクト: kaithar/muhubot
 def main_loop(self):
     try:
         self.log('In proc')
         self.create_socket()
         while True:
             data = self.socket.recv()
             safe_data = data.decode('utf-8')
             self.log('I got one!')
             self.input_queue.put(True)
             port = self.output_queue.get()
             self.socket.send(str(port).encode())
             self.log('I sent one!')
             
             # Check for useful events
             if (self.monitor.closed == False and self.monitor.poll(timeout=100)):
                 evt = recv_monitor_message(self.monitor)
                 evt.update({'description': EVENT_MAP[evt['event']]})
                 if evt['event'] not in (zmq.EVENT_CONNECT_RETRIED,
                                         zmq.EVENT_CONNECT_DELAYED,
                                         zmq.EVENT_CLOSED):
                     # Completely ignore these 3 events because they spam too much.
                     self.log("Event: {}".format(evt))
                     if evt['event'] == zmq.EVENT_MONITOR_STOPPED:
                         break
     except zmq.ZMQError as e:
         self.log('Exception!')
         if e.errno == zmq.ETERM:
             pass           # Interrupted
         else:
             self.log(''.join(traceback.format_exc(None)))
     except:
         self.log(''.join(traceback.format_exc(None)))
     self.log('Exiting thread!')
コード例 #19
0
ファイル: drone.py プロジェクト: zerolugithub/beeswarm
 def monitor_worker(self, monitor_socket, log_name):
     monitor_socket.linger = 0
     poller = zmq.Poller()
     poller.register(monitor_socket, zmq.POLLIN)
     while True:
         socks = poller.poll(1)
         gevent.sleep(0.1)
         if len(socks) > 0:
             data = recv_monitor_message(monitor_socket)
             event = data['event']
             if event == zmq.EVENT_CONNECTED:
                 logger.info('Connected to {0}'.format(log_name))
                 # always ask for config to avoid race condition.
                 send_zmq_push(SocketNames.SERVER_RELAY.value, '{0}'.format(Messages.DRONE_WANT_CONFIG.value))
                 if 'outgoing' in log_name:
                     send_zmq_push(SocketNames.SERVER_RELAY.value, '{0}'.format(Messages.PING.value))
                     own_ip = get_most_likely_ip()
                     send_zmq_push(SocketNames.SERVER_RELAY.value, '{0} {1}'.format(Messages.IP.value, own_ip))
                 elif 'incomming':
                     pass
                 else:
                     assert False
             elif event == zmq.EVENT_DISCONNECTED:
                 logger.warning('Disconnected from {0}, will reconnect in {1} seconds.'.format(log_name, 5))
         gevent.sleep()
コード例 #20
0
 def _DWX_ZMQ_EVENT_MONITOR_(self, 
                             socket_name, 
                             monitor_socket):
     
     # 05-08-2019 11:21 CEST
     while self._ACTIVE:
         
         sleep(self._sleep_delay) # poll timeout is in ms, sleep() is s.
         
         # while monitor_socket.poll():
         while monitor_socket.poll(self._poll_timeout):
             
             try:
                 evt = recv_monitor_message(monitor_socket, zmq.DONTWAIT)
                 evt.update({'description': self._MONITOR_EVENT_MAP[evt['event']]})
                 
                 # print(f"\r[{socket_name} Socket] >> {evt['description']}", end='', flush=True)
                 print(f"\n[{socket_name} Socket] >> {evt['description']}")
                 
                 # Set socket status on HANDSHAKE
                 if evt['event'] == 4096:        # EVENT_HANDSHAKE_SUCCEEDED
                     
                     if socket_name == "PUSH":
                         self._PUSH_SOCKET_STATUS['state'] = True
                         self._PUSH_SOCKET_STATUS['latest_event'] = 'EVENT_HANDSHAKE_SUCCEEDED'
                         
                     elif socket_name == "PULL":
                         self._PULL_SOCKET_STATUS['state'] = True
                         self._PULL_SOCKET_STATUS['latest_event'] = 'EVENT_HANDSHAKE_SUCCEEDED'
                         
                     # print(f"\n[{socket_name} Socket] >> ..ready for action!\n")
                         
                 else:    
                     # Update 'latest_event'
                     if socket_name == "PUSH":
                         self._PUSH_SOCKET_STATUS['state'] = False
                         self._PUSH_SOCKET_STATUS['latest_event'] = evt['description']
                         
                     elif socket_name == "PULL":
                         self._PULL_SOCKET_STATUS['state'] = False
                         self._PULL_SOCKET_STATUS['latest_event'] = evt['description']
             
                 if evt['event'] == zmq.EVENT_MONITOR_STOPPED:
                     
                     # Reinitialize the socket
                     if socket_name == "PUSH":
                         monitor_socket = self._PUSH_SOCKET.get_monitor_socket()
                     elif socket_name == "PULL":
                         monitor_socket = self._PULL_SOCKET.get_monitor_socket()
                     
             except Exception as ex:
                 _exstr = "Exception Type {0}. Args:\n{1!r}"
                 _msg = _exstr.format(type(ex).__name__, ex.args)
                 print(_msg)
            
     # Close Monitor Socket
     monitor_socket.close()
     
     print(f"\n++ [KERNEL] {socket_name} _DWX_ZMQ_EVENT_MONITOR_() Signing Out ++")
コード例 #21
0
ファイル: mispzmq.py プロジェクト: tomking2/MISP
def event_monitor(monitor, logger):
    while monitor.poll():
        evt = recv_monitor_message(monitor)
        if evt["event"] == zmq.EVENT_MONITOR_STOPPED:
            break
        evt.update({"description": EVENT_MAP[evt["event"]]})
        logger.debug("ZMQ event: {}".format(evt))
    monitor.close()
コード例 #22
0
ファイル: security.py プロジェクト: chrisjbillington/zprocess
    def connect(self, addr, timeout=None, interruptor=None):
        """Wrapper around connect, configuring security. If timeout is not None, then
        this method will block until the given timeout in ms (or forever if timeout=-1)
        or until the connection is sucessful or interrupted. When called in this
        blocking way, failed authentication will be raised as an exception. Do not set
        timeout if you already have a monitor socket for this socket, in this case you
        should monitor for connection events yourself."""

        # Not allowed to insecurely connect to external addresses unless
        # allow_insecure specified:
        if not (self.secure or self._is_internal(addr) or self.allow_insecure):
            raise InsecureConnection(INSECURE_CONNECT_ERROR % addr)

        if timeout is not None:
            monitor = self.get_monitor_socket()
            poller = zmq.Poller()
            poller.register(monitor, zmq.POLLIN)
            if interruptor is not None:
                interruption_sock = interruptor.subscribe()
                poller.register(interruption_sock)
            if timeout != -1:
                # Convert to s:
                timeout /= 1000

        try:
            result = self._bind_or_connect(addr, connect=True)
            if timeout is None:
                return result
            # Block until we get an authentication success or failure
            deadline = monotonic() + timeout
            while True:
                if timeout == -1:
                    remaining = None
                else:
                    remaining = (deadline - monotonic()) * 1000  # ms
                events = dict(poller.poll(remaining))
                if not events:
                    raise TimeoutError(
                        'Could not connect to server: timed out')
                if interruptor is not None and interruption_sock in events:
                    raise Interrupted(interruption_sock.recv().decode('utf8'))
                assert events[monitor] == zmq.POLLIN
                event = recv_monitor_message(monitor)['event']
                if event in CONN_SUCCESS_EVENTS:
                    return result
                elif event in CONN_FAIL_EVENTS:
                    msg = (
                        "Failed to authenticate with server. Ensure both client "
                        + "and server have the same shared secret.")
                    raise AuthenticationFailure(msg)
        finally:
            if timeout is not None:
                if interruptor is not None:
                    poller.unregister(interruption_sock)
                    interruptor.unsubscribe()
                poller.unregister(monitor)
                self.disable_monitor()
                monitor.close()
コード例 #23
0
ファイル: server.py プロジェクト: scottcanoe/mesoimg
def event_monitor(monitor):
    while monitor.poll():
        evt = recv_monitor_message(monitor)
        evt.update({'description': EVENT_MAP[evt['event']]})
        print("Event: {}".format(evt))
        if evt['event'] == zmq.EVENT_MONITOR_STOPPED:
            break
    monitor.close()
    print("event monitor thread done!")
コード例 #24
0
    def _receive_events(self, sock_created):
        import zmq
        from zmq.utils.monitor import recv_monitor_message

        sub = self._context.socket(zmq.SUB)  # @UndefinedVariable
        sub_endpoints = set()
        sub.setsockopt(zmq.SUBSCRIBE, b"")  # @UndefinedVariable
        sub_monitor = sub.get_monitor_socket()
        sock_created.set()

        pending_connections = {}
        while self._pubsub_running:

            # A new subscription has been requested
            try:
                subscription = self._subscriptions.get_nowait()
                if subscription.endpoint in sub_endpoints:
                    subscription.finished_evt.set()
                else:
                    sub.connect(subscription.endpoint)
                    pending_connections[
                        subscription.endpoint
                    ] = subscription.finished_evt
            except queue.Empty:
                pass

            try:
                msg = recv_monitor_message(sub_monitor, flags=zmq.NOBLOCK)
                if msg["event"] != zmq.EVENT_CONNECTED:
                    continue
                endpoint = utils.b2s(msg["endpoint"])
                sub_endpoints.add(endpoint)
                finished_evt = pending_connections.pop(endpoint)
                finished_evt.set()
            except zmq.error.Again:
                pass

            try:
                evt = sub.recv_pyobj(flags=zmq.NOBLOCK)  # @UndefinedVariable
                self._events_in.put(evt)
            except zmq.error.Again:
                time.sleep(0.01)
            except Exception:
                # Figure out what to do here
                logger.exception(
                    "Something bad happened in %s:%d to ZMQ :'(",
                    self._events_host,
                    self._events_port,
                )
                break

        # Flush pending connection events to avoid callers hanging out forever
        for evt in pending_connections:
            evt.set()

        sub_monitor.close()
        sub.close()
コード例 #25
0
ファイル: zstack.py プロジェクト: pradeep1991singh/stp
    def hasLostConnection(self):
        if self.socket is None:
            logger.warning('Remote {} already disconnected'.format(self))
            return False

        monitor = self.socket.get_monitor_socket()
        events = []
        while True:
            try:
                # noinspection PyUnresolvedReferences
                m = recv_monitor_message(monitor, flags=zmq.NOBLOCK)
                events.append(m['event'])
            except zmq.Again:
                break

        if events:
            logger.trace('Remote {} has monitor events: {}'.format(
                self, events))
        # noinspection PyUnresolvedReferences
        if zmq.EVENT_DISCONNECTED in events or zmq.EVENT_CLOSED in events:
            logger.debug('{} found disconnected event on monitor'.format(self))

            # Reverse events list since list has no builtin to get last index
            events.reverse()

            try:
                # noinspection PyUnresolvedReferences
                d = events.index(zmq.EVENT_DISCONNECTED)
            except ValueError:
                d = sys.maxsize

            try:
                # noinspection PyUnresolvedReferences
                cl = events.index(zmq.EVENT_CLOSED)
            except ValueError:
                cl = sys.maxsize

            try:
                # noinspection PyUnresolvedReferences
                c = events.index(zmq.EVENT_CONNECTED)
            except ValueError:
                c = sys.maxsize

            try:
                # noinspection PyUnresolvedReferences
                p = events.index(zmq.EVENT_CONNECT_DELAYED)
            except ValueError:
                p = sys.maxsize

            # If sudden disconnect or socket closed precedes connection
            # or pending connection
            d = min(d, cl)
            if d < c and d < p:
                # If no connected event
                return True
        return False
コード例 #26
0
ファイル: simple_monitor.py プロジェクト: ykwon8651/project-e
def logger(monitor):
    done = False
    while monitor.poll(timeout=5000):
        evt = recv_monitor_message(monitor)
        print(json.dumps(evt, indent=1))
        if evt['event'] == zmq.EVENT_MONITOR_STOPPED:
            break
    print()
    print("Logger done!")
    monitor.close()
コード例 #27
0
ファイル: simple_monitor.py プロジェクト: Bluehorn/pyzmq
def logger(monitor):
    done = False
    while monitor.poll(timeout=5000):
        evt = recv_monitor_message(monitor)
        print(json.dumps(evt, indent=1))
        if evt['event'] == zmq.EVENT_MONITOR_STOPPED:
            break
    print()
    print("Logger done!")
    monitor.close()
コード例 #28
0
 def test_monitor_connected(self):
     """Test connected monitoring socket."""
     s_rep = self.context.socket(zmq.REP)
     s_req = self.context.socket(zmq.REQ)
     self.sockets.extend([s_rep, s_req])
     s_req.bind("tcp://127.0.0.1:6667")
     # try monitoring the REP socket
     # create listening socket for monitor
     s_event = s_rep.get_monitor_socket()
     s_event.linger = 0
     self.sockets.append(s_event)
     # test receive event for connect event
     s_rep.connect("tcp://127.0.0.1:6667")
     m = recv_monitor_message(s_event)
     self.assertEqual(m['event'], zmq.EVENT_CONNECT_DELAYED)
     self.assertEqual(m['endpoint'], b"tcp://127.0.0.1:6667")
     # test receive event for connected event
     m = recv_monitor_message(s_event)
     self.assertEqual(m['event'], zmq.EVENT_CONNECTED)
コード例 #29
0
ファイル: simple_monitor.py プロジェクト: 326029212/pyzmq
def event_monitor(monitor):
    while monitor.poll():
        evt = recv_monitor_message(monitor)
        evt.update({'description': EVENT_MAP[evt['event']]})
        print("Event: {}".format(evt))
        if evt['event'] == zmq.EVENT_MONITOR_STOPPED:
            break
    monitor.close()
    print()
    print("event monitor thread done!")
コード例 #30
0
 def event_monitor(monitor):
     while monitor.poll():
         evt = recv_monitor_message(monitor)
         evt.update({'description': EVENT_MAP[evt['event']]})
         if evt['event'] == zmq.EVENT_HANDSHAKE_SUCCEEDED:
             push_socket.send_json(
                 {"type": "REGISTER", "id": str(worker_id)})
         if evt['event'] == zmq.EVENT_MONITOR_STOPPED:
             break
     monitor.close()
コード例 #31
0
 def monitorSocket(self, monitor, timer):
     events = cfg.events
     timeout = time.time() + timer
     connected_users = []
     while (monitor.poll() and (time.time() < timeout)):
         event = recv_monitor_message(monitor)
         event.update({'descriptor:': events[event['event']]})
         if ("EVENT_ACCEPTED" in events[event['event']]):
             print("Player connected!")
             connected_users.append(event['value'])
         print("Event: {}".format(event))
コード例 #32
0
 def test_monitor_repeat(self):
     s = self.socket(zmq.PULL)
     m = s.get_monitor_socket()
     self.sockets.append(m)
     m2 = s.get_monitor_socket()
     assert m is m2
     s.disable_monitor()
     evt = recv_monitor_message(m)
     self.assertEqual(evt['event'], zmq.EVENT_MONITOR_STOPPED)
     m.close()
     s.close()
コード例 #33
0
ファイル: test_monitor.py プロジェクト: zeromq/pyzmq
 def test_monitor_repeat(self):
     s = self.socket(zmq.PULL)
     m = s.get_monitor_socket()
     self.sockets.append(m)
     m2 = s.get_monitor_socket()
     assert m is m2
     s.disable_monitor()
     evt = recv_monitor_message(m)
     self.assertEqual(evt['event'], zmq.EVENT_MONITOR_STOPPED)
     m.close()
     s.close()
コード例 #34
0
    def run(self):

        context = zmq.Context()
        socket = context.socket(zmq.ROUTER)
        monitor = socket.get_monitor_socket()
        ip = get_ip()
        port_zmq = str(self.__port)
        socket.bind("tcp://" + ip + ":" + port_zmq)

        poller = zmq.Poller()
        poller.register(monitor, zmq.POLLIN | zmq.POLLERR)
        poller.register(socket, zmq.POLLIN | zmq.POLLERR)

        EVENT_MAP = {}
        for name in dir(zmq):
            if name.startswith('EVENT_'):
                value = getattr(zmq, name)
                EVENT_MAP[value] = name

        self.__devices_access.selector = poller
        while True:
            socks = dict(poller.poll())
            if socket in socks:
                [identity, message] = socket.recv_multipart()
                message = pickle.loads(message)
                try:
                    func = getattr(self, message[0])
                    ret = func(*message[1:])
                    ret = pickle.dumps(ret)
                    socket.send_multipart([identity, ret])
                except AttributeError:
                    socket.send_multipart([identity, b"Error"])
            if monitor in socks:
                evt = recv_monitor_message(monitor)
                evt.update({'description': EVENT_MAP[evt['event']]})
                # logger.info("Event: {}".format(evt))

            if self.__devices_access.fileno() in socks:
                dev_ac = self.__devices_access
                try:
                    poller.unregister(dev_ac)
                    logger.debug("The ADC device unregistered from the poller "
                                 "selector")
                except KeyError:
                    logger.warning("The ADC device not available to unregister"
                                   " from the poller selector, expose")

                [timestamp, pre_post, data] = dev_ac.retrieve_ADC_data()
                data = {
                    'function_name': 'update_data',
                    'args':
                    [timestamp, pre_post, data, dev_ac.unique_ADC_name]
                }
                self.server_publisher.send_message(data)
コード例 #35
0
 def evt_monitor(monitor):
     while monitor.poll():
         evt = recv_monitor_message(monitor)
         evt.update({'description': evt_map[evt['event']]})
         # TODO - uncomment - old connection not dying
         # print("Event: {}".format(evt))
         if evt['event'] == zmq.EVENT_MONITOR_STOPPED:
             break
     monitor.close()
     print()
     print('event monitor stopped.')
コード例 #36
0
ファイル: zstack.py プロジェクト: marchon/indy-plenum
 def get_monitor_events(monitor_socket, non_block=True):
     events = []
     # noinspection PyUnresolvedReferences
     flags = zmq.NOBLOCK if non_block else 0
     while True:
         try:
             # noinspection PyUnresolvedReferences
             message = recv_monitor_message(monitor_socket, flags)
             events.append(message)
         except zmq.Again:
             break
     return events
コード例 #37
0
ファイル: zstack.py プロジェクト: michaeldboyd/indy-plenum
 def get_monitor_events(monitor_socket, non_block=True):
     events = []
     # noinspection PyUnresolvedReferences
     flags = zmq.NOBLOCK if non_block else 0
     while True:
         try:
             # noinspection PyUnresolvedReferences
             message = recv_monitor_message(monitor_socket, flags)
             events.append(message)
         except zmq.Again:
             break
     return events
コード例 #38
0
    def host_management_cb(self, sock, mask):
        """

        :param sock: zmq socket
        :param mask: event mask
        :return:

        """
        if self.pushSock is not None and sock == self.pushSock.monitor:
            self.pushSock.monitorHandler(recv_monitor_message(sock))
            return
        if self.pullSock is not None and sock == self.pullSock.monitor:
            self.pullSock.monitorHandler(recv_monitor_message(sock))
            return
        if self.mgrConnection is not None and sock == self.mgrConnection.monitor:
            self.mgrConnection.monitorHandler(recv_monitor_message(sock))
            return

        while sock.getsockopt(zmq.EVENTS) and zmq.POLLIN:
            if not self.hal_message_cb(sock):
                break
コード例 #39
0
 def connect_to_hal(self):
     self.connectionSetup()
     self.register(self.drvID)
     i = 0
     while i < 5:
         socks = self.poller.poll(1000)
         print socks
         i += 1
         if not socks:
             continue
         for sock in socks:
             if self.pushSock is not None and sock == self.pushSock.monitor:
                 self.pushSock.monitorHandler(recv_monitor_message(sock))
                 continue
             if self.pullSock is not None and sock == self.pullSock.monitor:
                 self.pullSock.monitorHandler(recv_monitor_message(sock))
                 continue
             if self.mgrConnection is not None and sock == self.mgrConnection.monitor:
                 self.mgrConnection.monitorHandler(
                     recv_monitor_message(sock))
                 continue
             if socks[sock] == HalPoller.POLLIN:
                 try:
                     bin = sock.recv(flags=zmq.NOBLOCK)
                     msg = HalMessage.DeSerialize(bin)
                     print msg.msg
                     self.logger.debug("Got a zmq msg:%s" % msg.msg)
                     if msg.type in self.HalMsgsHandler:
                         handler = self.HalMsgsHandler[msg.type]
                         handler(msg)
                     else:
                         self.logger.error("Unsupported msg type:%s" %
                                           msg.type)
                 except zmq.ZMQError as e:
                     self.logger.debug(
                         "Got an error when trying with non-block read:" +
                         str(e))
                 except Exception as e:
                     self.logger.error("Error happens, reason:%s" % str(e))
             continue
コード例 #40
0
ファイル: zmq_proxy.py プロジェクト: kaithar/muhubot
    def main_loop(self, handshaking):
        try:
            self.ident = handshaking
            self.log('In proc')
            port = self.create_socket()
            self.log('Got port {}'.format(port))
            self.input_queue.put((port, 'PORT', handshaking))
            while True:
                if (self.socket.poll(timeout=100)):
                    request = self.socket.recv_multipart(copy=False)
                    pa = request[-1].get(b"Peer-Address")
                    request = [r.bytes for r in request]
                    self.input_queue.put((port,'RECV',pa,request))

                try:
                    o = self.output_queue.get(block=False)
                    self.socket.send_multipart(o)
                except queue.Empty:
                    pass
                
                # Check for useful events
                if (self.monitor.closed == False and self.monitor.poll(timeout=100)):
                    evt = recv_monitor_message(self.monitor)
                    evt.update({'description': EVENT_MAP[evt['event']]})
                    if evt['event'] not in (zmq.EVENT_CONNECT_RETRIED,
                                            zmq.EVENT_CONNECT_DELAYED,
                                            zmq.EVENT_CLOSED):
                        # Completely ignore these 3 events because they spam too much.
                        self.log("Event: {}".format(evt))
                        if evt['event'] == zmq.EVENT_CONNECTED:
                            self.connected = True
                            self.socket.unbind()
                            self.log('CONNECTION')
                        if evt['event'] == zmq.EVENT_DISCONNECTED:
                            self.log('DISCONNECT')
                            self.context.destroy(1)
                            break
                        if evt['event'] == zmq.EVENT_MONITOR_STOPPED:
                            self.log('Monitor stopped')
                            self.context.destroy(1)
                            break

        except zmq.ZMQError as e:
            self.log('Exception!')
            if e.errno == zmq.ETERM:
                pass           # Interrupted
            else:
                self.log(''.join(traceback.format_exc(None)))
        except:
            self.log(''.join(traceback.format_exc(None)))
        self.log('Exiting thread!')
コード例 #41
0
ファイル: core.py プロジェクト: Kisensum/volttron
        def monitor():
            # Call socket.monitor() directly rather than use
            # get_monitor_socket() so we can use green sockets with
            # regular contexts (get_monitor_socket() uses
            # self.context.socket()).
            addr = 'inproc://monitor.v-%d' % (id(self.socket),)
            sock = None
            if self.socket is not None:
                try:
                    self.socket.monitor(addr)
                    sock = zmq.Socket(self.context, zmq.PAIR)

                    sock.connect(addr)
                    while True:
                        try:
                            message = recv_monitor_message(sock)
                            self.onsockevent.send(self, **message)
                            event = message['event']
                            if event & zmq.EVENT_CONNECTED:
                                hello()
                            elif event & zmq.EVENT_DISCONNECTED:
                                self.__connected = False
                            elif event & zmq.EVENT_CONNECT_RETRIED:
                                self._reconnect_attempt += 1
                                if self._reconnect_attempt == 50:
                                    self.__connected = False
                                    sock.disable_monitor()
                                    self.stop()
                                    self.ondisconnected.send(self)
                            elif event & zmq.EVENT_MONITOR_STOPPED:
                                break
                        except ZMQError as exc:
                            if exc.errno == ENOTSOCK:
                                break

                except ZMQError as exc:
                    raise
                    # if exc.errno == EADDRINUSE:
                    #     pass
                finally:
                    try:
                        url = list(urlparse.urlsplit(self.address))
                        if url[0] in ['tcp'] and sock is not None:
                            sock.close()
                        if self.socket is not None:
                            self.socket.monitor(None, 0)
                    except Exception as exc:
                        _log.debug("Error in closing the socket: {}".format(exc.message))
コード例 #42
0
ファイル: drone.py プロジェクト: nghiemnv/beeswarm
 def monitor_worker(self, monitor_socket, log_name):
     monitor_socket.linger = 0
     poller = zmq.Poller()
     poller.register(monitor_socket, zmq.POLLIN)
     while True:
         socks = poller.poll(0)
         if len(socks) > 0:
             data = recv_monitor_message(monitor_socket)
             event = data['event']
             value = data['value']
             if event == zmq.EVENT_CONNECTED:
                 logger.info('Connected to {0}'.format(log_name))
                 send_zmq_push('ipc://serverRelay', '{0} {1}'.format(Messages.PING, self.id))
             elif event == zmq.EVENT_DISCONNECTED:
                 logger.warning('Disconnected from {0}, will reconnect in {1} seconds.'.format(log_name, 5))
         gevent.sleep()
コード例 #43
0
ファイル: __init__.py プロジェクト: papr/pupil-interface
    def _sub_loop(self,context,pipe):
        '''
        Subscription Thread Loop

        Connects to the Pupil Server given by `sub_addr:sub_port`.
        Adds
        '''
        socket = context.socket(zmq.SUB)
        network_mon = socket.get_monitor_socket()
        socket.connect(self.sub_addr+':'+self.sub_port)
        #get gaze data only
        socket.setsockopt(zmq.SUBSCRIBE, 'gaze_positions')

        poller = zmq.Poller()
        poller.register(pipe, zmq.POLLIN)
        poller.register(socket, zmq.POLLIN)
        poller.register(network_mon, zmq.POLLIN)

        while True:
            try:
                #this should not fail but it does sometimes. We need to clean this out.
                # I think we are not treating sockets correclty as they are not thread-save.
                items = dict(poller.poll())
            except zmq.ZMQError:
                logger.warning('Socket fail.')
                continue

            if network_mon in items and items[network_mon] == zmq.POLLIN:
                mon_msg = recv_monitor_message(network_mon)
                if mon_msg['event'] == zmq.EVENT_CONNECTED:
                    self.queueEvent({'net_subscription':mon_msg})
                # TODO: disconnect event?

            # get socket events
            if socket in items and items[socket] == zmq.POLLIN:
                topic,msg  = socket.recv_multipart()
                data = json.loads(msg)
                self.queueEvent({topic:data})

            if pipe in items and items[pipe] == zmq.POLLIN:
                message = pipe.recv()
                # message to quit
                if message.decode('utf-8') == exit_thread:
                    break
        self.sub_pipe = None
        network_mon.close()
コード例 #44
0
ファイル: remote.py プロジェクト: michaeldboyd/indy-plenum
 def _get_monitor_events(socket, non_block=True):
     # It looks strange to call get_monitor_socket() each time we
     # want to get it instead of get it once and save reference.
     # May side effects here, will create a ticket to check and clean
     # up the implementation.
     monitor = socket.get_monitor_socket()
     events = []
     # noinspection PyUnresolvedReferences
     flags = zmq.NOBLOCK if non_block else 0
     while True:
         try:
             # noinspection PyUnresolvedReferences
             message = recv_monitor_message(monitor, flags)
             events.append(message['event'])
         except zmq.Again:
             break
     return events
コード例 #45
0
ファイル: zmq_logger.py プロジェクト: fork42541/heralding
 def monitor_worker(self):
     monitor_socket = self.socket.get_monitor_socket()
     monitor_socket.linger = 0
     poller = zmq.Poller()
     poller.register(monitor_socket, zmq.POLLIN)
     while self.enabled:
         socks = poller.poll()
         if len(socks) > 0:
             data = recv_monitor_message(monitor_socket)
             event = data['event']
             if event == zmq.EVENT_CONNECTED:
                 logger.warning('Connected to {0}'.format(self.zmq_socket_url))
             elif event == zmq.EVENT_DISCONNECTED:
                 logger.warning('Connection to {0} was disconencted'.format(self.zmq_socket_url))
             elif event == zmq.EVENT_CONNECT_RETRIED:
                 logger.warning('Retrying connect to {0}'.format(self.zmq_socket_url))
         gevent.sleep()
コード例 #46
0
ファイル: zmq_tools.py プロジェクト: pupil-labs/pupil
    def __init__(self, ctx, url, block_until_connected=True):
        self.socket = zmq.Socket(ctx, zmq.PAIR)

        if block_until_connected:
            # connect node and block until a connecetion has been made
            monitor = self.socket.get_monitor_socket()
            self.socket.connect(url)
            while True:
                status = recv_monitor_message(monitor)
                if status["event"] == zmq.EVENT_CONNECTED:
                    break
                elif status["event"] == zmq.EVENT_CONNECT_DELAYED:
                    pass
                else:
                    raise Exception("ZMQ connection failed")
            self.socket.disable_monitor()
        else:
            self.socket.connect(url)
コード例 #47
0
 def loop(self):
     while True:
         if (self.socket.poll(timeout=100)):
             request = self.socket.recv_multipart(copy=False)
             fd = request[-1].get(zmq.SRCFD)
             pa = request[-1].get(b"Peer-Address")
             request = [r.bytes for r in request]
             print('{}({}/{}) -> {}: {}'.format(request[0], fd, pa, request[1], repr(request[2:])))
             handler = getattr(self, 'handle_{}'.format(request[1].decode('utf-8')), None)
             try:
                 if handler:
                     handler(*request)
             except:
                 traceback.print_exc(None)
             #if request[0] in self.servers:
             #    print('Setting fd for {} to {}'.format(request[0], fd))
             #    self.servers[request[0]]['fd'] = fd
             #self.fds[fd] = request[0]
         if (self.monitor.poll(timeout=100)):
             evt = recv_monitor_message(self.monitor)
             evt.update({'description': EVENT_MAP[evt['event']]})
             print("Event: {}".format(evt))
             #if evt['event'] == zmq.EVENT_DISCONNECTED:
                 #print(self.subs)
                 #print(self.servers)
                 #if evt['value'] in self.fds:
                 #    ident = self.fds[evt['value']]
                 #    print('DISCONNECT: {}'.format(ident))
                 #     if ident in self.servers:
                 #         print('Clearing server')
                 #         for c in self.servers[ident]['subs']:
                 #             print('Clearing {}'.format(c))
                 #             if (c in self.subs):
                 #                 print('Unsubbing {}'.format(c))
                 #                 self.subs[c].remove(ident)
                 #         del self.servers[ident]
                 # print(self.subs)
                 # print(self.servers)
                 # print('')
             if evt['event'] == zmq.EVENT_MONITOR_STOPPED:
                 break
     self.socket.close()
     self.context.term()
コード例 #48
0
 def thread(self):
     print('in thread')
     try:
         while self.running:
             if (self.socket.poll(timeout=100)):
                 data = self.socket.recv_multipart()
                 if (sys.version_info.major == 3):
                     safe_data = [d.decode('utf-8') for d in data]
                 else:
                     safe_data = [d for d in data]
                 print('(Thread loop) '+repr(safe_data)[0:100])
                 handler = getattr(self, 'handle_{}'.format(safe_data[0]), None)
                 if handler:
                     self.call_safely(handler, safe_data)
             if (self.monitor.poll(timeout=100)):
                 evt = recv_monitor_message(self.monitor)
                 evt.update({'description': EVENT_MAP[evt['event']]})
                 if evt['event'] not in (zmq.EVENT_CONNECT_RETRIED,
                                         zmq.EVENT_CONNECT_DELAYED,
                                         zmq.EVENT_CLOSED):
                     # Completely ignore these 3 events because they spam too much.
                     print("Event: {}".format(evt))
                     if evt['event'] == zmq.EVENT_CONNECTED:
                         self.connected = True
                         self.send_multipart('CONNECT')
                         for c in self.subs:
                             self.send_multipart('SUB', c)
                         while self.message_queue:
                             self.raw_send_multipart(self.message_queue.pop(0))
                     if evt['event'] == zmq.EVENT_DISCONNECTED:
                         print('DISCONNECT')
                         self.connected = False
                     if evt['event'] == zmq.EVENT_MONITOR_STOPPED:
                         break
     except zmq.ZMQError as e:
         print('Exception!')
         if e.errno == zmq.ETERM:
             pass           # Interrupted
         else:
             raise
     print('Exiting thread!')
コード例 #49
0
ファイル: core.py プロジェクト: cbs-iiith/volttron
 def monitor():
     # Call socket.monitor() directly rather than use
     # get_monitor_socket() so we can use green sockets with
     # regular contexts (get_monitor_socket() uses
     # self.context.socket()).
     addr = 'inproc://monitor.v-%d' % (id(self.socket),)
     self.socket.monitor(addr)
     try:
         sock = zmq.Socket(self.context, zmq.PAIR)
         sock.connect(addr)
         while True:
             message = recv_monitor_message(sock)
             self.onsockevent.send(self, **message)
             event = message['event']
             if event & zmq.EVENT_CONNECTED:
                 hello()
             elif event & zmq.EVENT_DISCONNECTED:
                 self.__connected = False
                 self.ondisconnected.send(self)
     finally:
         self.socket.monitor(None, 0)