Skip to content

Commit

Permalink
Added disconnected method since reconnect is called multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
wsobel committed Jun 14, 2024
1 parent bd9b2ba commit 1e3ac1b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/mtconnect/configuration/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ namespace mtconnect {
boost::optional<std::string> command;
boost::optional<std::string> config;

const char *argp[3] = {"agent", "run", (const char *) configFile};
const char *argp[3] = {"agent", "run", (const char *)configFile};
auto options = g_service->parseOptions(3, argp, command, config);
g_service->initialize(options);

Expand Down
28 changes: 18 additions & 10 deletions src/mtconnect/mqtt/mqtt_client_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ namespace mtconnect {
else
{
LOG(info) << "MQTT ConnAck: MQTT connection failed: " << ec;
reconnect();
disconnected();
}
return true;
});
Expand All @@ -164,7 +164,7 @@ namespace mtconnect {
m_connected = false;
if (m_running)
{
reconnect();
disconnected();
return true;
}
else
Expand All @@ -177,7 +177,7 @@ namespace mtconnect {
LOG(error) << "error: " << ec.message();
m_connected = false;
if (m_running)
reconnect();
disconnected();
});

client->set_publish_handler([this](mqtt::optional<std::uint16_t> packet_id,
Expand Down Expand Up @@ -356,22 +356,30 @@ namespace mtconnect {
m_handler->m_receive(shared_from_this(), string(topic), string(contents));
}

void disconnected()
{
NAMED_SCOPE("MqttClientImpl::disconnected");

LOG(info) << "Calling handler disconnected";

if (m_handler && m_handler->m_disconnected)
m_handler->m_disconnected(shared_from_this());

if (m_running)
reconnect();
}

/// <summary>
///
/// </summary>
void reconnect()
{
NAMED_SCOPE("MqttClientImpl::reconnect");

if (!m_running)
return;

LOG(info) << "Calling handler disconnected";

if (m_handler && m_handler->m_disconnected)
m_handler->m_disconnected(shared_from_this());

LOG(info) << "Start reconnect timer";
LOG(info) << "Start reconnect timer";

// Set an expiry time relative to now.
m_reconnectTimer.expires_after(m_connectInterval);
Expand Down

0 comments on commit 1e3ac1b

Please sign in to comment.