twoclienttestcontext.cpp
3.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
#include "twoclienttestcontext.h"
#include <QEventLoop>
#include <QTimer>
// TODO: port to QMqttClient that newer Qts now have?
TwoClientTestContext::TwoClientTestContext(QObject *parent) : QObject(parent)
{
QHostInfo targetHostInfo = QHostInfo::fromName("localhost");
QHostAddress targetHost(targetHostInfo.addresses().first());
sender.reset(new QMQTT::Client(targetHost));
receiver.reset(new QMQTT::Client(targetHost));
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, bool retain)
{
QMQTT::Message msg;
msg.setTopic(topic);
msg.setRetain(retain);
msg.setQos(0);
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)
{
receiver->subscribe(topic);
}
void TwoClientTestContext::waitReceiverReceived()
{
QEventLoop waiter;
QTimer timeout;
timeout.setSingleShot(true);
timeout.setInterval(1000);
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 new std::runtime_error(msg.toStdString());
}
void TwoClientTestContext::onReceiverReceived(const QMQTT::Message &message)
{
receivedMessages.append(message);
}