示例#1
0
 def send_back(self, message_type, correlation_id, content):
     """
     Return a response to a message.
     :param message_type: validator_pb2.Message.MessageType enum value
     :param correlation_id: a random str internal to the validator
     :param content: protobuf bytes
     :raises (ValidatorConnectionError):
     """
     if not self._event.is_set():
         raise ValidatorConnectionError()
     message = validator_pb2.Message(message_type=message_type,
                                     correlation_id=correlation_id,
                                     content=content)
     self._send_recieve_thread.put_message(message)
示例#2
0
    def send(self, message_type, content):
        """Send a message to the validator

        :param: message_type(validator_pb2.Message.MessageType)
        :param: content(bytes)
        :return: (future.Future)
        :raises: (ValidatorConnectionError)
        """

        if not self._event.is_set():
            raise ValidatorConnectionError()
        message = validator_pb2.Message(message_type=message_type,
                                        correlation_id=_generate_id(),
                                        content=content)
        future = Future(message.correlation_id)
        self._futures.put(future)

        self._send_recieve_thread.put_message(message)
        return future
示例#3
0
    def _process(self, msg):
        if msg.message_type != Message.TP_PROCESS_REQUEST:
            LOGGER.debug(
                "Transaction Processor recieved invalid message type. "
                "Message type should be TP_PROCESS_REQUEST,"
                " but is %s", Message.MessageType.Name(msg.message_type))
            return

        request = TpProcessRequest()
        request.ParseFromString(msg.content)
        state = State(self._stream, request.context_id)
        header = TransactionHeader()
        header.ParseFromString(request.header)
        try:
            if not self._stream.is_ready():
                raise ValidatorConnectionError()
            handler = self._find_handler(header)
            if handler is None:
                return
            handler.apply(request, state)
            self._stream.send_back(
                message_type=Message.TP_PROCESS_RESPONSE,
                correlation_id=msg.correlation_id,
                content=TpProcessResponse(
                    status=TpProcessResponse.OK).SerializeToString())
        except InvalidTransaction as it:
            LOGGER.warning("Invalid Transaction %s", it)
            try:
                self._stream.send_back(
                    message_type=Message.TP_PROCESS_RESPONSE,
                    correlation_id=msg.correlation_id,
                    content=TpProcessResponse(
                        status=TpProcessResponse.INVALID_TRANSACTION,
                        message=str(it),
                        extended_data=it.extended_data).SerializeToString())
            except ValidatorConnectionError as vce:
                # TP_PROCESS_REQUEST has made it through the
                # handler.apply and an INVALID_TRANSACTION would have been
                # sent back but the validator has disconnected and so it
                # doesn't care about the response.
                LOGGER.warning("during invalid transaction response: %s", vce)
        except InternalError as ie:
            LOGGER.warning("internal error: %s", ie)
            try:
                self._stream.send_back(
                    message_type=Message.TP_PROCESS_RESPONSE,
                    correlation_id=msg.correlation_id,
                    content=TpProcessResponse(
                        status=TpProcessResponse.INTERNAL_ERROR,
                        message=str(ie),
                        extended_data=ie.extended_data).SerializeToString())
            except ValidatorConnectionError as vce:
                # Same as the prior except block, but an internal error has
                # happened, but because of the disconnect the validator
                # probably doesn't care about the response.
                LOGGER.warning("during internal error response: %s", vce)
        except ValidatorConnectionError as vce:
            # Somewhere within handler.apply a future resolved with an
            # error status that the validator has disconnected. There is
            # nothing left to do but reconnect.
            LOGGER.warning(
                "during handler.apply a future was resolved "
                "with error status: %s", vce)
示例#4
0
 def message_type(self):
     raise ValidatorConnectionError()
示例#5
0
 def content(self):
     raise ValidatorConnectionError()