From cb3ac1582f2d103905ccad71a905f3ef3886d449 Mon Sep 17 00:00:00 2001 From: Konstantin Ritt Date: Mon, 21 Aug 2017 17:24:12 +0300 Subject: [PATCH] Make published() signal more application-friendly The signature has been changed in 83ad60daee086b0fdf501 to return just msgid and qos, forcing the application to track all messages queued for publishing just to recover them by msgid later, if used. The messages queue will be required for implementing the proper messages delivery ordering at QOS1 and QOS2, so start tracking published messages in qmqtt (somewhat similar to 3d6354e9f and 21e2b4d90) --- src/mqtt/qmqtt_client.h | 2 +- src/mqtt/qmqtt_client_p.cpp | 20 ++++++++++---------- src/mqtt/qmqtt_client_p.h | 1 + tests/gtest/tests/clienttest.cpp | 6 +++--- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/mqtt/qmqtt_client.h b/src/mqtt/qmqtt_client.h index 2e0150f..dd0de6d 100644 --- a/src/mqtt/qmqtt_client.h +++ b/src/mqtt/qmqtt_client.h @@ -199,7 +199,7 @@ public slots: void subscribed(const QString& topic, const quint8 qos = 0); void unsubscribed(const QString& topic); - void published(const quint16 msgid, const quint8 qos); + void published(const QMQTT::Message& message, quint16 msgid = 0); void received(const QMQTT::Message& message); void pingresp(); diff --git a/src/mqtt/qmqtt_client_p.cpp b/src/mqtt/qmqtt_client_p.cpp index d85212d..ba8b2e6 100644 --- a/src/mqtt/qmqtt_client_p.cpp +++ b/src/mqtt/qmqtt_client_p.cpp @@ -327,7 +327,9 @@ quint16 QMQTT::ClientPrivate::publish(const Message& message) // Emit published only at QOS0 if (message.qos() == QOS0) - emit q->published(msgid, QOS0); + emit q->published(message, msgid); + else + _midToMessage[msgid] = message; return msgid; } @@ -355,6 +357,7 @@ void QMQTT::ClientPrivate::onNetworkDisconnected() stopKeepAlive(); _midToTopic.clear(); + _midToMessage.clear(); emit q->disconnected(); } @@ -444,15 +447,12 @@ void QMQTT::ClientPrivate::handlePuback(const quint8 type, const quint16 msgid) { sendPuback(PUBCOMP, msgid); } - - // Emit published on PUBACK at QOS1 - if (type == PUBACK) - emit q->published(msgid, QOS1); - - // Emit published on PUBCOMP at QOS2 - if (type == PUBCOMP) - emit q->published(msgid, QOS2); - + else if (type == PUBACK || type == PUBCOMP) + { + // Emit published on PUBACK at QOS1 and on PUBCOMP at QOS2 + const Message &message = _midToMessage.take(msgid); + emit q->published(message, msgid); + } } void QMQTT::ClientPrivate::handlePingresp() { diff --git a/src/mqtt/qmqtt_client_p.h b/src/mqtt/qmqtt_client_p.h index 354e7dd..c312346 100644 --- a/src/mqtt/qmqtt_client_p.h +++ b/src/mqtt/qmqtt_client_p.h @@ -76,6 +76,7 @@ class ClientPrivate QByteArray _willMessage; QHash _socketErrorHash; QHash _midToTopic; + QHash _midToMessage; Client* const q_ptr; diff --git a/tests/gtest/tests/clienttest.cpp b/tests/gtest/tests/clienttest.cpp index ae4b6bf..62471c8 100644 --- a/tests/gtest/tests/clienttest.cpp +++ b/tests/gtest/tests/clienttest.cpp @@ -405,11 +405,11 @@ TEST_F(ClientTest, publishEmitsPublishedSignal_Test) QSignalSpy spy(_client.data(), &QMQTT::Client::published); QMQTT::Message message(222, "topic", QByteArray("payload")); - _client->publish(message); + quint16 msgid = _client->publish(message); ASSERT_EQ(1, spy.count()); - EXPECT_EQ(message.id(), spy.at(0).at(0).value()); - EXPECT_EQ(QOS0, spy.at(0).at(1).value()); + EXPECT_EQ(message, spy.at(0).at(0).value()); + EXPECT_EQ(msgid, spy.at(0).at(1).value()); } // todo: network received sends a puback, test what happens