Пример #1
0
    def GenerationMethod(resolve, reject):
      message = _GetMessage(method, flags, None, *args, **kwargs)
      if method.response_struct:
        def Accept(message):
          try:
            assert message.header.message_type == method.ordinal
            payload = message.payload
            response = method.response_struct.Deserialize(
                serialization.RootDeserializationContext(payload.data,
                                                         payload.handles))
            as_dict = response.AsDict()
            if len(as_dict) == 1:
              value = as_dict.values()[0]
              if not isinstance(value, dict):
                response = value
            resolve(response)
            return True
          except Exception as e:
            # Adding traceback similarly to python 3.0 (pep-3134)
            e.__traceback__ = sys.exc_info()[2]
            reject(e)
            return False
          finally:
            self._error_handler.RemoveCallback(reject)

        self._error_handler.AddCallback(reject)
        if not self._router.AcceptWithResponder(
            message, messaging.ForwardingMessageReceiver(Accept)):
          self._error_handler.RemoveCallback(reject)
          reject(messaging.MessagingException("Unable to send message."))
      else:
        if (self._router.Accept(message)):
          resolve(None)
        else:
          reject(messaging.MessagingException("Unable to send message."))
Пример #2
0
    def testRequestResponse(self):
        header_data = messaging.MessageHeader(
            0, messaging.MESSAGE_EXPECTS_RESPONSE_FLAG).Serialize()
        message = messaging.Message(header_data)
        back_messages = []

        def OnBackMessage(message):
            back_messages.append(message)
            return True

        self.router.AcceptWithResponder(
            message, messaging.ForwardingMessageReceiver(OnBackMessage))
        self.loop.RunUntilIdle()
        self.assertFalse(self.received_errors)
        self.assertFalse(self.received_messages)
        (res, data, _) = self.handle.ReadMessage(bytearray(len(header_data)))
        self.assertEquals(system.RESULT_OK, res)
        message_header = messaging.MessageHeader.Deserialize(data[0])
        self.assertNotEquals(message_header.request_id, 0)
        response_header_data = messaging.MessageHeader(
            0, messaging.MESSAGE_IS_RESPONSE_FLAG,
            message_header.request_id).Serialize()
        self.handle.WriteMessage(response_header_data)
        self.loop.RunUntilIdle()
        self.assertFalse(self.received_errors)
        self.assertEquals(len(back_messages), 1)
        self.assertEquals(back_messages[0].data, response_header_data)
Пример #3
0
    def setUp(self):
        super(ConnectorTest, self).setUp()
        self.received_messages = []
        self.received_errors = []

        def _OnMessage(message):
            self.received_messages.append(message)
            return True

        def _OnError(result):
            self.received_errors.append(result)

        handles = system.MessagePipe()
        self.connector = messaging.Connector(handles.handle1)
        self.connector.SetIncomingMessageReceiver(
            messaging.ForwardingMessageReceiver(_OnMessage))
        self.connector.SetErrorHandler(
            _ForwardingConnectionErrorHandler(_OnError))
        self.connector.Start()
        self.handle = handles.handle0