mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-31 04:25:35 +08:00
refs #42:
- Added CDBusServer into Blackcore library - Added DBus server and client sample
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
# GUI is required for the matrix classes
|
||||
QT += network
|
||||
QT += network dbus
|
||||
|
||||
TARGET = blackcore
|
||||
TEMPLATE = lib
|
||||
|
||||
56
src/blackcore/dbusserver.cpp
Normal file
56
src/blackcore/dbusserver.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <QDebug>
|
||||
|
||||
#include "dbusserver.h"
|
||||
|
||||
namespace BlackCore
|
||||
{
|
||||
|
||||
// TODO:
|
||||
// - Change constructor to use address from the config file
|
||||
// - Ammend debug message according to the final result in the forum discussion
|
||||
|
||||
CDBusServer::CDBusServer(QObject *parent) :
|
||||
QObject(parent), m_busServer("tcp:host=127.0.0.1,port=6668", parent)
|
||||
{
|
||||
if (!m_busServer.isConnected())
|
||||
{
|
||||
qWarning() << m_busServer.lastError().message();
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "Server is running on" << m_busServer.address();
|
||||
}
|
||||
|
||||
connect(&m_busServer, &QDBusServer::newConnection, this, &CDBusServer::newConnection);
|
||||
}
|
||||
|
||||
void CDBusServer::newConnection(const QDBusConnection & connection)
|
||||
{
|
||||
QMap<QString, QObject*>::ConstIterator i = m_objects.begin();
|
||||
QDBusConnection newConnection(connection);
|
||||
|
||||
m_DBusConnections.insert(newConnection.name(), newConnection);
|
||||
|
||||
qDebug() << "New Connection from: " << newConnection.name();
|
||||
|
||||
while (i != m_objects.end())
|
||||
{
|
||||
qDebug() << "Adding " << i.key() << "to the new connection.";
|
||||
newConnection.registerObject(i.key(), i.value());
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
void CDBusServer::addObject(const QString &name, QObject *object)
|
||||
{
|
||||
m_objects.insert(name, object);
|
||||
}
|
||||
|
||||
void CDBusServer::printError()
|
||||
{
|
||||
qWarning() << m_busServer.lastError().name();
|
||||
}
|
||||
|
||||
} // namespace BlackCore
|
||||
|
||||
|
||||
51
src/blackcore/dbusserver.h
Normal file
51
src/blackcore/dbusserver.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#ifndef BLACKCORE_DBUSSERVER_H
|
||||
#define BLACKCORE_DBUSSERVER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QtDBus/QDBusServer>
|
||||
#include <QtDBus/QDBusError>
|
||||
#include <QtDBus/QDBusConnection>
|
||||
#include <QStringList>
|
||||
#include <QMap>
|
||||
|
||||
namespace BlackCore
|
||||
{
|
||||
|
||||
class CDBusServer : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_CLASSINFO("D-Bus Interface", "org.vatsim.pilotClient")
|
||||
|
||||
private:
|
||||
QDBusServer m_busServer; //!< QDBusServer implementation
|
||||
|
||||
QMap<QString, QObject*> m_objects; //!< Mapping of all exposed objects
|
||||
|
||||
QMap<QString, QDBusConnection> m_DBusConnections; //!< Mapping of all DBusConnection objects
|
||||
|
||||
public:
|
||||
|
||||
/*!
|
||||
* \brief Constructor
|
||||
* \param parent
|
||||
*/
|
||||
CDBusServer(QObject *parent = 0);
|
||||
|
||||
/*!
|
||||
* \brief Adds a QObject to be exposed to DBus
|
||||
* \param name
|
||||
* \param object
|
||||
*/
|
||||
void addObject(const QString &name, QObject *object);
|
||||
|
||||
|
||||
void printError();
|
||||
|
||||
public slots:
|
||||
void newConnection(const QDBusConnection & connection);
|
||||
|
||||
};
|
||||
|
||||
} // namespace BlackCore
|
||||
|
||||
#endif // BLACKCORE_DBUSSERVER_H
|
||||
Reference in New Issue
Block a user