7 #include <QMetaClassInfo>
16 using namespace swift::misc::network;
33 static const QString desc(
"Mode: %1 Address: '%2' Service: '%3'");
39 case SERVERMODE_SESSIONBUS:
47 CLogMessage(
this).
warning(u
"Cannot register DBus service, check server running: dbus-daemon.exe "
48 u
"--session --address=tcp:host=192.168.0.133,port=45000");
52 case SERVERMODE_SYSTEMBUS:
60 CLogMessage(
this).
warning(u
"Cannot register DBus service, check server running: dbus-daemon.exe "
61 u
"--system --address=tcp:host=192.168.0.133,port=45000");
72 dbusAddress = dbusAddress.
append(
",bind=*");
76 m_busServer->setObjectName(
"QDBusServer: " + this->
objectName());
77 m_busServer->setAnonymousAuthenticationAllowed(
true);
80 if (m_busServer->isConnected())
82 CLogMessage(
this).
info(u
"DBus P2P Server listening on address: '%1'") << m_busServer->address();
86 &CDBusServer::registerObjectsWithP2PConnection);
127 if (canonicalAddress.
contains(
"host=") || canonicalAddress.
contains(
"port="))
131 for (
const QString &part : parts)
144 if (!ok) { port = -1; }
147 if (port < 0) { port = 45000; }
148 if (host.
isEmpty()) { host =
"127.0.0.1"; }
183 QString CDBusServer::getDBusInterfaceFromClassInfo(
QObject *
object)
185 if (!
object) {
return {}; }
191 if (name ==
"d-bus interface") {
return QString(ci.
value()); }
201 bool CDBusServer::registerObjectsWithP2PConnection(
QDBusConnection connection)
203 Q_ASSERT(!m_objects.
isEmpty());
204 m_connections.
insert(connection.
name(), connection);
205 CLogMessage(
this).info(u
"New Connection from: '%1'") << connection.
name();
207 for (
auto i = m_objects.
cbegin(); i != m_objects.
cend(); ++i)
210 const bool ok = connection.
registerObject(key, i.value(), registerOptions());
213 CLogMessage(
this).info(u
"Adding '%1' to the new connection '%2'")
214 << key << this->getDBusInterfaceFromClassInfo(i.value());
218 CLogMessage(
this).info(u
"Adding '%1' failed, connection '%2', error '%3'")
219 << key << this->getDBusInterfaceFromClassInfo(i.value()) << connection.
lastError().
message();
228 if (!
object) {
return; }
229 m_objects.
insert(path,
object);
233 switch (m_serverMode)
235 case SERVERMODE_SESSIONBUS:
241 << path << getDBusInterfaceFromClassInfo(
object);
246 << path << getDBusInterfaceFromClassInfo(
object) << connection.
lastError().
message();
250 case SERVERMODE_SYSTEMBUS:
256 << path << getDBusInterfaceFromClassInfo(
object);
261 << path << getDBusInterfaceFromClassInfo(
object) << connection.
lastError().
message();
272 << path << getDBusInterfaceFromClassInfo(
object) << connection.
name();
277 << path << getDBusInterfaceFromClassInfo(
object) << connection.
name()
283 default: Q_ASSERT_X(
false, Q_FUNC_INFO,
"Wrong server mode");
290 return m_busServer->lastError();
301 switch (m_serverMode)
321 static const QString session(
"session");
327 static const QString system(
"system");
361 if (h.
isEmpty()) { h =
"127.0.0.1"; }
388 h = parts.
at(0).trimmed();
389 p = parts.
at(1).trimmed();
391 else { p =
"45000"; }
395 Q_ASSERT_X(CNetworkUtils::isValidIPv4Address(p),
"CDBusServer::p2pAddress",
"Wrong IP in String");
396 return QStringLiteral(
"tcp:host=%1,port=%2").arg(h, p);
401 static const QString n(
"p2pConnection");
426 else {
return SERVERMODE_P2P; }
431 static const QString p2p =
"P2P";
432 static const QString session =
"session";
433 static const QString system =
"system";
437 case SERVERMODE_P2P:
return p2p;
438 case SERVERMODE_SYSTEMBUS:
return system;
439 case SERVERMODE_SESSIONBUS:
448 return CNetworkUtils::canConnect(address, port, unused, timeoutMs);
453 return CNetworkUtils::canConnect(address, port, message, timeoutMs);
460 message =
"No address.";
static void disconnectFromDBus(const QDBusConnection &connection, const QString &dBusAddress)
Disconnect from Bus/Peer to peer.
static bool dBusAddressToHostAndPort(const QString &dbusAddress, QString &o_host, int &o_port)
Extract host and port from a DBus address.
static const QDBusConnection & defaultConnection()
Default connection.
void removeAllObjects()
Remove all objects added with addObject.
static const QString & systemBusAddress()
Address denoting a system bus server.
void addObject(const QString &name, QObject *object)
Add a QObject to be exposed via DBus.
static QDBusConnection connectToDBus(const QString &dbusAddress, const QString &name={})
Connect to DBus.
static bool isSessionOrSystemAddress(const QString &address)
True if address is session or system bus address.
bool hasQDBusServer() const
True if using P2P.
QDBusError lastQDBusServerError() const
Last error.
static const QString & modeToString(ServerMode mode)
Mode to string.
static ServerMode modeOfAddress(QString address)
Return the server mode of the given address.
static bool isDBusAvailable(const QString &host, int port, int timeoutMs=1500)
Is there a DBus server running at the given address?
static const QStringList & getLogCategories()
Log categories.
const QDBusServer * qDBusServer() const
DBus server (if using P2P)
static QString normalizeAddress(const QString &address)
Turn something like 127.0.0.1:45000 into "tcp:host=127.0.0.1,port=45000".
static const QString & sessionBusAddress()
Address denoting a session bus server.
static QString p2pAddress(const QString &host, const QString &port="")
Address denoting a P2P server at the given host and port.
static const QString & p2pConnectionName()
P2P connection name.
CDBusServer(const QString &address, QObject *parent=nullptr)
Construct a server for the core service.
static bool isP2PConnection(const QDBusConnection &connection)
False if address is session or system bus connection.
static bool isQtDefaultConnection(const QDBusConnection &connection)
Is the given connection one of the default connections?
static bool isP2PAddress(const QString &address)
False if address is session or system bus address.
static const QString & coreServiceName()
Default service name.
virtual ~CDBusServer()
Destructor.
static bool isQtDBusAddress(const QString &address)
True if a valid Qt DBus address, e.g. "unix:tmpdir=/tmp", "tcp:host=127.0.0.1,port=45000".
static const QString & dbus()
DBus related.
Class for emitting a log message.
Derived & warning(const char16_t(&format)[N])
Set the severity to warning, providing a format string.
Derived & error(const char16_t(&format)[N])
Set the severity to error, providing a format string.
Derived & info(const char16_t(&format)[N])
Set the severity to info, providing a format string.
#define SWIFT_SERVICENAME
Service name of DBus service.
Free functions in swift::misc.
auto makeKeysRange(const T &container)
Returns a const CRange for iterating over the keys of a Qt associative container.
QDBusConnection connectToBus(QDBusConnection::BusType type, const QString &name)
QDBusConnection connectToPeer(const QString &address, const QString &name)
void disconnectFromBus(const QString &name)
void disconnectFromPeer(const QString &name)
bool isConnected() const const
QDBusError lastError() const const
QString name() const const
bool registerObject(const QString &path, QObject *object, QDBusConnection::RegisterOptions options)
bool registerService(const QString &serviceName)
QDBusConnection sessionBus()
QDBusConnection systemBus()
void unregisterObject(const QString &path, QDBusConnection::UnregisterMode mode)
bool unregisterService(const QString &serviceName)
QString message() const const
void newConnection(const QDBusConnection &connection)
QList< T >::const_reference at(qsizetype i) const const
QMap< Key, T >::const_iterator cbegin() const const
QMap< Key, T >::const_iterator cend() const const
QMap< Key, T >::iterator insert(QMap< Key, T >::const_iterator pos, const Key &key, const T &value)
bool isEmpty() const const
QMap< Key, T >::size_type remove(const Key &key)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void destroyed(QObject *obj)
void setObjectName(QAnyStringView name)
bool isNull() const const
QString & append(QChar ch)
QString arg(Args &&... args) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString mid(qsizetype position, qsizetype n) &&
QString number(double n, char format, int precision)
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
int toInt(bool *ok, int base) const const
QString toLower() const const
short toShort(bool *ok, int base) const const
QString trimmed() const const