Esempio n. 1
0
    def _format_response(msg_header, msg_body, status):
        """
        Format a response
        :param msg_header: The header portion of a received request
        :param msg_body: The response body
        :param status: True is this represents a successful response
        :return: a InterContainerMessage message type
        """
        try:
            response = InterContainerMessage()
            response_body = InterContainerResponseBody()
            response.header.id = msg_header.id
            response.header.timestamp.GetCurrentTime()
            response.header.type = MessageType.Value("RESPONSE")
            response.header.from_topic = msg_header.to_topic
            response.header.to_topic = msg_header.from_topic
            if isinstance(msg_body, Deferred):
                msg_body = msg_body.result
            if msg_body is not None:
                response_body.result.Pack(msg_body)
            response_body.success = status
            response.body.Pack(response_body)
            return response

        except Exception as e:
            log.exception("formatting-response-failed",
                          header=msg_header,
                          body=msg_body,
                          status=status,
                          e=e)
            return None
 def _parse_response(self, msg):
     try:
         message = InterContainerMessage()
         message.ParseFromString(msg)
         resp = InterContainerResponseBody()
         if message.body.Is(InterContainerResponseBody.DESCRIPTOR):
             message.body.Unpack(resp)
         else:
             log.debug("unsupported-msg", msg_type=type(message.body))
             return None
         log.debug("parsed-response", input=message, output=resp)
         return resp
     except Exception as e:
         log.exception("parsing-response-failed", msg=msg, e=e)
         return None
Esempio n. 3
0
 def _parse_response(self, msg):
     try:
         message = InterContainerMessage()
         message.ParseFromString(msg)
         resp = InterContainerResponseBody()
         if message.body.Is(InterContainerResponseBody.DESCRIPTOR):
             message.body.Unpack(resp)
         else:
             log.debug("unsupported-msg", msg_type=type(message.body))
             return None
         log.debug("parsed-response",
                   type=message.header.type,
                   from_topic=message.header.from_topic,
                   to_topic=message.header.to_topic,
                   transaction_id=message.header.id)
         return resp
     except Exception as e:
         log.exception("parsing-response-failed", msg=msg, e=e)
         return None