-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmqttmanager.cpp
273 lines (243 loc) · 8.6 KB
/
mqttmanager.cpp
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "mqttmanager.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QTimer>
#include <QMqttClient>
#include <QSslConfiguration>
#include <QUuid>
#include <QSettings>
#include "updater.h"
#include "globals.h"
namespace Yate {
MqttManager::MqttManager(QObject *parent, QString key)
: QObject{parent}, running_(false), messageBufferTimer_(new QTimer(this)), settings_(new QSettings), clientState_(QMqttClient::Disconnected)
{
connect(messageBufferTimer_, &QTimer::timeout, this, &MqttManager::checkMessageBuffers);
auto keyparts = key.trimmed().split(":");
if (keyparts.size() != 2) {
qCritical() << "MQTT Manager: received invalid key.";
initialized_ = false;
return;
}
bool isSecure_ = QSslSocket::supportsSsl() && !QSslConfiguration::defaultConfiguration().isNull();
if (!isSecure_) {
qCritical() << "MQTT Manager: Secure connection is required.";
initialized_ = false;
return;
}
client_ = new QMqttClient(this);
client_->setHostname(MQTT_HOST);
client_->setPort(MQTT_PORT_SECURE);
client_->setProtocolVersion(QMqttClient::MQTT_3_1_1);
client_->setKeepAlive(MQTT_KEEPALIVE);
username_ = keyparts[0];
password_ = keyparts[1];
userId_ = YATE_2_0_PREFIX + QString(QUuid::createUuid().toRfc4122().toHex()).mid(12);
userPassword_ = QString(QUuid::createUuid().toRfc4122().toHex()).mid(0, 20);
settings_->setValue(SETTINGS_KEY_MQTT_USER_ID, userId_);
settings_->setValue(SETTINGS_KEY_MQTT_USER_PASS, userPassword_);
client_->setUsername(username_);
client_->setPassword(password_);
qDebug() << "Connecting" << MQTT_HOST << " " << MQTT_PORT_SECURE << " secure?" << isSecure_;
qDebug() << "User ID" << userId_;
qDebug() << "User Password" << userPassword_;
connect(client_, &QMqttClient::stateChanged, this, &MqttManager::updateLogStateChange);
connect(client_, &QMqttClient::disconnected, this, &MqttManager::brokerDisconnected);
connect(client_, &QMqttClient::messageReceived, this, &MqttManager::messageReceived);
// connect(client_, &QMqttClient::pingResponseReceived, this, []() {
// qDebug() << "ping";
// });
client_->connectToHostEncrypted(QSslConfiguration::defaultConfiguration());
initialized_ = true;
}
bool MqttManager::connectTo(QString lobby, QString &errMsg)
{
auto lobbyParts = lobby.trimmed().split(":");
if (lobbyParts.size() != 2 || !lobby.startsWith(YATE_2_0_PREFIX)) {
errMsg = "Invalid lobby ID";
emit connectionFailed(errMsg);
return false;
}
lobby = lobbyParts[0] + "/" + lobbyParts[1];
if (client_->state() == QMqttClient::Connecting) {
errMsg = "Still connecting to the server, try again in few seconds.";
emit connectionFailed(errMsg);
return false;
} else if (client_->state() == QMqttClient::Disconnected) {
errMsg = "Failed to connect to the server";
emit connectionFailed(errMsg);
return false;
} else {
if (peerTopic_.isValid()) {
client_->unsubscribe(QMqttTopicFilter(peerTopic_.name()));
}
peerTopic_.setName(lobby);
auto subscription = client_->subscribe(QMqttTopicFilter(peerTopic_.name()));
if (!subscription) {
errMsg = "Failed to connect to the lobby";
emit connectionFailed(errMsg);
return false;
}
}
return true;
}
QString MqttManager::topic() {
return userId_ + "/" + userPassword_;
}
QString MqttManager::topicUrl() {
return userId_ + ":" + userPassword_;
}
void MqttManager::start()
{
messageBufferTimer_->start(MQTT_MESSAGE_BUFFER_TIMER);
running_ = true;
}
void MqttManager::sendMessageOnChannel1(QString msg)
{
sendMessageOnChannel(msg, 1);
}
void MqttManager::sendMessageOnChannel2(QString msg)
{
sendMessageOnChannel(msg, 2);
}
void MqttManager::sendMessageOnChannel3(QString msg)
{
sendMessageOnChannel(msg, 3);
}
void MqttManager::disconnectFromLobby()
{
if (peerTopic_.isValid()) {
client_->unsubscribe(QMqttTopicFilter(peerTopic_.name()));
peerTopic_.setName("");
}
}
void MqttManager::reconnect() {
client_->connectToHostEncrypted(QSslConfiguration::defaultConfiguration());
}
void MqttManager::messageReceived(const QByteArray &message, const QMqttTopicName &)
{
if (peerTopic_.isValid()) {
qDebug() << "MQTT Manager: Message accepted";
QJsonDocument jsonMsg = QJsonDocument::fromJson(message);
QJsonObject obj = jsonMsg.object();
if (!obj.contains("from") || obj.value("from").toString() != peerTopic_.name()) {
qWarning() << "Received message from invalid topic "
<< (obj.contains("from")? obj.value("from").toString(): "<no from>")
<< ", expected " << peerTopic_.name();
return;
}
if (obj.contains("message") && obj.contains("type") && obj.value("type").toString() == "default") {
QString message = obj.value("message").toString();
if (obj.contains("channel")) {
QString ch = obj.value("channel").toString();
if (ch == "1") {
qDebug() << "MQTT Manager: Message received on channel 1";
emit onMessageFromChannel1(message);
} else if (ch == "2") {
qDebug() << "MQTT Manager: Message received on channel 2";
emit onMessageFromChannel2(message);
} else if (ch == "3") {
qDebug() << "MQTT Manager: Message received on channel 3";
emit onMessageFromChannel3(message);
}
}
}
} else {
qDebug () << "MQTT Manager: No topic";
}
}
void MqttManager::checkMessageBuffers()
{
QMutexLocker lock(&bufferMutex_);
if (ch1Buffer_.size()) {
QString payload = ch1Buffer_;
ch1Buffer_ = "";
publishMessage(payload);
}
if (ch2Buffer_.size()) {
QString payload = ch2Buffer_;
ch2Buffer_ = "";
publishMessage(payload);
}
if (ch3Buffer_.size()) {
QString payload = ch3Buffer_;
ch3Buffer_ = "";
publishMessage(payload);
}
}
void MqttManager::updateLogStateChange() {
clientState_ = client_->state();
qDebug() << "MQTT Manager: state changed " << clientState_ << "(" << stateToString_(clientState_) << ")";
switch(clientState_) {
case QMqttClient::Connected:
qDebug () << "MQTT Manager: Connected!";
emit connected();
emit onLobbyIdChange(topicUrl());
break;
case QMqttClient::Connecting:
qDebug () << "MQTT Manager: Connecting!";
emit connecting();
break;
case QMqttClient::Disconnected:
qDebug () << "MQTT Manager: Disconnected!";
emit disconnected();
break;
}
}
void MqttManager::brokerDisconnected()
{
qDebug() << "MQTT Manager: disconnected";
}
void MqttManager::publishMessage(QString msg)
{
if (clientState_ == QMqttClient::Connected) {
qDebug() << "MQTT Manager: publishing to" << topic();
auto publishResult = client_->publish(topic(), msg.toUtf8());
qDebug() << "MQTT Manager: publish result:" << publishResult;
} else {
qWarning() << "MQTT Manager: not connected.";
}
}
void MqttManager::sendMessageOnChannel(QString msg, int ch)
{
qDebug() << "MQTT Manager: sendMessageOnChannel" << ch;
if(clientState_ == QMqttClient::Connected) {
QJsonObject json;
json.insert("message", msg);
json.insert("version", "2.0");
json.insert("protocol", "mqtt");
json.insert("type", "default");
json.insert("from", topic());
json.insert("yate_ver", Updater::getInstance(0)->getVersion());
json.insert("channel", QString::number(ch));
QString msgJson(QJsonDocument(json).toJson(QJsonDocument::Compact));
QMutexLocker lock(&bufferMutex_);
if (ch == 1) {
ch1Buffer_ = msgJson;
} else if (ch == 2) {
ch2Buffer_ = msgJson;
} else if (ch == 3) {
ch3Buffer_ = msgJson;
} else {
qWarning() << "MQTT Manager: channel" << ch << " is not supported";
}
} else {
qWarning() << "MQTT Manager: not connected.";
}
}
const QString &MqttManager::getUserId() const
{
return userId_;
}
QString MqttManager::stateToString_(int state)
{
if (state == QMqttClient::Disconnected) {
return "Disconnected";
} else if (state == QMqttClient::Connected) {
return "Connected";
} else if (state == QMqttClient::Connecting) {
return "Connecting";
}
return "";
}
}