mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-10 22:15:34 +08:00
Add DBus P2P support to XSwiftBus
ref T291
This commit is contained in:
committed by
Klaus Basan
parent
fd45de89d9
commit
488ff96ce6
@@ -24,6 +24,12 @@ namespace XSwiftBus
|
||||
dbus_threads_init_default();
|
||||
}
|
||||
|
||||
CDBusConnection::CDBusConnection(DBusConnection *connection)
|
||||
{
|
||||
dbus_connection_ref(connection);
|
||||
// Don't exit application, if the connection is disconnected
|
||||
dbus_connection_set_exit_on_disconnect(connection, false);
|
||||
m_connection.reset(connection);
|
||||
}
|
||||
|
||||
CDBusConnection::~CDBusConnection()
|
||||
|
||||
@@ -34,9 +34,12 @@ namespace XSwiftBus
|
||||
//! Bus type
|
||||
enum BusType { SessionBus };
|
||||
|
||||
//! Constructor
|
||||
//! Default constructor
|
||||
CDBusConnection();
|
||||
|
||||
//! Constructor
|
||||
CDBusConnection(DBusConnection *connection);
|
||||
|
||||
//! Destructor
|
||||
~CDBusConnection();
|
||||
|
||||
|
||||
86
src/xswiftbus/dbusserver.cpp
Normal file
86
src/xswiftbus/dbusserver.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
#include <utility>
|
||||
|
||||
/* Copyright (C) 2018
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "dbusserver.h"
|
||||
#include "dbusobject.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
|
||||
namespace XSwiftBus
|
||||
{
|
||||
|
||||
CDBusServer::CDBusServer()
|
||||
{
|
||||
dbus_threads_init_default();
|
||||
}
|
||||
|
||||
CDBusServer::~CDBusServer()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
bool CDBusServer::listen(const std::string &address)
|
||||
{
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
m_server.reset(dbus_server_listen(address.c_str(), &error));
|
||||
dbus_server_set_new_connection_function(m_server.get(), onNewConnection, this, nullptr);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CDBusServer::isConnected() const
|
||||
{
|
||||
return dbus_server_get_is_connected(m_server.get());
|
||||
}
|
||||
|
||||
void CDBusServer::close()
|
||||
{
|
||||
dbus_server_disconnect(m_server.get());
|
||||
}
|
||||
|
||||
void CDBusServer::setDispatcher(CDBusDispatcher *dispatcher)
|
||||
{
|
||||
assert(dispatcher);
|
||||
|
||||
m_dispatcher = dispatcher;
|
||||
|
||||
dbus_server_set_watch_functions(
|
||||
m_server.get(),
|
||||
dispatcher->m_watchCallbacks.add,
|
||||
dispatcher->m_watchCallbacks.remove,
|
||||
dispatcher->m_watchCallbacks.toggled,
|
||||
&dispatcher->m_watchCallbacks, nullptr);
|
||||
|
||||
dbus_server_set_timeout_functions(
|
||||
m_server.get(),
|
||||
dispatcher->m_timeoutCallbacks.add,
|
||||
dispatcher->m_timeoutCallbacks.remove,
|
||||
dispatcher->m_timeoutCallbacks.toggled,
|
||||
&dispatcher->m_timeoutCallbacks, nullptr);
|
||||
}
|
||||
|
||||
void CDBusServer::onNewConnection(DBusServer *, DBusConnection *conn)
|
||||
{
|
||||
INFO_LOG("onNewConnection");
|
||||
auto dbusConnection = std::make_shared<CDBusConnection>(conn);
|
||||
m_newConnectionFunc(dbusConnection);
|
||||
}
|
||||
|
||||
void CDBusServer::onNewConnection(DBusServer *server, DBusConnection *conn, void *data)
|
||||
{
|
||||
auto *obj = static_cast<CDBusServer *>(data);
|
||||
return obj->onNewConnection(server, conn);
|
||||
}
|
||||
|
||||
}
|
||||
84
src/xswiftbus/dbusserver.h
Normal file
84
src/xswiftbus/dbusserver.h
Normal file
@@ -0,0 +1,84 @@
|
||||
/* Copyright (C) 2018
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
#ifndef BLACKSIM_XSWIFTBUS_DBUSSERVER_H
|
||||
#define BLACKSIM_XSWIFTBUS_DBUSSERVER_H
|
||||
|
||||
#include "dbusmessage.h"
|
||||
#include "dbuserror.h"
|
||||
#include "dbuscallbacks.h"
|
||||
#include "dbusdispatcher.h"
|
||||
|
||||
#include <event2/event.h>
|
||||
#include <dbus/dbus.h>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
|
||||
namespace XSwiftBus
|
||||
{
|
||||
|
||||
class CDBusObject;
|
||||
|
||||
//! DBus connection
|
||||
class CDBusServer : public IDispatchable
|
||||
{
|
||||
public:
|
||||
//! New connection handler function
|
||||
using NewConnectionFunc = std::function<void(std::shared_ptr<CDBusConnection>)>;
|
||||
|
||||
//! Constructor
|
||||
CDBusServer();
|
||||
|
||||
//! Destructor
|
||||
~CDBusServer();
|
||||
|
||||
//! Set the dispatcher
|
||||
void setDispatcher(CDBusDispatcher *dispatcher);
|
||||
|
||||
//! Connect to bus
|
||||
bool listen(const std::string &address);
|
||||
|
||||
//! Is connected?
|
||||
bool isConnected() const;
|
||||
|
||||
void dispatch() {}
|
||||
|
||||
//! Close connection
|
||||
void close();
|
||||
|
||||
//! Get the last error
|
||||
CDBusError lastError() const { return m_lastError; }
|
||||
|
||||
//! Set the function to be used for handling new connections.
|
||||
void setNewConnectionFunc(const NewConnectionFunc &func)
|
||||
{
|
||||
m_newConnectionFunc = func;
|
||||
}
|
||||
|
||||
private:
|
||||
void onNewConnection(DBusServer *server, DBusConnection *conn);
|
||||
static void onNewConnection(DBusServer *server, DBusConnection *conn, void *data);
|
||||
|
||||
struct DBusServerDeleter
|
||||
{
|
||||
void operator()(DBusServer *obj) const { dbus_server_unref(obj); }
|
||||
};
|
||||
|
||||
std::unique_ptr<DBusServer, DBusServerDeleter> m_server;
|
||||
CDBusError m_lastError;
|
||||
CDBusDispatcher *m_dispatcher;
|
||||
NewConnectionFunc m_newConnectionFunc;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // guard
|
||||
@@ -36,13 +36,13 @@ namespace XSwiftBus
|
||||
m_planeViewSubMenu = m_menu.subMenu("Follow Plane View");
|
||||
planeViewOwnAircraftMenuItem = m_planeViewSubMenu.item("Own Aircraft", [this] { switchToOwnAircraftView(); });
|
||||
|
||||
m_dbusThread = std::thread([this]()
|
||||
/*m_dbusThread = std::thread([this]()
|
||||
{
|
||||
while(!m_shouldStop)
|
||||
{
|
||||
m_dbusConnection->runBlockingEventLoop();
|
||||
}
|
||||
});
|
||||
});*/
|
||||
|
||||
XPLMRegisterFlightLoopCallback(flightLoopCallback, -1, this);
|
||||
}
|
||||
@@ -67,25 +67,47 @@ namespace XSwiftBus
|
||||
|
||||
m_traffic->setPlaneViewMenu(m_planeViewSubMenu);
|
||||
|
||||
// Todo: retry if it fails
|
||||
bool success = m_dbusConnection->connect(CDBusConnection::SessionBus);
|
||||
|
||||
if (!success)
|
||||
if (m_useDBusP2P)
|
||||
{
|
||||
// Print error
|
||||
return;
|
||||
m_dbusP2PServer = std::make_unique<CDBusServer>();
|
||||
|
||||
// FIXME: make listen address configurable
|
||||
m_dbusP2PServer->listen("tcp:host=127.0.0.1,port=45000");
|
||||
m_dbusP2PServer->setDispatcher(&m_dbusDispatcher);
|
||||
|
||||
m_dbusP2PServer->setNewConnectionFunc([this](const std::shared_ptr<CDBusConnection> &conn)
|
||||
{
|
||||
m_dbusConnection = conn;
|
||||
m_dbusConnection->setDispatcher(&m_dbusDispatcher);
|
||||
m_service->setDBusConnection(m_dbusConnection);
|
||||
m_service->registerDBusObjectPath(m_service->InterfaceName(), m_service->ObjectPath());
|
||||
m_traffic->setDBusConnection(m_dbusConnection);
|
||||
m_traffic->registerDBusObjectPath(m_traffic->InterfaceName(), m_traffic->ObjectPath());
|
||||
m_weather->setDBusConnection(m_dbusConnection);
|
||||
m_weather->registerDBusObjectPath(m_weather->InterfaceName(), m_weather->ObjectPath());
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
// Todo: retry if it fails
|
||||
bool success = m_dbusConnection->connect(CDBusConnection::SessionBus);
|
||||
|
||||
m_dbusConnection->setDispatcher(&m_dbusDispatcher);
|
||||
m_dbusConnection->requestName(xswiftbusServiceName());
|
||||
if (!success)
|
||||
{
|
||||
// Print error
|
||||
return;
|
||||
}
|
||||
|
||||
m_service->setDBusConnection(m_dbusConnection);
|
||||
m_service->registerDBusObjectPath(m_service->InterfaceName(), m_service->ObjectPath());
|
||||
m_traffic->setDBusConnection(m_dbusConnection);
|
||||
m_traffic->registerDBusObjectPath(m_traffic->InterfaceName(), m_traffic->ObjectPath());
|
||||
m_weather->setDBusConnection(m_dbusConnection);
|
||||
m_weather->registerDBusObjectPath(m_weather->InterfaceName(), m_weather->ObjectPath());
|
||||
m_dbusConnection->setDispatcher(&m_dbusDispatcher);
|
||||
m_dbusConnection->requestName(xswiftbusServiceName());
|
||||
|
||||
m_service->setDBusConnection(m_dbusConnection);
|
||||
m_service->registerDBusObjectPath(m_service->InterfaceName(), m_service->ObjectPath());
|
||||
m_traffic->setDBusConnection(m_dbusConnection);
|
||||
m_traffic->registerDBusObjectPath(m_traffic->InterfaceName(), m_traffic->ObjectPath());
|
||||
m_weather->setDBusConnection(m_dbusConnection);
|
||||
m_weather->registerDBusObjectPath(m_weather->InterfaceName(), m_weather->ObjectPath());
|
||||
}
|
||||
|
||||
INFO_LOG("XSwiftBus started.");
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
#include "dbusconnection.h"
|
||||
#include "dbusdispatcher.h"
|
||||
#include "dbusserver.h"
|
||||
#include "datarefs.h"
|
||||
#include "XPLM/XPLMCamera.h"
|
||||
#include "menus.h"
|
||||
@@ -55,6 +56,7 @@ namespace XSwiftBus
|
||||
|
||||
private:
|
||||
CDBusDispatcher m_dbusDispatcher;
|
||||
std::unique_ptr<CDBusServer> m_dbusP2PServer;
|
||||
std::shared_ptr<CDBusConnection> m_dbusConnection;
|
||||
std::unique_ptr<CService> m_service;
|
||||
std::unique_ptr<CTraffic> m_traffic;
|
||||
@@ -72,6 +74,8 @@ namespace XSwiftBus
|
||||
std::thread m_dbusThread;
|
||||
bool m_shouldStop = false;
|
||||
|
||||
bool m_useDBusP2P = true;
|
||||
|
||||
void startServer(CDBusConnection::BusType bus);
|
||||
void switchToOwnAircraftView();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user