From 618e2cc081e09d9222418bd933876224675a7530 Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Wed, 18 Feb 2015 02:13:49 +0100 Subject: [PATCH] dbus: Add method serial() and replySerial() to class DBusMessage. This patch includes setup of class member 'msg' in QDBusMessagePrivate::toDBusMessage() to be able to get the serial after message sending. Testcases for comparing the 'reply serial to' with the 'serial' are included. Task-number: QTBUG-44490 Change-Id: Iae7c48f5b0c70a6c5ae500904072b38b46dfd876 Reviewed-by: Thiago Macieira --- src/dbus/qdbus_symbols_p.h | 2 + src/dbus/qdbusmessage.cpp | 41 +++++++++++++++++++ src/dbus/qdbusmessage.h | 2 + src/dbus/qdbusmessage_p.h | 2 + .../qdbusconnection/tst_qdbusconnection.cpp | 7 ++++ 5 files changed, 54 insertions(+) diff --git a/src/dbus/qdbus_symbols_p.h b/src/dbus/qdbus_symbols_p.h index 1991a462e9..67680f6b82 100644 --- a/src/dbus/qdbus_symbols_p.h +++ b/src/dbus/qdbus_symbols_p.h @@ -286,6 +286,8 @@ DEFINEFUNC(const char* , dbus_message_get_sender, (DBusMessage *message), (message), return) DEFINEFUNC(dbus_uint32_t , dbus_message_get_serial, (DBusMessage *message), (message), return) +DEFINEFUNC(dbus_uint32_t , dbus_message_get_reply_serial, (DBusMessage *message), + (message), return) DEFINEFUNC(const char* , dbus_message_get_signature, (DBusMessage *message), (message), return) DEFINEFUNC(int , dbus_message_get_type, (DBusMessage *message), diff --git a/src/dbus/qdbusmessage.cpp b/src/dbus/qdbusmessage.cpp index 32b7787514..e20c851d6c 100644 --- a/src/dbus/qdbusmessage.cpp +++ b/src/dbus/qdbusmessage.cpp @@ -188,7 +188,12 @@ DBusMessage *QDBusMessagePrivate::toDBusMessage(const QDBusMessage &message, QDB // check if everything is ok if (marshaller.ok) + { + QDBusMessage *m = (QDBusMessage*)&message; + q_dbus_message_ref(msg); + m->d_ptr->msg = msg; return msg; + } // not ok; q_dbus_message_unref(msg); @@ -317,6 +322,16 @@ QDBusMessage QDBusMessagePrivate::makeLocalReply(const QDBusConnectionPrivate &c return QDBusMessage(); // failed } +uint QDBusMessagePrivate::serial() +{ + return msg ? q_dbus_message_get_serial(msg) : reply ? q_dbus_message_get_serial(reply) : 0; +} + +uint QDBusMessagePrivate::replySerial() +{ + return msg ? q_dbus_message_get_reply_serial(msg) : reply ? q_dbus_message_get_reply_serial(reply) : 0; +} + /*! \class QDBusMessage \inmodule QtDBus @@ -632,6 +647,32 @@ QString QDBusMessage::signature() const return d_ptr->signature; } +/*! + Returns the serial of the message or 0 if undefined. + + The serial number is a unique identifier of a message coming from a + given connection. + + The serial is set to a non zero value after the message has been sent + over a D-Bus connection. +*/ +uint QDBusMessage::serial() const +{ + return d_ptr->serial(); +} + +/*! + Returns the serial of the message this is a reply to or 0 if undefined. + + The serial number is a unique identifier of a message coming from a + given connection and D-Bus messages of 'method return' or 'error' type + use them to match the reply to the method call message. +*/ +uint QDBusMessage::replySerial() const +{ + return d_ptr->replySerial(); +} + /*! Returns the flag that indicates if this message should see a reply or not. This is only meaningful for \l {MethodCallMessage}{method diff --git a/src/dbus/qdbusmessage.h b/src/dbus/qdbusmessage.h index e85d600080..f6538bd2cf 100644 --- a/src/dbus/qdbusmessage.h +++ b/src/dbus/qdbusmessage.h @@ -104,6 +104,8 @@ public: QString errorMessage() const; MessageType type() const; QString signature() const; + uint serial() const; + uint replySerial() const; bool isReplyRequired() const; diff --git a/src/dbus/qdbusmessage_p.h b/src/dbus/qdbusmessage_p.h index 5abd490502..0ad9924cac 100644 --- a/src/dbus/qdbusmessage_p.h +++ b/src/dbus/qdbusmessage_p.h @@ -93,6 +93,8 @@ public: const QDBusMessage &asSent); static QDBusMessage makeLocalReply(const QDBusConnectionPrivate &conn, const QDBusMessage &asSent); + uint serial(); + uint replySerial(); }; QT_END_NAMESPACE diff --git a/tests/auto/dbus/qdbusconnection/tst_qdbusconnection.cpp b/tests/auto/dbus/qdbusconnection/tst_qdbusconnection.cpp index e91f87d6c8..a1530a8b86 100644 --- a/tests/auto/dbus/qdbusconnection/tst_qdbusconnection.cpp +++ b/tests/auto/dbus/qdbusconnection/tst_qdbusconnection.cpp @@ -71,6 +71,7 @@ void tst_QDBusConnection::noConnection() QVERIFY(con.callWithCallback(msg, &spy, SLOT(asyncReply)) == 0); QDBusMessage reply = con.call(msg); + QCOMPARE(msg.serial(), reply.replySerial()); QVERIFY(reply.type() == QDBusMessage::ErrorMessage); QDBusReply voidreply(reply); @@ -152,6 +153,7 @@ void tst_QDBusConnection::send() QDBusMessage reply = con.call(msg); + QCOMPARE(msg.serial(), reply.replySerial()); QCOMPARE(reply.arguments().count(), 1); QCOMPARE(reply.arguments().at(0).typeName(), "QStringList"); QVERIFY(reply.arguments().at(0).toStringList().contains(con.baseService())); @@ -171,6 +173,7 @@ void tst_QDBusConnection::sendWithGui() QDBusMessage reply = con.call(msg, QDBus::BlockWithGui); + QCOMPARE(msg.serial(), reply.replySerial()); QCOMPARE(reply.arguments().count(), 1); QCOMPARE(reply.arguments().at(0).typeName(), "QStringList"); QVERIFY(reply.arguments().at(0).toStringList().contains(con.baseService())); @@ -840,6 +843,7 @@ void tst_QDBusConnection::callSelf() QString(), "test3"); msg << 44; reply = connection.call(msg); + QCOMPARE(msg.serial(), reply.replySerial()); QCOMPARE(reply.arguments().value(0).toInt(), 45); } @@ -907,6 +911,7 @@ void tst_QDBusConnection::callSelfByAnotherName() QString(), "test0"); QDBusMessage reply = con.call(msg, QDBus::Block, 1000); + QCOMPARE(msg.serial(), reply.replySerial()); QVERIFY(reply.type() == QDBusMessage::ReplyMessage); } @@ -922,6 +927,7 @@ void tst_QDBusConnection::multipleInterfacesInQObject() QDBusMessage msg = QDBusMessage::createMethodCall(con.baseService(), "/p1", "local.BaseObject", "anotherMethod"); QDBusMessage reply = con.call(msg, QDBus::Block); + QCOMPARE(msg.serial(), reply.replySerial()); QCOMPARE(reply.type(), QDBusMessage::ReplyMessage); QVERIFY(reply.arguments().count() == 0); } @@ -1202,6 +1208,7 @@ void tst_QDBusConnection::callVirtualObjectLocal() QDBusMessage message = QDBusMessage::createMethodCall(con.baseService(), path, QString(), "hello"); QDBusMessage reply = con.call(message, QDBus::Block, 5000); + QCOMPARE(message.serial(), reply.replySerial()); QCOMPARE(obj.callCount, 1); QCOMPARE(obj.lastMessage.service(), con.baseService()); QCOMPARE(obj.lastMessage.interface(), QString());