Exemplo n.º 1
0
    def send(self, message_type, data, connection_id, callback=None):
        """
        Send a message of message_type
        :param connection_id: the identity for the connection to send to
        :param message_type: validator_pb2.Message.* enum value
        :param data: bytes serialized protobuf
        :return: future.Future
        """
        if connection_id not in self._connections:
            raise ValueError("Unknown connection id: %s", connection_id)
        connection_info = self._connections.get(connection_id)
        if connection_info.connection_type == \
                ConnectionType.ZMQ_IDENTITY:
            message = validator_pb2.Message(correlation_id=_generate_id(),
                                            content=data,
                                            message_type=message_type)

            timer_tag = get_enum_name(message.message_type)
            timer_ctx = self._get_send_response_timer(timer_tag).time()
            fut = future.Future(message.correlation_id,
                                message.content,
                                callback,
                                timer_ctx=timer_ctx)

            self._futures.put(fut)

            self._send_receive_thread.send_message(msg=message,
                                                   connection_id=connection_id)
            return fut

        return connection_info.connection.send(message_type,
                                               data,
                                               callback=callback)
Exemplo n.º 2
0
    def send(self, message_type, data, callback=None):
        """Sends a message of message_type

        Args:
            message_type (validator_pb2.Message): enum value
            data (bytes): serialized protobuf
            callback (function): a callback function to call when a
                response to this message is received

        Returns:
            future.Future
        """
        message = validator_pb2.Message(correlation_id=_generate_id(),
                                        content=data,
                                        message_type=message_type)

        fut = future.Future(
            message.correlation_id,
            message.content,
            has_callback=True if callback is not None else False)

        if callback is not None:
            fut.add_callback(callback)

        self._futures.put(fut)

        self._send_receive_thread.send_message(message)
        return fut
Exemplo n.º 3
0
    def send(self, message_type, data, connection_id, callback=None):
        """
        Send a message of message_type
        :param connection_id: the identity for the connection to send to
        :param message_type: validator_pb2.Message.* enum value
        :param data: bytes serialized protobuf
        :return: future.Future
        """
        if connection_id not in self._connections:
            raise ValueError("Unknown connection id: %s", connection_id)
        connection_info = self._connections.get(connection_id)
        if connection_info.connection_type == \
                ConnectionType.ZMQ_IDENTITY:
            message = validator_pb2.Message(correlation_id=_generate_id(),
                                            content=data,
                                            message_type=message_type)

            fut = future.Future(
                message.correlation_id,
                message.content,
                has_callback=True if callback is not None else False)

            if callback is not None:
                fut.add_callback(callback)

            self._futures.put(fut)

            self._send_receive_thread.send_message(msg=message,
                                                   connection_id=connection_id)
            return fut
        else:
            return connection_info.connection.send(message_type,
                                                   data,
                                                   callback=callback)
Exemplo n.º 4
0
    def _do_heartbeat(self):

        ping = PingRequest()

        while True:
            try:
                if self._socket.getsockopt(zmq.TYPE) == zmq.ROUTER:
                    expired = \
                        [ident for ident in self._last_message_times
                         if time.time() - self._last_message_times[ident] >
                         self._heartbeat_interval]
                    for zmq_identity in expired:
                        if self._is_connection_lost(
                                self._last_message_times[zmq_identity]):
                            LOGGER.debug(
                                "No response from %s in %s seconds"
                                " - removing connection.", zmq_identity,
                                self._connection_timeout)
                            self.remove_connected_identity(zmq_identity)
                        else:
                            message = validator_pb2.Message(
                                correlation_id=_generate_id(),
                                content=ping.SerializeToString(),
                                message_type=validator_pb2.Message.PING_REQUEST
                            )
                            fut = future.Future(
                                message.correlation_id,
                                message.content,
                            )
                            self._futures.put(fut)
                            message_frame = [
                                bytes(zmq_identity),
                                message.SerializeToString()
                            ]
                            yield from self._send_message_frame(message_frame)
                elif self._socket.getsockopt(zmq.TYPE) == zmq.DEALER:
                    if self._last_message_time and \
                            self._is_connection_lost(self._last_message_time):
                        LOGGER.debug(
                            "No response from %s in %s seconds"
                            " - removing connection.", self._connection,
                            self._connection_timeout)
                        connection_id = hashlib.sha512(
                            self.connection.encode()).hexdigest()
                        if connection_id in self._connections:
                            del self._connections[connection_id]
                        yield from self._stop()
                yield from asyncio.sleep(self._heartbeat_interval)
            except CancelledError:
                # The concurrent.futures.CancelledError is caught by asyncio
                # when the Task associated with the coroutine is cancelled.
                # The raise is required to stop this component.
                raise
            except Exception as e:  # pylint: disable=broad-except
                LOGGER.exception(
                    "An error occurred while sending heartbeat: %s", e)
