This repository has been archived by the owner on May 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mosquittotransceiver.cpp
210 lines (163 loc) · 5.66 KB
/
mosquittotransceiver.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
/*
* Copyright (c) 2018 Poliba Corse
* Authors: Giovanni Grieco <[email protected]>
*
* Part of this code have been inspired by
* - Alessandro Pezzato (https://github.com/alepez/MosQtitto)
*/
#include "mosquittotransceiver.h"
#include <QByteArray>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include <mosquitto.h>
namespace {
void on_connect_wrapper(struct mosquitto *, void *that, int rc)
{
MosquittoTransceiver *m = reinterpret_cast<MosquittoTransceiver *>(that);
m->_onConnect(rc);
}
void on_disconnect_wrapper(struct mosquitto *, void *that, int rc)
{
MosquittoTransceiver *m = reinterpret_cast<MosquittoTransceiver *>(that);
m->_onDisconnect(rc);
}
void on_message_wrapper(struct mosquitto *, void *that, const struct mosquitto_message *message)
{
MosquittoTransceiver *m = reinterpret_cast<MosquittoTransceiver *>(that);
m->_onMessage(message);
}
} /* namespace _ */
MosquittoTransceiver::MosquittoTransceiver(QObject *parent) : QThread(parent), isConnected_{false}
{
mosquitto_lib_init();
auto onConnected = [this]() {
isConnected_ = true;
emit connected();
emit isConnectedChanged();
};
auto onDisconnected = [this]() {
isConnected_ = false;
emit disconnected();
emit isConnectedChanged();
};
auto onMessage = [this](QString topic, QJsonObject payload) {
/* emit with queued connection */
emit message(topic, payload);
};
/* QueuedConnection is needed, so callbacks are handled outside mosquitto thread */
QObject::connect(this, &MosquittoTransceiver::_connected, this, onConnected, Qt::QueuedConnection);
QObject::connect(this, &MosquittoTransceiver::_disconnected, this, onDisconnected, Qt::QueuedConnection);
QObject::connect(this, &MosquittoTransceiver::_message, this, onMessage, Qt::QueuedConnection);
qDebug() << "MosquittoTransceiver initialized.";
startTime_.start();
}
MosquittoTransceiver::~MosquittoTransceiver()
{
/* Force disconnection and stop the thread */
this->disconnect();
mosquitto_lib_cleanup();
}
void MosquittoTransceiver::connect()
{
this->QThread::start();
}
void MosquittoTransceiver::disconnect()
{
continueRunning_ = false;
if (!this->wait(2000)) {
qWarning() << "Timeout while waiting for mosquitto disconnection.";
}
}
void MosquittoTransceiver::run()
{
int rc = 0;
auto mosq = mosquitto_new("DataConverter", true, this);
if (!mosq) {
std::runtime_error{"MosquittoTransceiver: cannot instantiate mosquitto client."};
}
mosquitto_connect_callback_set(mosq, on_connect_wrapper);
mosquitto_disconnect_callback_set(mosq, on_disconnect_wrapper);
mosquitto_message_callback_set(mosq, on_message_wrapper);
continueRunning_ = true;
rc = mosquitto_connect(mosq, "localhost", 1883, 60);
if (rc != MOSQ_ERR_SUCCESS) {
qCritical() << "MosquittoReceiver: cannot connect!";
mosquitto_destroy(mosq);
return;
}
qDebug() << "MosquittoReceiver: connected. Start looping...";
while (continueRunning_) {
rc = mosquitto_loop(mosq, 500, 1); // we need high performance and quasi-realtime data elaboration
if (continueRunning_ && rc) {
qWarning() << "Mosquitto connection error. Thread will sleep.";
QThread::sleep(1);
mosquitto_reconnect(mosq);
}
mutex_.lock();
while (!subscribeQueue_.isEmpty()) {
const auto &topic = subscribeQueue_.dequeue();
mosquitto_subscribe(mosq, nullptr, topic.toStdString().c_str(), 0); // QoS 0: just stream it
}
while (!publishQueue_.isEmpty()) {
const auto& subscription = publishQueue_.front();
auto topic = std::get<0>(subscription);
auto payload = std::get<1>(subscription);
mosquitto_publish(mosq, nullptr, topic.toStdString().c_str(), payload.size(), payload.constData(), 0, false);
publishQueue_.pop_front();
}
mutex_.unlock();
}
mosquitto_disconnect(mosq);
mosquitto_destroy(mosq);
}
void MosquittoTransceiver::publish(QString topic, QJsonObject jpayload)
{
qDebug() << "Publishing to " << topic;
/* Debug - Time elapsed */
mutex_startTime_.lock();
qDebug() << "Elapsed time from last arrive: " << startTime_.elapsed() << "ms";
mutex_startTime_.unlock();
auto payload = QJsonDocument(jpayload).toJson(QJsonDocument::Compact);
mutex_.lock();
publishQueue_.push_back(std::make_tuple(topic, payload));
mutex_.unlock();
}
void MosquittoTransceiver::subscribe(QString topic)
{
qDebug() << "Subscribing to " << topic;
mutex_.lock();
subscribeQueue_.enqueue(topic);
mutex_.unlock();
}
void MosquittoTransceiver::_onConnect(int rc)
{
if (rc != MOSQ_ERR_SUCCESS) {
qWarning() << "Mosquitto connection failed!";
return;
}
emit _connected();
}
void MosquittoTransceiver::_onDisconnect(int rc)
{
if (rc != MOSQ_ERR_SUCCESS) {
qWarning() << "Mosquitto disconnected unexpectedly!";
}
emit _disconnected();
}
void MosquittoTransceiver::_onMessage(const mosquitto_message *message)
{
QString topic{message->topic};
QByteArray payload{reinterpret_cast<const char *>(message->payload), message->payloadlen};
try {
auto jdoc = QJsonDocument::fromJson(payload);
auto jpayload = jdoc.object();
mutex_startTime_.lock();
startTime_.restart();
mutex_startTime_.unlock();
qDebug() << "Message arrived from " << topic;
emit _message(topic, jpayload);
} catch (...) {
qWarning("Something happened during json message parsing.");
}
}