Skip to content

Commit

Permalink
Make published() signal more application-friendly
Browse files Browse the repository at this point in the history
The signature has been changed in 83ad60d
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 3d6354e and 21e2b4d90)
  • Loading branch information
KonstantinRitt committed Aug 21, 2017
1 parent 915c2ad commit cb3ac15
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/mqtt/qmqtt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
20 changes: 10 additions & 10 deletions src/mqtt/qmqtt_client_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -355,6 +357,7 @@ void QMQTT::ClientPrivate::onNetworkDisconnected()

stopKeepAlive();
_midToTopic.clear();
_midToMessage.clear();
emit q->disconnected();
}

Expand Down Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions src/mqtt/qmqtt_client_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class ClientPrivate
QByteArray _willMessage;
QHash<QAbstractSocket::SocketError, ClientError> _socketErrorHash;
QHash<quint16, QString> _midToTopic;
QHash<quint16, Message> _midToMessage;

Client* const q_ptr;

Expand Down
6 changes: 3 additions & 3 deletions tests/gtest/tests/clienttest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<quint16>());
EXPECT_EQ(QOS0, spy.at(0).at(1).value<quint8>());
EXPECT_EQ(message, spy.at(0).at(0).value<QMQTT::Message>());
EXPECT_EQ(msgid, spy.at(0).at(1).value<quint16>());
}

// todo: network received sends a puback, test what happens
Expand Down

0 comments on commit cb3ac15

Please sign in to comment.