Exemplo n.º 5
0
    def send(self, message_type, data):
        """
        Send a message of message_type
        :param message_type: validator_pb2.Message.* enum value
        :param data: bytes serialized protobuf
        :return: future.Future
        """
        message = validator_pb2.Message(correlation_id=_generate_id(),
                                        content=data,
                                        message_type=message_type)

        fut = future.Future(message.correlation_id, message.content)
        self._futures.put(fut)

        self._send_receive_thread.send_message(message)
        return fut
Exemplo n.º 6
0
    def send(self, message_type, data, identity, has_callback=False):
        """
        Send a message of message_type
        :param identity: the zmq identity of the dealer to send to or None
        :param message_type: validator_pb2.Message.* enum value
        :param data: bytes serialized protobuf
        :return: future.Future
        """
        message = validator_pb2.Message(correlation_id=_generate_id(),
                                        content=data,
                                        message_type=message_type)

        fut = future.Future(message.correlation_id,
                            message.content,
                            has_callback=has_callback)
        self._futures.put(fut)

        self._send_receive_thread.send_message(msg=message, identity=identity)
        return fut
Exemplo n.º 7
0
    def _do_heartbeat(self):
        with self._condition:
            self._condition.wait_for(lambda: self._socket is not None)

        ping = PingRequest()

        while True:
            if self._socket.getsockopt(zmq.TYPE) == zmq.ROUTER:
                expired = [
                    ident for ident in self._last_message_times
                    if time.time() -
                    self._last_message_times[ident] > self._heartbeat_interval
                ]
                for zmq_identity in expired:
                    if self._is_connection_lost(
                            self._last_message_times[zmq_identity]):
                        LOGGER.debug(
                            "No response from %s in %s seconds"
                            " - removing connection.", zmq_identity,
                            self._connection_timeout)
                        self._remove_connected_identity(zmq_identity)
                    else:
                        message = validator_pb2.Message(
                            correlation_id=_generate_id(),
                            content=ping.SerializeToString(),
                            message_type=validator_pb2.Message.NETWORK_PING)
                        fut = future.Future(message.correlation_id,
                                            message.content,
                                            has_callback=False)
                        self._futures.put(fut)
                        yield from self._send_message(zmq_identity, message)
            elif self._socket.getsockopt(zmq.TYPE) == zmq.DEALER:
                if self._last_message_time:
                    if self._is_connection_lost(self._last_message_time):
                        LOGGER.debug(
                            "No response from %s in %s seconds"
                            " - removing connection.", self._connection,
                            self._connection_timeout)
                        yield from self._stop()
            yield from asyncio.sleep(self._heartbeat_interval)
Exemplo n.º 8
0
    def send_last_message(self, message_type, data, callback=None):
        """Sends a message of message_type and then close the connection.

        Args:
            message_type (validator_pb2.Message): enum value
            data (bytes): serialized protobuf
            callback (function): a callback function to call when a
                response to this message is received

        Returns:
            future.Future
        """
        message = validator_pb2.Message(correlation_id=_generate_id(),
                                        content=data,
                                        message_type=message_type)

        fut = future.Future(message.correlation_id, message.content, callback)

        self._futures.put(fut)

        self._send_receive_thread.send_last_message(message)
        return fut
Exemplo n.º 9
0
    def send(self, message_type, data, connection_id, has_callback=False):
        """
        Send a message of message_type
        :param connection_id: the identity for the connection to send to
        :param message_type: validator_pb2.Message.* enum value
        :param data: bytes serialized protobuf
        :return: future.Future
        """
        connection_type, connection = self._connections.get(connection_id)
        if connection_type == "ZMQ_Identity":
            message = validator_pb2.Message(correlation_id=_generate_id(),
                                            content=data,
                                            message_type=message_type)

            fut = future.Future(message.correlation_id,
                                message.content,
                                has_callback=has_callback)
            self._futures.put(fut)

            self._send_receive_thread.send_message(msg=message,
                                                   connection_id=connection_id)
            return fut
        else:
            return connection.send(message_type, data)
Exemplo n.º 10
0
    def _send_heartbeat(self):
        with self._condition:
            self._condition.wait_for(lambda: self._socket is not None)

        ping = PingRequest()

        while True:
            if self._socket.getsockopt(zmq.TYPE) == zmq.ROUTER:
                expired = [
                    ident for ident in self._connected_identities
                    if time.time() - self._connected_identities[ident] >
                    self._heartbeat_interval
                ]
                for zmq_identity in expired:
                    message = validator_pb2.Message(
                        correlation_id=_generate_id(),
                        content=ping.SerializeToString(),
                        message_type=validator_pb2.Message.NETWORK_PING)
                    fut = future.Future(message.correlation_id,
                                        message.content,
                                        has_callback=False)
                    self._futures.put(fut)
                    yield from self._send_message(zmq_identity, message)
            yield from asyncio.sleep(self._heartbeat_interval)