mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-30 20:15:35 +08:00
refs #452 adjusted network utilities
* formatting * new connect / URL methods
This commit is contained in:
committed by
Mathew Sutcliffe
parent
b5477c7c94
commit
567cead20d
@@ -9,6 +9,7 @@
|
||||
#include <QCoreApplication>
|
||||
#include <QHostAddress>
|
||||
#include <QAbstractSocket>
|
||||
#include <QUrl>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
@@ -48,7 +49,7 @@ namespace BlackMisc
|
||||
}
|
||||
|
||||
/*
|
||||
* my IP
|
||||
* My IP
|
||||
*/
|
||||
QStringList CNetworkUtils::getKnownIpAddresses()
|
||||
{
|
||||
@@ -103,6 +104,44 @@ namespace BlackMisc
|
||||
return CNetworkUtils::canConnect(server.getAddress(), server.getPort(), message, timeoutMs);
|
||||
}
|
||||
|
||||
/*
|
||||
* Can connect url?
|
||||
*/
|
||||
bool CNetworkUtils::canConnect(const QString &url, QString &message, int timeoutMs)
|
||||
{
|
||||
if (url.isEmpty())
|
||||
{
|
||||
message = QObject::tr("Missing URL", "BlackMisc");
|
||||
return false;
|
||||
}
|
||||
return canConnect(QUrl(url), message, timeoutMs);
|
||||
}
|
||||
|
||||
/*
|
||||
* Can connect url?
|
||||
*/
|
||||
bool CNetworkUtils::canConnect(const QUrl &url, QString &message, int timeoutMs)
|
||||
{
|
||||
if (!url.isValid())
|
||||
{
|
||||
message = QObject::tr("Invalid URL: %1", "BlackMisc").arg(url.toString());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (url.isRelative())
|
||||
{
|
||||
message = QObject::tr("Relative URL cannot be tested: %1", "BlackMisc").arg(url.toString());
|
||||
return false;
|
||||
}
|
||||
|
||||
QString scheme(url.scheme().toLower());
|
||||
int p = scheme.contains("https") ? 443 : 80;
|
||||
p = url.port(80);
|
||||
|
||||
QString host(url.host());
|
||||
return canConnect(host, p, message, timeoutMs);
|
||||
}
|
||||
|
||||
/*
|
||||
* Valid IPv4 address
|
||||
*/
|
||||
@@ -131,4 +170,23 @@ namespace BlackMisc
|
||||
if (!success) return false;
|
||||
return (p >= 1 && p <= 65535);
|
||||
}
|
||||
|
||||
/*
|
||||
* Build URL
|
||||
*/
|
||||
QString CNetworkUtils::buildUrl(const QString &protocol, const QString &server, const QString &baseUrl, const QString &serviceUrl)
|
||||
{
|
||||
Q_ASSERT_X(protocol.length() > 3, Q_FUNC_INFO, "worng protocol");
|
||||
Q_ASSERT_X(!server.isEmpty(), Q_FUNC_INFO, "missing server");
|
||||
Q_ASSERT_X(!serviceUrl.isEmpty(), Q_FUNC_INFO, "missing service URL");
|
||||
|
||||
QString url(server);
|
||||
if (!baseUrl.isEmpty())
|
||||
{
|
||||
url.append("/").append(baseUrl);
|
||||
}
|
||||
url.append("/").append(serviceUrl);
|
||||
url.replace("//", "/");
|
||||
return protocol + "://" + url;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
@@ -14,42 +14,44 @@
|
||||
|
||||
#include "blackmiscexport.h"
|
||||
#include "network/server.h"
|
||||
#include <QUrl>
|
||||
#include <QStringList>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
//! Utilities, e.g. checking whether a network connection can be established
|
||||
class BLACKMISC_EXPORT CNetworkUtils
|
||||
{
|
||||
|
||||
public:
|
||||
/*!
|
||||
* Is a connected interface available?
|
||||
* \param withDebugOutput enables some debugging output
|
||||
* \return
|
||||
*/
|
||||
//!
|
||||
//! Is a connected interface available?
|
||||
//! \param withDebugOutput enables some debugging output
|
||||
//! \return
|
||||
static bool hasConnectedInterface(bool withDebugOutput = false);
|
||||
|
||||
/*!
|
||||
* Can connect?
|
||||
* \param hostAddress 130.4.20.3, or myserver.com
|
||||
* \param port 80, 1234
|
||||
* \param timeoutMs
|
||||
* \param message human readable message
|
||||
* \return
|
||||
*/
|
||||
//!
|
||||
//! Can connect?
|
||||
//! \param hostAddress 130.4.20.3, or myserver.com
|
||||
//! \param port 80, 1234
|
||||
//! \param timeoutMs
|
||||
//! \param message human readable message
|
||||
//! \return
|
||||
static bool canConnect(const QString &hostAddress, int port, QString &message, int timeoutMs = 1500);
|
||||
|
||||
/*!
|
||||
* Can connect to server?
|
||||
* \param server
|
||||
* \param message human readable message
|
||||
* \param timeoutMs
|
||||
* \return
|
||||
*/
|
||||
//!
|
||||
//! Can connect to server?
|
||||
//! \param server
|
||||
//! \param message human readable message
|
||||
//! \param timeoutMs
|
||||
//! \return
|
||||
static bool canConnect(const BlackMisc::Network::CServer &server, QString &message, int timeoutMs = 1500);
|
||||
|
||||
//! Can connect to URL?
|
||||
static bool canConnect(const QString &url, QString &message, int timeoutMs = 1500);
|
||||
|
||||
//! Can connect to URL?
|
||||
static bool canConnect(const QUrl &url, QString &message, int timeoutMs = 1500);
|
||||
|
||||
//! Find out my IPv4 address, empty if not possible
|
||||
static QStringList getKnownIpAddresses();
|
||||
|
||||
@@ -62,6 +64,9 @@ namespace BlackMisc
|
||||
//! Valid port
|
||||
static bool isValidPort(const QString &port);
|
||||
|
||||
//! Build / concatenate an URL
|
||||
static QString buildUrl(const QString &protocol, const QString &server, const QString &baseUrl, const QString &serviceUrl);
|
||||
|
||||
private:
|
||||
//! Deleted constructor
|
||||
CNetworkUtils() {}
|
||||
|
||||
Reference in New Issue
Block a user