Beispiel #1
0
    def handle(self, msg):
        """\
        Handle a message and call the appropriate method.

        :param msg: A parsed message object.
        :returns: Whether the call was successful.
        """
        try:
            handler = getattr(self,
                    'handle_v' + msg.mversion.replace('.', '_'), None)
            if not handler:
                raise LXError("unimplemented version processor")

            prev_item = IDStoreItem()
            if msg.mid in settings.ID_STORE:
                prev_item = settings.ID_STORE[msg.mid]
            settings.ID_STORE[msg.mid] = IDStoreItem(msg, prev_item.callback)

            try:
                prev_item.callback(msg, prev_item.msg)
            except:
                logger.exception("error occurred in callback")

            return handler(msg, prev_item.msg)
        except:
            logger.exception("error occurred in message processor")
            return False
Beispiel #2
0
    def _run_device(self):
        try:
            self.jobs = []
            self.thpool = futures.ThreadPoolExecutor(max_workers=1)
            self.client_sock = secure_socket(
                dealer_socket, self.private_key, self.public_key, self.server_key, self.context
            )
            self.client_sock.monitor(self.state_endpoint)
            self.client_sock.connect(settings.EXCHANGE_ENDPOINT)

            self.client_state_sock = pair_socket(self.context)
            self.client_state_sock.connect(self.state_endpoint)

            self.publisher_sock = secure_socket(
                sub_socket, self.private_key, self.public_key, self.server_key, self.context
            )
            self.publisher_sock.connect(settings.EXCHANGE_PUBLISHER_ENDPOINT)
            self.publisher_sock.subscribe = b""

            self.cmd_sock = router_socket(self.context)
            self.cmd_sock.bind(self.cmd_endpoint)

            self.poller = zmq.Poller()
            self.poller.register(self.client_sock, zmq.POLLIN)
            self.poller.register(self.client_state_sock, zmq.POLLIN)
            self.poller.register(self.publisher_sock, zmq.POLLIN)
            self.poller.register(self.cmd_sock, zmq.POLLIN)

            # Context's I/O loop
            self.__io_loop()

        except:
            logger.exception("error occurred in context I/O loop")

        finally:
            self.client_sock.close()
            self.client_state_sock.close()
            self.publisher_sock.close()
            self.cmd_sock.close()
            self.thpool.shutdown()
Beispiel #3
0
    def handle_state_event(self, state):
        """\
        Handle a state event and call the appropriate method.

        :param state: The resulted dictionary from calling
            :func:`zmq.utils.monitor.parse_monitor_message`.
        :returns: Whether the call was successful.
        """
        event, endpoint = state['event'], state['endpoint']
        try:
            if event == zmq.EVENT_CONNECTED:
                return self.handle_event_connected(endpoint)
            elif event == zmq.EVENT_CONNECT_RETRIED:
                return self.handle_event_retried(endpoint, state['value'])
            elif event == zmq.EVENT_CLOSED:
                return self.handle_event_closed(endpoint)
            elif event == zmq.EVENT_MONITOR_STOPPED:
                return self.handle_event_monitor_stopped(endpoint)
            else:
                return True # XXX: We are not interested in the other events
        except:
            logger.exception("error occurred in message processor")
            return False