From 76d013731490e4332f1552001a1db810be0a97e2 Mon Sep 17 00:00:00 2001 From: Peter M. Groen Date: Mon, 4 Jul 2022 01:46:30 +0200 Subject: [PATCH] Fix on connection --- debug_log.txt | 39 +++++++++++++++++++++++++++++++++++++++ src/clientpaho.cpp | 49 ++++++++++++++++++++++++++++++------------------- 2 files changed, 69 insertions(+), 19 deletions(-) create mode 100644 debug_log.txt diff --git a/debug_log.txt b/debug_log.txt new file mode 100644 index 0000000..9c6b103 --- /dev/null +++ b/debug_log.txt @@ -0,0 +1,39 @@ +With broker present + +Jul 02 00:50:19 intelnuc64.osdev.nl test_connection[30797]: MQTT Client started[30797]: [MqttClient::MqttClient] +Jul 02 00:50:19 intelnuc64.osdev.nl [MqttClient::eventHandler][30797]: ConnectionTest - starting event handler. +Jul 02 00:50:19 intelnuc64.osdev.nl MqttClient[30797]: ConnectionTest - Request connect +Jul 02 00:50:19 intelnuc64.osdev.nl [ClientPaho][30797]: ConnectionTest_1_83746b87-431f-4ce1-82e3-2543e9b0a37a - Setting the extra onConnected callback. +Jul 02 00:50:19 intelnuc64.osdev.nl [MqttClient::connectionStatusChanged][30797]: ConnectionTest - connection status of wrapped client ConnectionTest_1_83746b87-431f-4ce1-82e3-2543e9b0a37a changed to 2 +Jul 02 00:50:19 intelnuc64.osdev.nl ClientPaho[30797]: ConnectionTest_1_83746b87-431f-4ce1-82e3-2543e9b0a37a - starting callback event handler +Jul 02 00:50:19 intelnuc64.osdev.nl [ClientPaho::onConnectSuccess][30797]: onConnectSucces triggered.. +Jul 02 00:50:19 intelnuc64.osdev.nl [ClientPaho][30797]: onConnectSuccessOnInstance ConnectionTest_1_83746b87-431f-4ce1-82e3-2543e9b0a37a - connected to endpoint localhost:1883 (mqtt version 4, session present FALSE ) +Jul 02 00:50:19 intelnuc64.osdev.nl [MqttClient::connectionStatusChanged][30797]: ConnectionTest - connection status of wrapped client ConnectionTest_1_83746b87-431f-4ce1-82e3-2543e9b0a37a changed to 4 +Jul 02 00:50:30 intelnuc64.osdev.nl mosquitto[29463]: 1656715830: Client ConnectionTest_1_83746b87-431f-4ce1-82e3-2543e9b0a37a closed its connection. + + +Without a broker present + +Jul 02 00:55:33 intelnuc64.osdev.nl test_connection[31574]: MQTT Client started[31574]: [MqttClient::MqttClient] +Jul 02 00:55:33 intelnuc64.osdev.nl [MqttClient::eventHandler][31574]: ConnectionTest - starting event handler. +Jul 02 00:55:33 intelnuc64.osdev.nl MqttClient[31574]: ConnectionTest - Request connect +Jul 02 00:55:33 intelnuc64.osdev.nl [ClientPaho][31574]: ConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 - Setting the extra onConnected callback. +Jul 02 00:55:33 intelnuc64.osdev.nl [MqttClient::connectionStatusChanged][31574]: ConnectionTest - connection status of wrapped client ConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 changed to 2 +Jul 02 00:55:33 intelnuc64.osdev.nl ClientPaho[31574]: ConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 - starting callback event handler +Jul 02 00:55:33 intelnuc64.osdev.nl ClientPaho[31574]: onConnectFailureOnInstanceConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 - connection failed with code MQTTASYNC_FAILURE (TCP/TLS connect failure) +Jul 02 00:55:33 intelnuc64.osdev.nl [MqttClient::connectionStatusChanged][31574]: ConnectionTest - connection status of wrapped client ConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 changed to 0 + +Jul 02 00:55:42 intelnuc64.osdev.nl systemd[1]: Starting Mosquitto MQTT Broker... +Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: mosquitto version 2.0.14 starting +Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: Config loaded from /etc/mosquitto/mosquitto.conf. +Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: Starting in local only mode. Connections will only be possible from clients running on this machine. +Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: Create a configuration file which defines a listener to allow remote access. +Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: For more details see https://mosquitto.org/documentation/authentication-methods/ +Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: Opening ipv4 listen socket on port 1883. +Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: Opening ipv6 listen socket on port 1883. +Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: mosquitto version 2.0.14 running +Jul 02 00:55:45 intelnuc64.osdev.nl mosquitto[31610]: 1656716145: New connection from 127.0.0.1:42196 on port 1883. +Jul 02 00:55:45 intelnuc64.osdev.nl mosquitto[31610]: 1656716145: New client connected from 127.0.0.1:42196 as ConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 (p2, c1, k5). +Jul 02 00:55:53 intelnuc64.osdev.nl mosquitto[31610]: 1656716153: Client ConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 closed its connection. + + diff --git a/src/clientpaho.cpp b/src/clientpaho.cpp index 622e44e..155a32c 100644 --- a/src/clientpaho.cpp +++ b/src/clientpaho.cpp @@ -123,37 +123,29 @@ ClientPaho::ClientPaho(const std::string& _endpoint, , m_callbackEventQueue(m_clientId) , m_workerThread() { - if (0 == s_numberOfInstances++) { + if (0 == s_numberOfInstances++) + { MQTTAsync_setTraceCallback(&ClientPaho::onLogPaho); } - // MLOGIC_COMMON_DEBUG("ClientPaho", "%1 - ctor ClientPaho %2", m_clientId, this); + + LogDebug( "[ClientPaho::ClientPaho]", std::string( " " + m_clientId + " - ctor ClientPaho ") ); parseEndpoint(_endpoint); + auto rc = MQTTAsync_create(&m_client, m_endpoint.c_str(), m_clientId.c_str(), MQTTCLIENT_PERSISTENCE_NONE, nullptr); if (MQTTASYNC_SUCCESS == rc) { MQTTAsync_setCallbacks(m_client, reinterpret_cast(this), ClientPaho::onConnectionLost, ClientPaho::onMessageArrived, ClientPaho::onDeliveryComplete); - LogDebug( "[ClientPaho]", std::string( m_clientId + " - Setting the extra onConnected callback.") ); - - auto ccb = MQTTAsync_setConnected( m_client, reinterpret_cast(this), ClientPaho::onFirstConnect ); - if( MQTTASYNC_SUCCESS == ccb ) - { - LogDebug( "[ClientPaho]", std::string( m_clientId + " - Setting the extra onConnected callback SUCCEEDED.") ); - } - else - { - LogDebug( "[ClientPaho]", std::string( m_clientId + " - Setting the extra onConnected callback FAILED.") ); - } - m_workerThread = std::thread(&ClientPaho::callbackEventHandler, this); } else { - // Do something sensible here. + LogError( "[ClientPaho::ClientPaho]", std::string( m_clientId + " - Failed to create client for endpoint " + m_endpoint + ", return code " + pahoAsyncErrorCodeToString( rc ) ) ); } } ClientPaho::~ClientPaho() { + LogDebug( "[ClientPaho::~ClientPaho]", std::string( m_clientId + " - dtor ClientPao" ) ); if( MQTTAsync_isConnected( m_client ) ) { this->unsubscribeAll(); @@ -206,6 +198,8 @@ std::int32_t ClientPaho::connect( bool wait, const mqtt_LWT &lwt ) setConnectionStatus( ConnectionStatus::ConnectInProgress ); } + LogInfo( "[ClientPaho::connect]", std::string( m_clientId + " - start connect to endpoint " + m_endpoint ) ); + MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer; conn_opts.keepAliveInterval = 5; conn_opts.cleansession = 1; @@ -214,6 +208,18 @@ std::int32_t ClientPaho::connect( bool wait, const mqtt_LWT &lwt ) conn_opts.context = this; conn_opts.automaticReconnect = 1; + // Make sure we get a signal if the promise is fulfilled + auto ccb = MQTTAsync_setConnected( m_client, reinterpret_cast(this), ClientPaho::onFirstConnect ); + if( MQTTASYNC_SUCCESS == ccb ) + { + LogDebug( "[ClientPaho]", std::string( m_clientId + " - Setting the extra onConnected callback SUCCEEDED.") ); + } + else + { + LogDebug( "[ClientPaho]", std::string( m_clientId + " - Setting the extra onConnected callback FAILED.") ); + } + + // Setup the last will and testament, if so desired. if( !lwt.topic().empty() ) { MQTTAsync_willOptions will_opts = MQTTAsync_willOptions_initializer; @@ -221,6 +227,8 @@ std::int32_t ClientPaho::connect( bool wait, const mqtt_LWT &lwt ) will_opts.topicName = lwt.topic().c_str(); conn_opts.will = &will_opts; + + LogDebug( "[ClientPaho::connect]", std::string( m_clientId + " - Set Last will and testament. Topic : " + lwt.topic() + " => Message : " + lwt.message() ) ); } else { @@ -309,8 +317,10 @@ std::int32_t ClientPaho::disconnect(bool wait, int timeoutMs) } int rc = MQTTAsync_disconnect(m_client, &disconn_opts); - if (MQTTASYNC_SUCCESS != rc) { - if (MQTTASYNC_DISCONNECTED == rc) { + if (MQTTASYNC_SUCCESS != rc) + { + if (MQTTASYNC_DISCONNECTED == rc) + { currentStatus = ConnectionStatus::Disconnected; } @@ -1140,9 +1150,10 @@ void ClientPaho::onConnectSuccess(void* context, MQTTAsync_successData* response if (context) { auto* cl = reinterpret_cast(context); - if (!response) { + if (!response) + { // connect should always have a valid response struct. - // ("ClientPaho", "onConnectSuccess - no response data"); + LogError( "[ClientPaho]", "onConnectSuccess - no response data"); } MqttSuccess resp(response->token, ConnectionData(response->alt.connect.serverURI, response->alt.connect.MQTTVersion, response->alt.connect.sessionPresent)); cl->pushIncomingEvent([cl, resp]() { cl->onConnectSuccessOnInstance(resp); }); -- libgit2 0.21.4