Ejemplo n.º 1
0
    def _get_response_message(self):
        """
Returns the IPC response message read from the socket.

:param timeout: Alternative timeout value

:return: (object) Message instance
:since:  v0.3.00
        """

        # pylint: disable=broad-except

        if (self.log_handler is not None): self.log_handler.debug("#echo(__FILEPATH__)# -{0!r}._get_message()- (#echo(__LINE__)#)", self, context = "pas_bus")

        data_unread = 16
        message_data = Binary.BYTES_TYPE()
        message_size = 0
        timeout_time = time() + self.timeout

        while ((data_unread > 0 or message_size == 0) and time() < timeout_time):
            select([ self.socket.fileno() ], [ ], [ ], self.timeout)
            data = self.socket.recv(data_unread)

            if (len(data) > 0):
                message_data += data

                if (message_size < 1):
                    try:
                        message_size = Message.get_marshaled_message_size(message_data)
                        data_unread = (message_size - len(message_data))
                    except IOException: pass
                else: data_unread -= len(data)
            #
        #

        if (message_size == 0): raise IOException("Timeout occurred before message size was calculable")
        elif (len(message_data) < message_size): raise IOException("Timeout occurred before expected message size of {0:d} bytes was received".format(message_size))

        return Message.unmarshal(message_data)
Ejemplo n.º 2
0
    def test_simple_marshaled_data(self):
        test_data = Binary.bytes(
            "l\x01\x03\x01\x03\x00\x00\x00\x01\x00\x00\x00\x03\x00\x00\x00\x01\x01o\x00\x02\x00\x00\x00/t\x00\x00\x00\x00\x00\x00\x03\x01s\x00\x04\x00\x00\x00test\x00\x00\x00\x00\x08\x01g\x00\x03yyy\x00llo"
        )

        self.assertEqual(60, Message.get_marshaled_message_size(test_data))

        result_data = Message.unmarshal_data("yyyyuua(yv)yyy", test_data, True)

        self.assertEqual(10, len(result_data))
        self.assertIs(list, type(result_data[6]))
        self.assertEqual(3, len(result_data[6]))
        self.assertIs(list, type(result_data[6][0]))
        self.assertEqual(2, len(result_data[6][0]))
        self.assertEqual(Message.HEADER_FIELD_PATH, result_data[6][0][0])
        self.assertEqual("/t", Binary.str(result_data[6][0][1]))

        message = Message.unmarshal(test_data)
        self.assertEqual(Message.TYPE_METHOD_CALL, message.get_type())
        self.assertEqual((Message.FLAG_NO_REPLY_EXPECTED | Message.FLAG_NO_AUTO_START), message.get_flags())
        self.assertEqual("/t", message.get_object_path())
        self.assertEqual("test", message.get_object_member())
        self.assertEqual(1, message.get_serial())
Ejemplo n.º 3
0
    def _thread_run(self):
        """
Active conversation

:since: v0.3.00
        """

        # pylint: disable=broad-except

        if self.log_handler is not None:
            self.log_handler.debug(
                "#echo(__FILEPATH__)# -{0!r}._thread_run()- (#echo(__LINE__)#)", self, context="pas_bus"
            )

        data = Binary.BYTES_TYPE()

        while self.socket is not None:
            try:
                data += self.get_data(16)
                request_message_size = 0

                try:
                    request_message_size = Message.get_marshaled_message_size(data)
                except IOException:
                    pass

                if request_message_size > 0 and len(data) >= request_message_size:
                    request_message_data = data[:request_message_size]

                    self._set_data(data[request_message_size:])
                    data = Binary.BYTES_TYPE()

                    request = BusRequest(self, request_message_data)

                    if request.is_close_requested():
                        self.stop()
                    else:
                        request.execute()
                #
            except Exception as handled_exception:
                if self.log_handler is not None:
                    self.log_handler.error(
                        "#echo(__FILEPATH__)# -Connection._thread_run()- reporting: Error {1!r} occurred",
                        self,
                        handled_exception,
                        context="pas_bus",
                    )
Ejemplo n.º 4
0
    def __init__(self, connection):
        """
Constructor __init__(BusResponse)

:param handler: IPC client handler

:since: v0.3.00
        """

        AbstractResponse.__init__(self)

        self.connection = connection
        """
IPC connection to send the result to.
        """
        self.message = Message()
        """
Ejemplo n.º 5
0
    def _get_message_call_template(self):
        """
Returns a D-Bus message prepared to be used for IPC communication.

:return: (object) Message instance
:since:  v0.3.00
        """

        _return = Message(Message.TYPE_METHOD_CALL)
        _return.set_body_signature("a{sv}")
        _return.set_object_interface("de.direct-netware.pas.Bus1")
        _return.set_object_member("call")
        _return.set_object_path("/de/direct-netware/pas/Bus")
        _return.set_serial(1)

        return _return
Ejemplo n.º 6
0
class BusResponse(AbstractResponse):
    """
Bus response sends the result for one executed bus result.

:author:     direct Netware Group et al.
:copyright:  (C) direct Netware Group - All rights reserved
:package:    pas
:subpackage: bus
:since:      v0.3.00
:license:    https://www.direct-netware.de/redirect?licenses;mpl2
             Mozilla Public License, v. 2.0
    """

    def __init__(self, connection):
        """
Constructor __init__(BusResponse)

:param handler: IPC client handler

:since: v0.3.00
        """

        AbstractResponse.__init__(self)

        self.connection = connection
        """
IPC connection to send the result to.
        """
        self.message = Message()
        """
Result message to be send
        """
    #

    def handle_critical_error(self, message):
        """
"handle_critical_error()" is called to send a critical error message.

:param message: Message (will be translated if possible)

:since: v0.3.00
        """

        self.handle_error(message)
    #

    def handle_error(self, message):
        """
"handle_error()" is called to send a error message.

:param message: Message (will be translated if possible)

:since: v0.3.00
        """

        self.message.set_type(Message.TYPE_ERROR)
        self.message.set_error_name("de.direct-netware.pas.Bus.Error")
        self.message.set_body(message)
    #

    def handle_exception(self, message, exception):
        """
"handle_exception()" is called if an exception occurs and should be
send.

:param message: Message (will be translated if possible)
:param exception: Original exception or formatted string (should be shown in
                  dev mode)

:since: v0.3.00
        """

        self.handle_error("{0!r}".format(exception)
                          if (message is None) else
                          message
                         )
    #

    def send(self):
        """
Sends the prepared response.

:since: v0.3.00
        """

        self.message.set_reply_serial(1)
        self.connection.write_data(self.message.marshal(2))
    #

    def set_result(self, result):
        """
Sets the encoded message to be send based on the result given.

:param result: Result data

:since: v0.3.00
        """

        self.message.set_type(Message.TYPE_METHOD_REPLY)
        if (result is not None): self.message.set_body(result)