diff --git a/client.pro b/client.pro index e08006914..fc6916e99 100644 --- a/client.pro +++ b/client.pro @@ -52,6 +52,8 @@ equals(WITH_SAMPLES, ON) { SUBDIRS += samples/pluginmgr/sample_pluginmgr.pro SUBDIRS += samples/blackmiscquantities/sample_quantities_avionics.pro SUBDIRS += samples/blackmiscvectorgeo/sample_vector_geo.pro + SUBDIRS += samples/dbusserver/sample_dbusserver.pro + SUBDIRS += samples/dbusclient/sample_dbusclient.pro } equals(WITH_UNITTESTS, ON) { diff --git a/samples/dbusclient/atcmanager.xml b/samples/dbusclient/atcmanager.xml new file mode 100644 index 000000000..c78196aa5 --- /dev/null +++ b/samples/dbusclient/atcmanager.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/samples/dbusclient/dbusclient.cpp b/samples/dbusclient/dbusclient.cpp new file mode 100644 index 000000000..cfd198363 --- /dev/null +++ b/samples/dbusclient/dbusclient.cpp @@ -0,0 +1,96 @@ +#include +#include + +#include "dbusclient.h" + + +DBusClient::DBusClient(QObject *parent) : + QObject(parent), m_connection("daemon") +{ + m_connection = QDBusConnection::connectToPeer("tcp:host=127.0.0.1,port=6668", "daemon"); + + if(!m_connection.isConnected()) + { + qWarning() << m_connection.lastError().message(); + } + + pilotManagerIface = new org::vatsim::pilotClient::PlaneManager("org.vatsim.PilotClient", "/PlaneManager", m_connection, this); + atcManagerIface = new org::vatsim::pilotClient::AtcManager("org.vatsim.PilotClien", "/AtcManager", m_connection, this); + fsdClientIface = new org::vatsim::pilotClient::FsdClient("org.vatsim.PilotClient", "/FsdClient", m_connection, this); + connect(fsdClientIface, &org::vatsim::pilotClient::FsdClient::connectedTo, this, &DBusClient::slot_connected); +} + +DBusClient::~DBusClient() +{ + +} + +void DBusClient::connectTo(const QString &host) +{ + fsdClientIface->connectTo(host); +} + +void DBusClient::disconnectFrom() +{ + //iface->disconnectFrom(); +} + +void DBusClient::slot_connected( const QString & host) +{ + qDebug() << "Conntected to " << host; + qDebug() << "So lets collect some information... "; + + + printPilotList(); + printAtcList(); + + //qDebug() << iface->getMetar("EDDM"); +} + +void DBusClient::slot_disconnected() +{ + qDebug() << "Disconntected from Vatsim"; +} + +void DBusClient::printPilotList() +{ + Q_ASSERT(pilotManagerIface); + + qDebug() << "Online pilots: "; + + QStringList planes = pilotManagerIface->pilotList(); + if (planes.isEmpty()) + { + qWarning() << "Got no pilots. Something went wrong!"; + return; + } + + foreach(QString plane, planes) + { + qDebug() << plane; + } +} + +void DBusClient::printAtcList() +{ + Q_ASSERT(atcManagerIface); + + qDebug() << "Online controllers: "; + + QStringList controllers = atcManagerIface->atcList(); + if (controllers.isEmpty()) + { + qWarning() << "Got no controllers. Something went wrong!"; + return; + } + + foreach(QString controller, controllers) + { + qDebug() << controller; + } +} + +void DBusClient::printError() +{ + qWarning() << m_connection.lastError().message(); +} diff --git a/samples/dbusclient/dbusclient.h b/samples/dbusclient/dbusclient.h new file mode 100644 index 000000000..af9ffddd1 --- /dev/null +++ b/samples/dbusclient/dbusclient.h @@ -0,0 +1,44 @@ +#ifndef DBUSCLIENT_H +#define DBUSCLIENT_H + +#include +#include +#include + +#include "planemanager_interface.h" +#include "atcmanager_interface.h" +#include "fsdclient_interface.h" + +class DBusClient : public QObject +{ + Q_OBJECT + +private: + QDBusConnection m_connection; + org::vatsim::pilotClient::PlaneManager *pilotManagerIface; + org::vatsim::pilotClient::AtcManager *atcManagerIface; + org::vatsim::pilotClient::FsdClient *fsdClientIface; + +public: + explicit DBusClient(QObject *parent = 0); + ~DBusClient(); + + void connectTo(const QString &host); + void disconnectFrom(); + + void printPilotList(); + void printAtcList(); + + void printError(); + +signals: + +public slots: + +private slots: + void slot_connected(const QString &host); + void slot_disconnected(); + +}; + +#endif // DBUSCLIENT_H diff --git a/samples/dbusclient/fsdclient.xml b/samples/dbusclient/fsdclient.xml new file mode 100644 index 000000000..4a50e30da --- /dev/null +++ b/samples/dbusclient/fsdclient.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/samples/dbusclient/main.cpp b/samples/dbusclient/main.cpp new file mode 100644 index 000000000..e17953f9c --- /dev/null +++ b/samples/dbusclient/main.cpp @@ -0,0 +1,12 @@ +#include +#include "dbusclient.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + DBusClient client; + + client.connectTo("dev.vatsim-germany.org"); + + return a.exec(); +} diff --git a/samples/dbusclient/planemanager.xml b/samples/dbusclient/planemanager.xml new file mode 100644 index 000000000..c7b38e162 --- /dev/null +++ b/samples/dbusclient/planemanager.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/samples/dbusclient/sample_dbusclient.pro b/samples/dbusclient/sample_dbusclient.pro new file mode 100644 index 000000000..96aada047 --- /dev/null +++ b/samples/dbusclient/sample_dbusclient.pro @@ -0,0 +1,24 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2013-07-07T15:31:07 +# +#------------------------------------------------- + +QT += core dbus + +QT -= gui + +TARGET = DBusClient +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + +DBUS_INTERFACES += planemanager.xml atcmanager.xml fsdclient.xml + + +SOURCES += main.cpp \ + dbusclient.cpp + +HEADERS += \ + dbusclient.h diff --git a/samples/dbusserver/atcmanager.cpp b/samples/dbusserver/atcmanager.cpp new file mode 100644 index 000000000..ed9bc3134 --- /dev/null +++ b/samples/dbusserver/atcmanager.cpp @@ -0,0 +1,12 @@ +#include "atcmanager.h" + +CAtcManager::CAtcManager(QObject *parent) : + QObject(parent) +{ + m_atcList << "EDDM_N_TWR" << "KJFK_GND" << "LOVV_CTR"; +} + +QStringList CAtcManager::atcList() const +{ + return m_atcList; +} diff --git a/samples/dbusserver/atcmanager.h b/samples/dbusserver/atcmanager.h new file mode 100644 index 000000000..b1f2fd9b2 --- /dev/null +++ b/samples/dbusserver/atcmanager.h @@ -0,0 +1,30 @@ +#ifndef ATCMANAGER_H +#define ATCMANAGER_H + +#include +#include + + +class CAtcManager : public QObject +{ + Q_OBJECT + + QStringList m_atcList; //!< List of all controllers with their callsigns + +public: + CAtcManager(QObject *parent = 0); + ~CAtcManager() {} + + /*! + * \brief Returns a list of all controllers with their callsigns + * \return atcList + */ + QStringList atcList() const; + +signals: + +public slots: + +}; + +#endif // ATCMANAGER_H diff --git a/samples/dbusserver/atcmanager.xml b/samples/dbusserver/atcmanager.xml new file mode 100644 index 000000000..c78196aa5 --- /dev/null +++ b/samples/dbusserver/atcmanager.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/samples/dbusserver/atcmanagerhandler.cpp b/samples/dbusserver/atcmanagerhandler.cpp new file mode 100644 index 000000000..d83c76415 --- /dev/null +++ b/samples/dbusserver/atcmanagerhandler.cpp @@ -0,0 +1,37 @@ +#include "atcmanager.h" +#include "blackcore/dbusserver.h" + +#include "atcmanagerhandler.h" + +// It is really IMPORTANT to include this header. +// Otherwise it won't be generated by qmake and the +// project can't be build +#include "atcmanager_adaptor.h" + +CAtcManagerHandler::CAtcManagerHandler(QObject *parent) : + QObject(parent), m_dbusserver(0), m_parent(0) +{ + m_dbusPath = "/AtcManager"; + m_parent = qobject_cast(parent); + + new AtcManagerAdaptor(this); +} + +CAtcManagerHandler::~CAtcManagerHandler() +{ + +} + +void CAtcManagerHandler::setDBusServer(BlackCore::CDBusServer *dbusServer) +{ + m_dbusserver = dbusServer; + + if (m_dbusserver) + m_dbusserver->addObject(m_dbusPath, this); +} + +QStringList CAtcManagerHandler::atcList() const +{ + qDebug() << "Somebody requested the list of controllers. Here you go..."; + return m_parent->atcList(); +} diff --git a/samples/dbusserver/atcmanagerhandler.h b/samples/dbusserver/atcmanagerhandler.h new file mode 100644 index 000000000..20a1e0f48 --- /dev/null +++ b/samples/dbusserver/atcmanagerhandler.h @@ -0,0 +1,36 @@ +#ifndef ATCMANAGERHANDLER_H +#define ATCMANAGERHANDLER_H + +#include +#include + +namespace BlackCore +{ + class CDBusServer; +} + +class CAtcManager; + +class CAtcManagerHandler : public QObject +{ + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "org.vatsim.pilotClient.AtcManager") + Q_PROPERTY( QStringList atcList READ atcList) + + QStringList atcList() const; + + BlackCore::CDBusServer *m_dbusserver; + CAtcManager *m_parent; + + QString m_dbusPath; + +public: + CAtcManagerHandler(QObject *parent = NULL); + ~CAtcManagerHandler(); + + void setDBusServer(BlackCore::CDBusServer *dbusServer); + + +}; + +#endif // ATCMANAGERHANDLER_H diff --git a/samples/dbusserver/fsdclient.cpp b/samples/dbusserver/fsdclient.cpp new file mode 100644 index 000000000..640e2b101 --- /dev/null +++ b/samples/dbusserver/fsdclient.cpp @@ -0,0 +1,13 @@ +#include + +#include "fsdclient.h" + +CFsdClient::CFsdClient(QObject *parent) : + QObject(parent) +{ +} + +void CFsdClient::connectTo(const QString &host) +{ + qDebug() << "Client requests to connect to " << host; +} diff --git a/samples/dbusserver/fsdclient.h b/samples/dbusserver/fsdclient.h new file mode 100644 index 000000000..58c526a17 --- /dev/null +++ b/samples/dbusserver/fsdclient.h @@ -0,0 +1,20 @@ +#ifndef FSDCLIENT_H +#define FSDCLIENT_H + +#include + +class CFsdClient : public QObject +{ + Q_OBJECT +public: + CFsdClient(QObject *parent = 0); + + void connectTo(const QString &host); + +signals: + +public slots: + +}; + +#endif // FSDCLIENT_H diff --git a/samples/dbusserver/fsdclient.xml b/samples/dbusserver/fsdclient.xml new file mode 100644 index 000000000..4a50e30da --- /dev/null +++ b/samples/dbusserver/fsdclient.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/samples/dbusserver/fsdclienthandler.cpp b/samples/dbusserver/fsdclienthandler.cpp new file mode 100644 index 000000000..5fd7729a8 --- /dev/null +++ b/samples/dbusserver/fsdclienthandler.cpp @@ -0,0 +1,32 @@ + +#include "blackcore/dbusserver.h" +#include "fsdclienthandler.h" +#include "fsdclient.h" + +// It is really IMPORTANT to include this header. +// Otherwise it won't be generated by qmake and the +// project can't be build +#include "fsdclient_adaptor.h" + +CFsdClientHandler::CFsdClientHandler(QObject *parent) : + QObject(parent), m_dbusserver(0), m_parent(0) +{ + m_dbusPath = "/FsdClient"; + m_parent = qobject_cast(parent); + + new FsdClientAdaptor(this); +} + +void CFsdClientHandler::connectTo(const QString &host) +{ + m_parent->connectTo(host); + emit connectedTo(host); +} + +void CFsdClientHandler::setDBusServer(BlackCore::CDBusServer *dbusServer) +{ + m_dbusserver = dbusServer; + + if (m_dbusserver) + m_dbusserver->addObject(m_dbusPath, this); +} diff --git a/samples/dbusserver/fsdclienthandler.h b/samples/dbusserver/fsdclienthandler.h new file mode 100644 index 000000000..5ce1fe4bd --- /dev/null +++ b/samples/dbusserver/fsdclienthandler.h @@ -0,0 +1,39 @@ +#ifndef FSDCLIENTHANDLER_H +#define FSDCLIENTHANDLER_H + +#include + + +namespace BlackCore +{ + class CDBusServer; +} + +class CFsdClient; + +class CFsdClientHandler : public QObject +{ + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "org.vatsim.pilotClient.FsdClient") + + BlackCore::CDBusServer *m_dbusserver; + CFsdClient *m_parent; + + QString m_dbusPath; + +public: + CFsdClientHandler(QObject *parent = 0); + ~CFsdClientHandler() {} + + Q_INVOKABLE void connectTo(const QString &host); + + void setDBusServer(BlackCore::CDBusServer *dbusServer); + +signals: + void connectedTo( const QString &host); + +public slots: + +}; + +#endif // FSDCLIENTHANDLER_H diff --git a/samples/dbusserver/main.cpp b/samples/dbusserver/main.cpp new file mode 100644 index 000000000..292c14b75 --- /dev/null +++ b/samples/dbusserver/main.cpp @@ -0,0 +1,35 @@ +#include +#include +#include "blackcore/dbusserver.h" +#include "planemanagerhandler.h" +#include "atcmanagerhandler.h" +#include "atcmanager.h" +#include "fsdclient.h" +#include "fsdclienthandler.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + BlackCore::CDBusServer server; + + // Setting up our objects + CPlaneManager planeManager; + CAtcManager atcManager; + CFsdClient fsdclient; + + + // Setting up the handler to expose the objects via DBus + CPlaneManagerHandler planeManagerHandler(&planeManager); + CAtcManagerHandler atcManagerHandler(&atcManager); + CFsdClientHandler fsdClientHandler (&fsdclient); + + // Pass the DBus server to the handlers. This registers also + // the handler in the DBus server and makes it available + // via the interface. + planeManagerHandler.setDBusServer(&server); + atcManagerHandler.setDBusServer(&server); + fsdClientHandler.setDBusServer(&server); + + return a.exec(); +} diff --git a/samples/dbusserver/planemanager.cpp b/samples/dbusserver/planemanager.cpp new file mode 100644 index 000000000..b2edb0d31 --- /dev/null +++ b/samples/dbusserver/planemanager.cpp @@ -0,0 +1,16 @@ +#include "planemanager.h" + +CPlaneManager::CPlaneManager(QObject *parent) : + QObject(parent) +{ + m_pilotsList << "DLH123" << "BER456" << "SWS789"; +} + +CPlaneManager::~CPlaneManager() +{ +} + +QStringList CPlaneManager::pilotList() const +{ + return m_pilotsList; +} diff --git a/samples/dbusserver/planemanager.h b/samples/dbusserver/planemanager.h new file mode 100644 index 000000000..38fb6674a --- /dev/null +++ b/samples/dbusserver/planemanager.h @@ -0,0 +1,28 @@ +#ifndef PLANEMANAGER_H +#define PLANEMANAGER_H + +#include +#include + +class CPlaneManager : public QObject +{ + Q_OBJECT + + QStringList m_pilotsList; //!< List of all pilots with their callsigns + +public: + CPlaneManager(QObject *parent = 0); + ~CPlaneManager(); + + /*! + * \brief Returns a list of all pilots with their callsigns + * \return pilotList + */ + QStringList pilotList() const; + +signals: + +public slots: +}; + +#endif // PLANEMANAGER_H diff --git a/samples/dbusserver/planemanager.xml b/samples/dbusserver/planemanager.xml new file mode 100644 index 000000000..c7b38e162 --- /dev/null +++ b/samples/dbusserver/planemanager.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/samples/dbusserver/planemanagerhandler.cpp b/samples/dbusserver/planemanagerhandler.cpp new file mode 100644 index 000000000..b4add309d --- /dev/null +++ b/samples/dbusserver/planemanagerhandler.cpp @@ -0,0 +1,33 @@ +#include +#include "planemanagerhandler.h" + +// It is really IMPORTANT to include this header. +// Otherwise it won't be generated by qmake and the +// project can't be build +#include "planemanager_adaptor.h" + +CPlaneManagerHandler::CPlaneManagerHandler(QObject *parent) : + QObject(parent), m_dbusserver(0), m_parent(0) +{ + m_dbusPath = "/PlaneManager"; + m_parent = qobject_cast(parent); + + new PlaneManagerAdaptor(this); + +} + +void CPlaneManagerHandler::setDBusServer(BlackCore::CDBusServer *dbusServer) +{ + m_dbusserver = dbusServer; + + // We add ourselves to the DBus server. This is needed, because + // DBus has to register all exposed objects for each new connection + if (m_dbusserver) + m_dbusserver->addObject(m_dbusPath, this); +} + +QStringList CPlaneManagerHandler::pilotList() const +{ + qDebug() << "Somebody requested the list of pilots. Here you go..."; + return m_parent->pilotList(); +} diff --git a/samples/dbusserver/planemanagerhandler.h b/samples/dbusserver/planemanagerhandler.h new file mode 100644 index 000000000..ed2b29d3c --- /dev/null +++ b/samples/dbusserver/planemanagerhandler.h @@ -0,0 +1,34 @@ +#ifndef PLANEMANAGERHANDLER_H +#define PLANEMANAGERHANDLER_H + +#include +#include "planemanager.h" +#include "blackcore/dbusserver.h" + +class CPlaneManagerHandler : public QObject +{ + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "org.vatsim.pilotClient.PlaneManager") + Q_PROPERTY( QStringList pilotList READ pilotList) + +public: + CPlaneManagerHandler(QObject *parent = NULL); + ~CPlaneManagerHandler() {} + + void setDBusServer(BlackCore::CDBusServer *dbusServer); + +signals: + +public slots: + +private: + QStringList pilotList() const; + + BlackCore::CDBusServer *m_dbusserver; + CPlaneManager *m_parent; + + QString m_dbusPath; + +}; + +#endif // PLANEMANAGERHANDLER_H diff --git a/samples/dbusserver/sample_dbusserver.pro b/samples/dbusserver/sample_dbusserver.pro new file mode 100644 index 000000000..725300350 --- /dev/null +++ b/samples/dbusserver/sample_dbusserver.pro @@ -0,0 +1,32 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2013-07-06T22:37:57 +# +#------------------------------------------------- + +QT += core dbus + +QT -= gui + +TARGET = DBusServer +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + +DBUS_ADAPTORS += planemanager.xml atcmanager.xml fsdclient.xml + +DEPENDPATH += . ../../src +INCLUDEPATH += . ../../src + +HEADERS += *.h +SOURCES += *.cpp + +LIBS += -L../../lib -lblackcore -lblackmisc + +win32:!win32-g++*: PRE_TARGETDEPS += ../../lib/blackmisc.lib \ + ../../lib/blackcore.lib +else: PRE_TARGETDEPS += ../../lib/libblackmisc.a \ + ../../lib/libblackcore.a + +DESTDIR = ../../bin diff --git a/src/blackcore/blackcore.pro b/src/blackcore/blackcore.pro index 08d1b0717..98361abb9 100644 --- a/src/blackcore/blackcore.pro +++ b/src/blackcore/blackcore.pro @@ -1,5 +1,5 @@ # GUI is required for the matrix classes -QT += network +QT += network dbus TARGET = blackcore TEMPLATE = lib diff --git a/src/blackcore/dbusserver.cpp b/src/blackcore/dbusserver.cpp new file mode 100644 index 000000000..02ccc24cf --- /dev/null +++ b/src/blackcore/dbusserver.cpp @@ -0,0 +1,56 @@ +#include + +#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::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 + + diff --git a/src/blackcore/dbusserver.h b/src/blackcore/dbusserver.h new file mode 100644 index 000000000..e65c473ae --- /dev/null +++ b/src/blackcore/dbusserver.h @@ -0,0 +1,51 @@ +#ifndef BLACKCORE_DBUSSERVER_H +#define BLACKCORE_DBUSSERVER_H + +#include +#include +#include +#include +#include +#include + +namespace BlackCore +{ + + class CDBusServer : public QObject + { + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "org.vatsim.pilotClient") + + private: + QDBusServer m_busServer; //!< QDBusServer implementation + + QMap m_objects; //!< Mapping of all exposed objects + + QMap 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