diff --git a/src/blackmisc/directoryutils.cpp b/src/blackmisc/directoryutils.cpp index 44c9e5d51..d55ad823c 100644 --- a/src/blackmisc/directoryutils.cpp +++ b/src/blackmisc/directoryutils.cpp @@ -11,7 +11,7 @@ #include "blackmisc/directoryutils.h" #include "blackmisc/fileutils.h" #include "blackmisc/stringutils.h" -#include "blackmisc/network/networkutils.h" +#include "blackmisc/network/ping.h" #include "blackmisc/range.h" #include "blackconfig/buildconfig.h" #include @@ -123,7 +123,7 @@ namespace BlackMisc // outdated, test again } - const bool p = CNetworkUtils::canPing(m); + const bool p = canPing(m); if (p) { good.insert(m, QDateTime::currentSecsSinceEpoch()); diff --git a/src/blackmisc/network/networkutils.cpp b/src/blackmisc/network/networkutils.cpp index 6e40b7411..96432cfaf 100644 --- a/src/blackmisc/network/networkutils.cpp +++ b/src/blackmisc/network/networkutils.cpp @@ -58,38 +58,6 @@ namespace BlackMisc return 3 * getTimeoutMs(); } - bool CNetworkUtils::canPing(const QString &hostAddress) - { - if (hostAddress.isEmpty()) { return false; } - QProcess process; - process.setProgram("ping"); - if (CBuildConfig::isRunningOnWindowsNtPlatform()) - { - process.setArguments({ "-n", "1", hostAddress }); - } - else - { - // all UNIX alike - process.setArguments({ "-c", "1", hostAddress }); - } - process.start(); - process.waitForFinished(); - const int rc = process.exitCode(); - if (rc != 0) { return false; } - - const QString std = process.readAllStandardOutput(); - const QString err = process.readAllStandardError(); - if (std.contains("unreachable", Qt::CaseInsensitive)) { return false; } - if (err.contains("unreachable", Qt::CaseInsensitive)) { return false; } - return true; - } - - bool CNetworkUtils::canPing(const QUrl &url) - { - if (url.isEmpty()) { return false; } - return CNetworkUtils::canPing(url.host()); - } - QStringList CNetworkUtils::getKnownLocalIpV4Addresses() { QStringList ips; @@ -374,7 +342,7 @@ namespace BlackMisc if (!url.isEmpty()) { const QString host = url.host(); - const bool canPing = CNetworkUtils::canPing(host); + const bool canPing = Network::canPing(host); const CStatusMessage ping(cats, canPing ? CStatusMessage::SeverityInfo : CStatusMessage::SeverityError, "Host: " + host + " ping: " + boolToYesNo(canPing)); msgs.push_back(ping); diff --git a/src/blackmisc/network/networkutils.h b/src/blackmisc/network/networkutils.h index 3b025148c..44c022d23 100644 --- a/src/blackmisc/network/networkutils.h +++ b/src/blackmisc/network/networkutils.h @@ -13,6 +13,7 @@ #include "blackmisc/blackmiscexport.h" #include "blackmisc/statusmessagelist.h" +#include "blackmisc/network/ping.h" #include #include @@ -50,14 +51,6 @@ namespace BlackMisc //! Default for timeout (long) static int getLongTimeoutMs(); - //! Can ping the address? - //! \note uses OS ping - static bool canPing(const QString &hostAddress); - - //! Can ping the address? - //! \note uses OS ping - static bool canPing(const QUrl &url); - //! Can connect? //! \param hostAddress 130.4.20.3, or myserver.com //! \param port 80, 1234 diff --git a/src/blackmisc/network/ping.cpp b/src/blackmisc/network/ping.cpp new file mode 100644 index 000000000..e88289099 --- /dev/null +++ b/src/blackmisc/network/ping.cpp @@ -0,0 +1,51 @@ +/* Copyright (C) 2013 + * swift Project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated, + * or distributed except according to the terms contained in the LICENSE file. + */ + +#include "blackmisc/network/ping.h" +#include "blackconfig/buildconfig.h" +#include + +using namespace BlackConfig; + +namespace BlackMisc +{ + namespace Network + { + bool canPing(const QString &hostAddress) + { + if (hostAddress.isEmpty()) { return false; } + QProcess process; + process.setProgram("ping"); + if (CBuildConfig::isRunningOnWindowsNtPlatform()) + { + process.setArguments({ "-n", "1", hostAddress }); + } + else + { + // all UNIX alike + process.setArguments({ "-c", "1", hostAddress }); + } + process.start(); + process.waitForFinished(); + const int rc = process.exitCode(); + if (rc != 0) { return false; } + + const QString std = process.readAllStandardOutput(); + const QString err = process.readAllStandardError(); + if (std.contains("unreachable", Qt::CaseInsensitive)) { return false; } + if (err.contains("unreachable", Qt::CaseInsensitive)) { return false; } + return true; + } + + bool canPing(const QUrl &url) + { + if (url.isEmpty()) { return false; } + return canPing(url.host()); + } + } +} \ No newline at end of file diff --git a/src/blackmisc/network/ping.h b/src/blackmisc/network/ping.h new file mode 100644 index 000000000..bef7a093c --- /dev/null +++ b/src/blackmisc/network/ping.h @@ -0,0 +1,32 @@ +/* Copyright (C) 2013 + * swift Project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated, + * or distributed except according to the terms contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_NETWORK_PING_H +#define BLACKMISC_NETWORK_PING_H + +#include "blackmisc/blackmiscexport.h" +#include +#include + +namespace BlackMisc +{ + namespace Network + { + //! Can ping the address? + //! \note uses OS ping + BLACKMISC_EXPORT bool canPing(const QString &hostAddress); + + //! Can ping the address? + //! \note uses OS ping + BLACKMISC_EXPORT bool canPing(const QUrl &url); + } +} + +#endif diff --git a/src/blackmisc/simulation/aircraftmodellist.cpp b/src/blackmisc/simulation/aircraftmodellist.cpp index c68815b6f..d0117a003 100644 --- a/src/blackmisc/simulation/aircraftmodellist.cpp +++ b/src/blackmisc/simulation/aircraftmodellist.cpp @@ -1662,7 +1662,7 @@ namespace BlackMisc for (const QString &m : uncMachines) { - const bool ping = CNetworkUtils::canPing(m); + const bool ping = canPing(m); if (!ping) { msgs.push_back(CStatusMessage(this).validationError(u"Cannot ping UNC machine(s): %1. UNC files: %2") << m << uncFiles.size()); diff --git a/tests/blackcore/testconnectivity/testconnectivity.cpp b/tests/blackcore/testconnectivity/testconnectivity.cpp index 721ae7065..e115a5c0d 100644 --- a/tests/blackcore/testconnectivity/testconnectivity.cpp +++ b/tests/blackcore/testconnectivity/testconnectivity.cpp @@ -99,7 +99,7 @@ namespace BlackCoreTest constexpr int max = 5; for (int i = 0; i < max; i++) { - bool ok = CNetworkUtils::canPing(host); + bool ok = canPing(host); if (!ok) { QSKIP(qPrintable("Cannot ping " + url.getFullUrl())); } } int elapsedMs = timer.elapsed(); @@ -112,7 +112,7 @@ namespace BlackCoreTest QVERIFY2(sApp->getNetworkWatchdog(), "No network watchdog"); const CUrl dbUrl = CNetworkWatchdog::dbTestUrl(); qDebug() << "Using DB test URL: " << dbUrl.toQString(); - const bool ok = CNetworkUtils::canPing(dbUrl); + const bool ok = canPing(dbUrl); if (!ok) { QSKIP(qPrintable("Cannot ping " + dbUrl.getFullUrl())); } // only if URL is reachable