Skip to content

Commit

Permalink
changes to MQTT plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed Nov 10, 2024
1 parent 78fb7a1 commit 2a0b85b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
18 changes: 4 additions & 14 deletions plotjuggler_plugins/DataStreamMQTT/mqtt_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,13 @@ bool MQTTClient::configureMosquitto(const MosquittoConfig& config)
{
// Threaded mode may not be supported on windows (libmosquitto < 2.1).
// See https://github.com/eclipse/mosquitto/issues/2707
Q_ASSERT(_thread == nullptr);
_thread = new std::thread([this]() {
_thread = std::thread([this]() {
int rc = mosquitto_loop_forever(this->_mosq, -1, 1);
if (rc != MOSQ_ERR_SUCCESS)
{
debug() << "MQTT loop forever failed:" << mosquitto_strerror(rc);
}
});
if (_thread == nullptr)
{
QMessageBox::warning(nullptr, "MQTT Client", QString("Failed to start MQTT client"),
QMessageBox::Ok);
debug() << "MQTT start failed: could not allocate memory.";
return false;
}
}
else if (rc != MOSQ_ERR_SUCCESS)
{
Expand All @@ -247,11 +239,9 @@ void MQTTClient::disconnect()
{
mosquitto_disconnect(_mosq);
mosquitto_loop_stop(_mosq, true);
if (_thread != nullptr)
if (_thread.joinable())
{
_thread->join();
delete _thread;
_thread = nullptr;
_thread.join();
}
mosquitto_destroy(_mosq);
_mosq = nullptr;
Expand Down Expand Up @@ -311,4 +301,4 @@ void MQTTClient::unsubscribe(const std::string& topic)
{
mosquitto_unsubscribe(_mosq, nullptr, topic.c_str());
}
}
}
2 changes: 1 addition & 1 deletion plotjuggler_plugins/DataStreamMQTT/mqtt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class MQTTClient : public QObject
std::unordered_set<std::string> _topics_set;
std::mutex _mutex;
MosquittoConfig _config;
std::thread* _thread;
std::thread _thread;
};

#endif // MQTT_CLIENT_H

0 comments on commit 2a0b85b

Please sign in to comment.