twoclienttestcontext.cpp
5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, version 3.
FlashMQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with FlashMQ. If not, see <https://www.gnu.org/licenses/>.
*/
#include "twoclienttestcontext.h"
#include <QEventLoop>
#include <QTimer>
// TODO: port to QMqttClient that newer Qts now have?
TwoClientTestContext::TwoClientTestContext(int clientNr, QObject *parent) : QObject(parent)
{
QHostInfo targetHostInfo = QHostInfo::fromName("localhost");
QHostAddress targetHost(targetHostInfo.addresses().first());
sender.reset(new QMQTT::Client(targetHost, 21883));
sender->setClientId(QString("Sender%1").arg(clientNr));
receiver.reset(new QMQTT::Client(targetHost, 21883));
receiver->setClientId(QString("Receiver%1").arg(clientNr));
connect(sender.data(), &QMQTT::Client::error, this, &TwoClientTestContext::onClientError);
connect(receiver.data(), &QMQTT::Client::error, this, &TwoClientTestContext::onClientError);
}
void TwoClientTestContext::publish(const QString &topic, const QByteArray &payload)
{
publish(topic, payload, 0, false);
}
void TwoClientTestContext::publish(const QString &topic, const QByteArray &payload, bool retain)
{
publish(topic, payload, 0, retain);
}
void TwoClientTestContext::publish(const QString &topic, const QByteArray &payload, const quint8 qos, bool retain)
{
QMQTT::Message msg;
msg.setTopic(topic);
msg.setRetain(retain);
msg.setQos(qos);
msg.setPayload(payload);
sender->publish(msg);
}
void TwoClientTestContext::connectSender()
{
sender->connectToHost();
QEventLoop waiter;
connect(sender.data(), &QMQTT::Client::connected, &waiter, &QEventLoop::quit);
waiter.exec();
}
void TwoClientTestContext::connectReceiver()
{
connect(receiver.data(), &QMQTT::Client::received, this, &TwoClientTestContext::onReceiverReceived);
receiver->connectToHost();
QEventLoop waiter;
connect(receiver.data(), &QMQTT::Client::connected, &waiter, &QEventLoop::quit);
waiter.exec();
}
void TwoClientTestContext::disconnectReceiver()
{
receiver->disconnectFromHost();
QEventLoop waiter;
connect(sender.data(), &QMQTT::Client::disconnected, &waiter, &QEventLoop::quit);
waiter.exec();
}
void TwoClientTestContext::subscribeReceiver(const QString &topic, const quint8 qos)
{
receiver->subscribe(topic, qos);
QEventLoop waiter;
QTimer timeout;
timeout.setSingleShot(true);
timeout.setInterval(1000);
connect(&timeout, &QTimer::timeout, &waiter, &QEventLoop::quit);
connect(receiver.data(), &QMQTT::Client::subscribed, &waiter, &QEventLoop::quit);
timeout.start();
waiter.exec();
}
void TwoClientTestContext::unsubscribeReceiver(const QString &topic)
{
receiver->unsubscribe(topic);
QEventLoop waiter;
QTimer timeout;
timeout.setSingleShot(true);
timeout.setInterval(1000);
connect(&timeout, &QTimer::timeout, &waiter, &QEventLoop::quit);
connect(receiver.data(), &QMQTT::Client::unsubscribed, &waiter, &QEventLoop::quit);
timeout.start();
waiter.exec();
}
void TwoClientTestContext::waitReceiverReceived(const int count)
{
if (count > 0 && receivedMessages.count() == count)
return;
int attempt = 0;
while(receivedMessages.count() != count && attempt++ < count)
{
QEventLoop waiter;
QTimer timeout;
timeout.setSingleShot(true);
timeout.setInterval(3000);
connect(&timeout, &QTimer::timeout, &waiter, &QEventLoop::quit);
connect(receiver.data(), &QMQTT::Client::received, &waiter, &QEventLoop::quit);
timeout.start();
waiter.exec();
}
}
void TwoClientTestContext::onClientError(const QMQTT::ClientError error)
{
const QMQTT::Client *_sender = sender.data();
// TODO: arg, doesn't qmqtt have a better way for this?
QString errStr = QString("unknown error");
if (error == QMQTT::SocketConnectionRefusedError)
errStr = "Connection refused";
if (error == QMQTT::SocketRemoteHostClosedError)
errStr = "Remote host closed";
if (error == QMQTT::SocketHostNotFoundError)
errStr = "Remote host not found";
if (error == QMQTT::MqttBadUserNameOrPasswordError)
errStr = "MQTT bad user or password";
if (error == QMQTT::MqttNotAuthorizedError)
errStr = "MQTT not authorized";
if (error == QMQTT::SocketResourceError)
errStr = "Socket resource error. Is your OS limiting you? Ulimit, etc?";
if (error == QMQTT::SocketSslInternalError)
errStr = "Socket SSL internal error.";
if (error == QMQTT::SocketTimeoutError)
errStr = "Socket timeout";
QString msg = QString("Client %1 error code: %2 (%3). Initiated delayed reconnect.\n").arg(_sender->clientId()).arg(error).arg(errStr);
throw std::runtime_error(msg.toStdString());
}
void TwoClientTestContext::onReceiverReceived(const QMQTT::Message &message)
{
receivedMessages.append(message);
}