Skip to content
Snippets Groups Projects
Unverified Commit d9a0fe39 authored by Tim Heß's avatar Tim Heß :roller_coaster:
Browse files

handle unconnected client cleanup, empty root config & use json::parse

parent b47dcfda
Branches
No related tags found
1 merge request!3handle unconnected client cleanup, empty root config & use json::parse
......@@ -27,8 +27,7 @@ MQTT::Configuration::Configuration()
MQTT::Configuration::Configuration(std::string filename)
{
std::ifstream infile(filename);
json j;
infile >> j;
json j = json::parse(infile);
host = j.value("host", "127.0.0.1");
port = j.value("port", 1883);
username = j.value("username", "guest");
......
......@@ -81,8 +81,14 @@ MQTT::Publisher::Publisher(std::string id, unsigned int buffer) : id(id), buffer
MQTT::Publisher::~Publisher()
{
this->disconnect();
worker->detach();
if (this->is_connected())
{
this->disconnect();
}
if (worker != nullptr)
{
worker->detach();
}
instances--;
if (instances == 0)
{
......@@ -169,11 +175,14 @@ void MQTT::Publisher::disconnect(unsigned int timeout)
void MQTT::Publisher::set_root_topic(std::string root_topic)
{
configuration.root = root_topic;
std::string::iterator last_character = configuration.root.end() - 1;
if ((*last_character) != '/')
if (configuration.root.length() > 0)
{
configuration.root += "/";
}
std::string::iterator last_character = configuration.root.end() - 1;
if ((*last_character) != '/')
{
configuration.root += "/";
}
}
}
void MQTT::Publisher::set_buffer(unsigned int buffer)
......@@ -183,6 +192,10 @@ void MQTT::Publisher::set_buffer(unsigned int buffer)
bool MQTT::Publisher::is_connected(void)
{
if (this->client == nullptr)
{
return false;
}
try
{
return client->is_connected();
......@@ -196,10 +209,13 @@ bool MQTT::Publisher::is_connected(void)
void MQTT::Publisher::configure(MQTT::Configuration configuration)
{
this->configuration = configuration;
std::string::iterator last_character = this->configuration.root.end() - 1;
if ((*last_character) != '/')
if (configuration.root.length() > 0)
{
this->configuration.root += "/";
std::string::iterator last_character = configuration.root.end() - 1;
if ((*last_character) != '/')
{
configuration.root += "/";
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment