mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-22 14:55:36 +08:00
Issue #77 Break cyclic dependency between CDirectoryUtils and CNetworkUtils by factoring out canPing method
This commit is contained in:
@@ -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 <QCoreApplication>
|
||||
@@ -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());
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
#include "blackmisc/blackmiscexport.h"
|
||||
#include "blackmisc/statusmessagelist.h"
|
||||
#include "blackmisc/network/ping.h"
|
||||
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkAccessManager>
|
||||
@@ -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
|
||||
|
||||
51
src/blackmisc/network/ping.cpp
Normal file
51
src/blackmisc/network/ping.cpp
Normal file
@@ -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 <QProcess>
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
32
src/blackmisc/network/ping.h
Normal file
32
src/blackmisc/network/ping.h
Normal file
@@ -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 <QString>
|
||||
#include <QUrl>
|
||||
|
||||
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
|
||||
@@ -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());
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user