mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-10 22:15:34 +08:00
refs #507, checks if URLs / DBus can be connected / reached
* new class CFailoverUrlList * improved utility methods * CDBusServer: utility function to check server / DBus can be connected * check in swift GUI if DBus is available
This commit is contained in:
committed by
Mathew Sutcliffe
parent
b24cd2e9c7
commit
6dd8fb333e
@@ -18,7 +18,6 @@ using namespace BlackMisc::Network;
|
||||
|
||||
namespace BlackCore
|
||||
{
|
||||
|
||||
/*
|
||||
* Constructor
|
||||
* Remark, without the default "unix:tmpdir=/tmp" any refereal to address crashes
|
||||
@@ -71,10 +70,13 @@ namespace BlackCore
|
||||
case SERVERMODE_P2P:
|
||||
default:
|
||||
{
|
||||
QString dbusAddress(CDBusServer::isQtDBusAddress(address) ? address : "tcp:host=127.0.0.1,port=45000");
|
||||
dbusAddress = dbusAddress.toLower().trimmed().replace(' ', "");
|
||||
if (!dbusAddress.contains("bind=")) { dbusAddress = dbusAddress.append(",bind=*"); }
|
||||
this->m_serverMode = CDBusServer::SERVERMODE_P2P;
|
||||
this->m_busServer.reset(
|
||||
new QDBusServer(
|
||||
CDBusServer::isQtDBusAddress(address) ? address : "tcp:host=127.0.0.1,port=45000", // "unix:tmpdir=/tmp"
|
||||
dbusAddress, // "unix:tmpdir=/tmp"
|
||||
parent)
|
||||
);
|
||||
m_busServer->setAnonymousAuthenticationAllowed(true);
|
||||
@@ -94,9 +96,6 @@ namespace BlackCore
|
||||
} // switch
|
||||
}
|
||||
|
||||
/*
|
||||
* Name of service
|
||||
*/
|
||||
const QString &CDBusServer::ServiceName()
|
||||
{
|
||||
static const QString sn(BLACKCORE_RUNTIME_SERVICENAME);
|
||||
@@ -111,28 +110,58 @@ namespace BlackCore
|
||||
if (!success) { CLogMessage(this).warning("Failed to launch dbus-daemon!"); }
|
||||
}
|
||||
|
||||
/*
|
||||
* Check for P2P address
|
||||
*/
|
||||
bool CDBusServer::isP2P(const QString &address)
|
||||
bool CDBusServer::isP2PAddress(const QString &address)
|
||||
{
|
||||
return CDBusServer::addressToDBusMode(address) == SERVERMODE_P2P;
|
||||
}
|
||||
|
||||
/*
|
||||
* Is Qt DBus address
|
||||
*/
|
||||
bool CDBusServer::splitDBusAddressIntoHostAndPort(const QString &dbusAddress, QString &host, int &port)
|
||||
{
|
||||
bool ok = false;
|
||||
QString dbus(dbusAddress.trimmed().toLower().replace(' ', ""));
|
||||
if (dbus.contains("host=") || dbus.contains("port="))
|
||||
{
|
||||
// "tcp:host=foo.com,port=123"
|
||||
QStringList parts(dbus.split(','));
|
||||
for (const QString &p : parts)
|
||||
{
|
||||
if (p.contains("host="))
|
||||
{
|
||||
host = p.mid(p.lastIndexOf("=") + 1).trimmed();
|
||||
}
|
||||
else if (p.contains("port="))
|
||||
{
|
||||
bool ok;
|
||||
port = p.mid(p.lastIndexOf("=") + 1).trimmed().toInt(&ok);
|
||||
if (!ok) { port = -1; }
|
||||
}
|
||||
}
|
||||
if (port < 0) { port = 45000; }
|
||||
if (host.isEmpty()) { host = "127.0.0.1"; }
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if (!ok)
|
||||
{
|
||||
host = "";
|
||||
port = -1;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool CDBusServer::isQtDBusAddress(const QString &address)
|
||||
{
|
||||
return
|
||||
(address.startsWith("tcp:") ||
|
||||
address.startsWith("unix:")
|
||||
(address.contains("tcp:") ||
|
||||
address.contains("unix:")
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class info
|
||||
*/
|
||||
bool CDBusServer::isSessionOrSystemAddress(const QString &address)
|
||||
{
|
||||
return address == sessionDBusServer() || address == systemDBusServer();
|
||||
}
|
||||
|
||||
const QString CDBusServer::getClassInfo(QObject *object)
|
||||
{
|
||||
if (!object) return "";
|
||||
@@ -147,9 +176,6 @@ namespace BlackCore
|
||||
return "";
|
||||
}
|
||||
|
||||
/*
|
||||
* Connection established
|
||||
*/
|
||||
bool CDBusServer::ps_registerObjectsWithP2PConnection(const QDBusConnection &connection)
|
||||
{
|
||||
Q_ASSERT(!this->m_objects.isEmpty());
|
||||
@@ -170,9 +196,6 @@ namespace BlackCore
|
||||
return success;
|
||||
}
|
||||
|
||||
/*
|
||||
* Add the objects
|
||||
*/
|
||||
void CDBusServer::addObject(const QString &path, QObject *object)
|
||||
{
|
||||
if (!object) { return; }
|
||||
@@ -217,9 +240,6 @@ namespace BlackCore
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Last error
|
||||
*/
|
||||
QDBusError CDBusServer::lastQDBusServerError() const
|
||||
{
|
||||
if (!hasQDBusServer()) { return QDBusError(); }
|
||||
@@ -231,17 +251,11 @@ namespace BlackCore
|
||||
return this->m_busServer.data();
|
||||
}
|
||||
|
||||
/*
|
||||
* Real server?
|
||||
*/
|
||||
bool CDBusServer::hasQDBusServer() const
|
||||
{
|
||||
return !this->m_busServer.isNull();
|
||||
}
|
||||
|
||||
/*
|
||||
* Unregister all objects
|
||||
*/
|
||||
void CDBusServer::unregisterAllObjects()
|
||||
{
|
||||
if (this->m_objects.isEmpty()) return;
|
||||
@@ -267,13 +281,31 @@ namespace BlackCore
|
||||
} // all paths
|
||||
}
|
||||
|
||||
/*
|
||||
* p2pDBusServer
|
||||
*/
|
||||
const QDBusConnection &CDBusServer::defaultConnection()
|
||||
{
|
||||
static QDBusConnection defaultConnection("default");
|
||||
return defaultConnection;
|
||||
}
|
||||
|
||||
const QString &CDBusServer::sessionDBusServer()
|
||||
{
|
||||
static QString session("session");
|
||||
return session;
|
||||
}
|
||||
|
||||
const QString &CDBusServer::systemDBusServer()
|
||||
{
|
||||
static QString system("system");
|
||||
return system;
|
||||
}
|
||||
|
||||
QString CDBusServer::p2pAddress(const QString &host, const QString &port)
|
||||
{
|
||||
QString h = host.isEmpty() ? "127.0.0.1" : host.trimmed();
|
||||
QString p = port;
|
||||
|
||||
// can handle host and port separately or combined
|
||||
// such as "myHost::1234"
|
||||
if (port.isEmpty())
|
||||
{
|
||||
if (h.contains(":"))
|
||||
@@ -296,9 +328,6 @@ namespace BlackCore
|
||||
return p2p;
|
||||
}
|
||||
|
||||
/*
|
||||
* Fix address
|
||||
*/
|
||||
QString CDBusServer::fixAddressToDBusAddress(const QString &address)
|
||||
{
|
||||
if (address.isEmpty() || address == sessionDBusServer() || address == systemDBusServer()) { return address; }
|
||||
@@ -306,9 +335,6 @@ namespace BlackCore
|
||||
return p2pAddress(address);
|
||||
}
|
||||
|
||||
/*
|
||||
* Convert address to mode
|
||||
*/
|
||||
CDBusServer::ServerMode CDBusServer::addressToDBusMode(const QString &address)
|
||||
{
|
||||
QString a = address.toLower();
|
||||
@@ -317,4 +343,55 @@ namespace BlackCore
|
||||
else { return SERVERMODE_P2P; }
|
||||
}
|
||||
|
||||
bool CDBusServer::isDBusAvailable(const QString &address, int port, int timeoutMs)
|
||||
{
|
||||
QString m;
|
||||
return CNetworkUtils::canConnect(address, port, m, timeoutMs);
|
||||
}
|
||||
|
||||
bool CDBusServer::isDBusAvailable(const QString &address, int port, QString &message, int timeoutMs)
|
||||
{
|
||||
return CNetworkUtils::canConnect(address, port, message, timeoutMs);
|
||||
}
|
||||
|
||||
bool CDBusServer::isDBusAvailable(const QString &dbusAddress, QString &message, int timeoutMs)
|
||||
{
|
||||
if (dbusAddress.isEmpty()) { message = "no address"; return false; }
|
||||
if (isP2PAddress(dbusAddress))
|
||||
{
|
||||
QString host;
|
||||
int port = -1;
|
||||
if (splitDBusAddressIntoHostAndPort(dbusAddress, host, port))
|
||||
{
|
||||
return isDBusAvailable(host, port, message, timeoutMs);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
QDBusConnection connection(
|
||||
(dbusAddress == systemDBusServer()) ?
|
||||
QDBusConnection::systemBus() :
|
||||
QDBusConnection::connectToBus(QDBusConnection::SessionBus, ServiceName())
|
||||
);
|
||||
bool success = connection.isConnected();
|
||||
|
||||
// further checks would need to go here
|
||||
// failing session bus not detected yet
|
||||
|
||||
message = connection.lastError().message();
|
||||
connection.disconnectFromBus(ServiceName());
|
||||
return success;
|
||||
}
|
||||
}
|
||||
|
||||
bool CDBusServer::isDBusAvailable(const QString &dbusAddress, int timeoutMs)
|
||||
{
|
||||
QString m;
|
||||
return isDBusAvailable(dbusAddress, m, timeoutMs);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -74,25 +74,13 @@ namespace BlackCore
|
||||
void unregisterAllObjects();
|
||||
|
||||
//! Default connection
|
||||
static const QDBusConnection &defaultConnection()
|
||||
{
|
||||
static QDBusConnection defaultConnection("default");
|
||||
return defaultConnection;
|
||||
}
|
||||
static const QDBusConnection &defaultConnection();
|
||||
|
||||
//! Denotes a session DBus server
|
||||
static const QString &sessionDBusServer()
|
||||
{
|
||||
static QString session("session");
|
||||
return session;
|
||||
}
|
||||
static const QString &sessionDBusServer();
|
||||
|
||||
//! Denotes a session DBus server
|
||||
static const QString &systemDBusServer()
|
||||
{
|
||||
static QString system("system");
|
||||
return system;
|
||||
}
|
||||
static const QString &systemDBusServer();
|
||||
|
||||
//! Denotes a P2P DBus server, e.g. "tcp:host=192.168.3.3,port=45000"
|
||||
//! \remarks it is valid to pass only one string as host:port
|
||||
@@ -107,6 +95,27 @@ namespace BlackCore
|
||||
//! Qt DBus address, e.g. "unix:tmpdir=/tmp", "tcp:host=127.0.0.1,port=45000"
|
||||
static bool isQtDBusAddress(const QString &address);
|
||||
|
||||
//! Session or system DBus address
|
||||
static bool isSessionOrSystemAddress(const QString &address);
|
||||
|
||||
//! Check if address means a real server with P2P connection
|
||||
static bool isP2PAddress(const QString &address);
|
||||
|
||||
//! Split DBus address into host and port
|
||||
static bool splitDBusAddressIntoHostAndPort(const QString &dbusAddress, QString &host, int &port);
|
||||
|
||||
//! Is DBus available?
|
||||
static bool isDBusAvailable(const QString &address, int port, int timeoutMs = 1500);
|
||||
|
||||
//! Is DBus available?
|
||||
static bool isDBusAvailable(const QString &address, int port, QString &message, int timeoutMs = 1500);
|
||||
|
||||
//! Is DBus available?
|
||||
static bool isDBusAvailable(const QString &dbusAddress, QString &message, int timeoutMs = 1500);
|
||||
|
||||
//! Is DBus available?
|
||||
static bool isDBusAvailable(const QString &dbusAddress, int timeoutMs = 1500);
|
||||
|
||||
private:
|
||||
ServerMode m_serverMode = SERVERMODE_P2P;
|
||||
QScopedPointer<QDBusServer> m_busServer; //!< QDBusServer implementation
|
||||
@@ -116,9 +125,6 @@ namespace BlackCore
|
||||
//! Manually launch our shipped dbus daemon
|
||||
void launchDbusDaemon();
|
||||
|
||||
//! Check if address means a real server with P2P connection
|
||||
static bool isP2P(const QString &address);
|
||||
|
||||
//! Get the class info
|
||||
static const QString getClassInfo(QObject *object);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user