From bd9b2baa29639e0e29a670be21ad26c6b7118970 Mon Sep 17 00:00:00 2001 From: Will Sobel Date: Thu, 13 Jun 2024 16:29:28 -0400 Subject: [PATCH 1/3] fixed two issues with disconnect and termiinal '/' interpreted as topic --- src/mtconnect/configuration/agent_config.cpp | 2 +- src/mtconnect/mqtt/mqtt_client_impl.hpp | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/mtconnect/configuration/agent_config.cpp b/src/mtconnect/configuration/agent_config.cpp index 80174ad8..a2fd59da 100644 --- a/src/mtconnect/configuration/agent_config.cpp +++ b/src/mtconnect/configuration/agent_config.cpp @@ -909,7 +909,7 @@ namespace mtconnect::configuration { string host, protocol, path; auto url = *GetOption(options, configuration::Url); - boost::regex pat("^([^:]+)://([^:/]+)(:[0-9]+)?(/?.+)"); + boost::regex pat("^([^:]+)://([^:/]+)(:[0-9]+)?(/.+)?"); boost::match_results match; if (boost::regex_match(url, match, pat)) { diff --git a/src/mtconnect/mqtt/mqtt_client_impl.hpp b/src/mtconnect/mqtt/mqtt_client_impl.hpp index 38ae0f5e..f1520dd2 100644 --- a/src/mtconnect/mqtt/mqtt_client_impl.hpp +++ b/src/mtconnect/mqtt/mqtt_client_impl.hpp @@ -87,7 +87,7 @@ namespace mtconnect { m_password = GetOption(options, configuration::MqttPassword); std::stringstream url; - url << "mqtt://" << m_host << ':' << m_port; + url << "mqtt://" << m_host << ':' << m_port << '/'; m_url = url.str(); // Some brokers require specific ClientID provided. @@ -162,9 +162,6 @@ namespace mtconnect { LOG(info) << "MQTT " << m_url << ": connection closed"; // Queue on a strand m_connected = false; - if (m_handler && m_handler->m_disconnected) - m_handler->m_disconnected(shared_from_this()); - m_handler->m_disconnected(shared_from_this()); if (m_running) { reconnect(); @@ -365,11 +362,16 @@ namespace mtconnect { void reconnect() { NAMED_SCOPE("MqttClientImpl::reconnect"); - + if (!m_running) return; - LOG(info) << "Start reconnect timer"; + LOG(info) << "Calling handler disconnected"; + + if (m_handler && m_handler->m_disconnected) + m_handler->m_disconnected(shared_from_this()); + + LOG(info) << "Start reconnect timer"; // Set an expiry time relative to now. m_reconnectTimer.expires_after(m_connectInterval); From 1e3ac1b9abeb6f2bf950bd3b5ec6ac79e5e6ef6a Mon Sep 17 00:00:00 2001 From: Will Sobel Date: Fri, 14 Jun 2024 10:03:57 -0400 Subject: [PATCH 2/3] Added disconnected method since reconnect is called multiple times --- src/mtconnect/configuration/service.cpp | 2 +- src/mtconnect/mqtt/mqtt_client_impl.hpp | 28 ++++++++++++++++--------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/mtconnect/configuration/service.cpp b/src/mtconnect/configuration/service.cpp index 557c6d9e..5840952c 100644 --- a/src/mtconnect/configuration/service.cpp +++ b/src/mtconnect/configuration/service.cpp @@ -575,7 +575,7 @@ namespace mtconnect { boost::optional command; boost::optional 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); diff --git a/src/mtconnect/mqtt/mqtt_client_impl.hpp b/src/mtconnect/mqtt/mqtt_client_impl.hpp index f1520dd2..510e8ece 100644 --- a/src/mtconnect/mqtt/mqtt_client_impl.hpp +++ b/src/mtconnect/mqtt/mqtt_client_impl.hpp @@ -153,7 +153,7 @@ namespace mtconnect { else { LOG(info) << "MQTT ConnAck: MQTT connection failed: " << ec; - reconnect(); + disconnected(); } return true; }); @@ -164,7 +164,7 @@ namespace mtconnect { m_connected = false; if (m_running) { - reconnect(); + disconnected(); return true; } else @@ -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 packet_id, @@ -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(); + } + /// /// /// 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); From 34093f8861bbf721aef26e3159332291f975e65a Mon Sep 17 00:00:00 2001 From: Will Sobel Date: Fri, 14 Jun 2024 10:07:51 -0400 Subject: [PATCH 3/3] Version 2.3.0.11 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 771bb955..c4f15273 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ set(AGENT_VERSION_MAJOR 2) set(AGENT_VERSION_MINOR 3) set(AGENT_VERSION_PATCH 0) -set(AGENT_VERSION_BUILD 10) +set(AGENT_VERSION_BUILD 11) set(AGENT_VERSION_RC "") # This minimum version is to support Visual Studio 2019 and C++ feature checking and FetchContent