Exemplo n.º 1
0
    def test_create_and_parse(self):
        """
        Test whether a message can be correctly serialised and deserialised again.
        """
        string = create_message_string(self.message)
        self.assertIsNotNone(string)
        self.assertGreater(len(string), 0)

        result = parse_message_string(string)
        self.assertEqual(self.message, result)
Exemplo n.º 2
0
    def test_create_and_parse(self):
        """
        Test whether a message can be correctly serialised and deserialised again.
        """
        string = create_message_string(self.message)
        self.assertIsNotNone(string)
        self.assertGreater(len(string), 0)

        result = parse_message_string(string)
        self.assertEqual(self.message, result)
Exemplo n.º 3
0
    def _parse_message(self, string, protocol):
        """
        Parse the contents of a message. Also do processing outside the reactor
        thread.

        This method should run in a separate thread to prevent blocking the reactor.

        Both the parsed original message and a possible return message are returned
        in a special MessageResult instance.
        
        Parameters
        ----------
        string : string
            Incoming message string to handle.
        protocol
            Twisted protocol object connected to the client.
            
        Returns
        -------
        result : :class:`MessageResult`
            Result of parsing the message.
        
        Raises
        ------
        MessageHandleError
            If any error occurs while handling the message.
        """
        #print("Parsing message: %s" % string)
        msg = parse_message_string(string)
        result = MessageResult(original_message=msg)

        if isinstance(msg, MethodCallMessage):
            # Handle method call
            res = self._method_call(msg)
            response_msg = ResponseMessage(result_code=0, result=res, response_to=msg.id)
            result.response = create_message_string(response_msg)
        elif isinstance(msg, SubscribeMessage):
            # Handle subscription to event
            response_msg = ResponseMessage(result_code=0, result=None, response_to=msg.id)
            result.response = create_message_string(response_msg)
        else:
            raise MessageHandleError(MessageHandleError.RESULT_UNEXPECTED_MESSAGE, msg)

        return result