* @brief Runs an out-of-band connection probe using a throwaway QMqttClient. */
| 668 | * @brief Runs an out-of-band connection probe using a throwaway QMqttClient. |
| 669 | */ |
| 670 | void MQTT::PublisherWorker::runTestConnection() |
| 671 | { |
| 672 | if (m_cfg.hostname.isEmpty() || m_cfg.port == 0) { |
| 673 | Q_EMIT testConnectionFinished( |
| 674 | false, tr("Configure broker hostname and port before testing the connection.")); |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | auto* tester = new QMqttClient(this); |
| 679 | tester->setHostname(m_cfg.hostname); |
| 680 | tester->setPort(m_cfg.port); |
| 681 | tester->setClientId(m_cfg.clientId + QStringLiteral("-probe")); |
| 682 | tester->setUsername(m_cfg.username); |
| 683 | tester->setPassword(m_cfg.password); |
| 684 | tester->setCleanSession(true); |
| 685 | tester->setKeepAlive(5); |
| 686 | tester->setProtocolVersion(m_cfg.mqttVersion); |
| 687 | |
| 688 | auto* timeout = new QTimer(tester); |
| 689 | timeout->setSingleShot(true); |
| 690 | timeout->setInterval(5000); |
| 691 | |
| 692 | auto done = std::make_shared<bool>(false); |
| 693 | auto report = [this, tester, done](bool ok, const QString& detail) { |
| 694 | if (*done) |
| 695 | return; |
| 696 | |
| 697 | *done = true; |
| 698 | if (tester->state() != QMqttClient::Disconnected) |
| 699 | tester->disconnectFromHost(); |
| 700 | |
| 701 | Q_EMIT testConnectionFinished(ok, detail); |
| 702 | |
| 703 | tester->deleteLater(); |
| 704 | }; |
| 705 | |
| 706 | connect( |
| 707 | tester, &QMqttClient::stateChanged, this, [tester, report](QMqttClient::ClientState state) { |
| 708 | if (state == QMqttClient::Connected) |
| 709 | report(true, |
| 710 | tr("Successfully connected to %1:%2.").arg(tester->hostname()).arg(tester->port())); |
| 711 | }); |
| 712 | |
| 713 | connect(tester, &QMqttClient::errorChanged, this, [report](QMqttClient::ClientError error) { |
| 714 | if (error == QMqttClient::NoError) |
| 715 | return; |
| 716 | |
| 717 | report(false, describeMqttError(error)); |
| 718 | }); |
| 719 | |
| 720 | connect(timeout, &QTimer::timeout, this, [report] { |
| 721 | report(false, tr("Timed out after 5 seconds without reaching the broker.")); |
| 722 | }); |
| 723 | |
| 724 | timeout->start(); |
| 725 | if (m_cfg.sslEnabled) |
| 726 | tester->connectToHostEncrypted(m_sslConfiguration); |
| 727 | else |
nothing calls this directly
no test coverage detected