Skip to content

Commit

Permalink
Merge branch 'master' into fix_002
Browse files Browse the repository at this point in the history
  • Loading branch information
avsdev-cw committed Jul 5, 2017
2 parents df9de16 + c74c442 commit 0279f94
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
26 changes: 23 additions & 3 deletions src/mqtt/qmqtt_client_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,19 +344,22 @@ void QMQTT::ClientPrivate::puback(const quint8 type, const quint16 msgid)
quint16 QMQTT::ClientPrivate::subscribe(const QString& topic, const quint8 qos)
{
quint16 msgid = sendSubscribe(topic, qos);
_midToTopic[msgid] = topic;
return msgid;
}

void QMQTT::ClientPrivate::unsubscribe(const QString& topic)
{
sendUnsubscribe(topic);
quint16 msgid = sendUnsubscribe(topic);
_midToTopic[msgid] = topic;
}

void QMQTT::ClientPrivate::onNetworkDisconnected()
{
Q_Q(Client);

stopKeepAlive();
clearMidToTopic();
emit q->disconnected();
}

Expand Down Expand Up @@ -403,10 +406,11 @@ void QMQTT::ClientPrivate::onNetworkReceived(const QMQTT::Frame& frm)
case SUBACK:
mid = frame.readInt();
qos = frame.readChar();
handleSuback(topic, qos);
handleSuback(midToTopic(mid), qos);
break;
case UNSUBACK:
handleUnsuback(topic);
mid = frame.readInt();
handleUnsuback(midToTopic(mid));
break;
case PINGRESP:

Expand Down Expand Up @@ -476,6 +480,22 @@ void QMQTT::ClientPrivate::handleUnsuback(const QString &topic) {
emit q->unsubscribed(topic);
}

QString QMQTT::ClientPrivate::midToTopic(const quint16 mid)
{
QString result;
QHash<quint16, QString>::Iterator it = _midToTopic.find(mid);
if (it != _midToTopic.end()) {
result = it.value();
_midToTopic.erase(it);
}
return result;
}

void QMQTT::ClientPrivate::clearMidToTopic()
{
_midToTopic.clear();
}

bool QMQTT::ClientPrivate::autoReconnect() const
{
return _network->autoReconnect();
Expand Down
4 changes: 3 additions & 1 deletion src/mqtt/qmqtt_client_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class ClientPrivate
bool _willRetain;
QString _willMessage;
QHash<QAbstractSocket::SocketError, ClientError> _socketErrorHash;

QHash<quint16, QString> _midToTopic;

Client* const q_ptr;

Expand Down Expand Up @@ -105,6 +105,8 @@ class ClientPrivate
void handleUnsuback(const QString& topic);
void handlePubcomp(const Message& message);
void handlePingresp();
QString midToTopic(const quint16 mid);
void clearMidToTopic();
bool autoReconnect() const;
void setAutoReconnect(const bool autoReconnect);
bool autoReconnectInterval() const;
Expand Down
4 changes: 2 additions & 2 deletions src/mqtt/qmqtt_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
#define PINGRESP 0xD0
#define DISCONNECT 0xE0

#define LSB(A) (quint8)(A & 0x00FF)
#define MSB(A) (quint8)((A & 0xFF00) >> 8)
#define LSB(A) quint8(A & 0x00FF)
#define MSB(A) quint8((A & 0xFF00) >> 8)

/*
|--------------------------------------
Expand Down
18 changes: 10 additions & 8 deletions tests/gtest/tests/clienttest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,40 +426,42 @@ TEST_F(ClientTest, networkReceivedSendsPublishEmitsReceivedSignal_Test)
EXPECT_EQ(1, spy.count());
}

// todo: should happen on suback
TEST_F(ClientTest, subscribeEmitsSubscribedSignal_Test)
{
EXPECT_CALL(*_networkMock, sendFrame(_));
QSignalSpy spy(_client.data(), &QMQTT::Client::subscribed);

_client->subscribe("topic", QOS2);
quint16 msgid = _client->subscribe("topic", QOS2);

QByteArray payLoad;
payLoad.append((char)0x12); // message ID
payLoad.append((char)0x23); // message ID
payLoad.append((char)(msgid >> 8)); // message ID MSB
payLoad.append((char)(msgid && 0xFF)); // message ID LSB
payLoad.append((char)QOS2); // QOS
QMQTT::Frame frame(SUBACK_TYPE, payLoad);
emit _networkMock->received(frame);

ASSERT_EQ(1, spy.count());
EXPECT_EQ("topic", spy.at(0).at(0).toString());
EXPECT_EQ(QOS2, spy.at(0).at(1).toInt());
}

// todo: network received sends suback triggers a subscribed signal (other things?)

// todo: should happen on unsuback
TEST_F(ClientTest, unsubscribeEmitsUnsubscribedSignal_Test)
{
EXPECT_CALL(*_networkMock, sendFrame(_));
QSignalSpy spy(_client.data(), &QMQTT::Client::unsubscribed);

_client->unsubscribe("topic");

QMQTT::Frame frame(UNSUBACK_TYPE, QByteArray());
QByteArray payLoad;
payLoad.append((char)0x00); // message ID MSB
payLoad.append((char)0x01); // message ID LSB
QMQTT::Frame frame(UNSUBACK_TYPE, payLoad);
emit _networkMock->received(frame);

EXPECT_EQ(1, spy.count());
// EXPECT_EQ("topic", spy.at(0).at(0).toString());
ASSERT_EQ(1, spy.count());
EXPECT_EQ("topic", spy.at(0).at(0).toString());
}

// todo: network received sends unsuback then emit unsubscribed signal (only then?)
Expand Down

0 comments on commit 0279f94

Please sign in to comment.