def _process(self, msg): 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() self._find_handler(header).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 ).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 ).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)
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)
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
def message_type(self): raise ValidatorConnectionError()
def content(self): raise ValidatorConnectionError()