1 // dbusserver.h 2 // 3 // Copyright (C) 2019 - swift Project Community / Contributors (http://swift-project.org/) 4 // Adapted to Flightgear by Lars Toenning <dev@ltoenning.de> 5 // 6 // This program is free software; you can redistribute it and/or 7 // modify it under the terms of the GNU General Public License as 8 // published by the Free Software Foundation; either version 2 of the 9 // License, or (at your option) any later version. 10 // 11 // This program is distributed in the hope that it will be useful, but 12 // WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 // General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with this program; if not, write to the Free Software 18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 20 #ifndef BLACKSIM_FGSWIFTBUS_DBUSSERVER_H 21 #define BLACKSIM_FGSWIFTBUS_DBUSSERVER_H 22 23 #include "dbusmessage.h" 24 #include "dbuserror.h" 25 #include "dbuscallbacks.h" 26 #include "dbusdispatcher.h" 27 28 #include <event2/event.h> 29 #include <dbus/dbus.h> 30 #include <string> 31 #include <unordered_map> 32 #include <vector> 33 #include <memory> 34 #include <functional> 35 36 namespace FGSwiftBus 37 { 38 39 class CDBusObject; 40 41 //! DBus connection 42 class CDBusServer : public IDispatchable 43 { 44 public: 45 //! New connection handler function 46 using NewConnectionFunc = std::function<void(std::shared_ptr<CDBusConnection>)>; 47 48 //! Constructor 49 CDBusServer(); 50 51 //! Destructor 52 ~CDBusServer(); 53 54 //! Set the dispatcher 55 void setDispatcher(CDBusDispatcher *dispatcher); 56 57 //! Connect to bus 58 bool listen(const std::string &address); 59 60 //! Is connected? 61 bool isConnected() const; 62 dispatch()63 void dispatch() override {} 64 65 //! Close connection 66 void close(); 67 68 //! Get the last error lastError()69 CDBusError lastError() const { return m_lastError; } 70 71 //! Set the function to be used for handling new connections. setNewConnectionFunc(const NewConnectionFunc & func)72 void setNewConnectionFunc(const NewConnectionFunc &func) 73 { 74 m_newConnectionFunc = func; 75 } 76 77 private: 78 void onNewConnection(DBusServer *server, DBusConnection *conn); 79 static void onNewConnection(DBusServer *server, DBusConnection *conn, void *data); 80 81 struct DBusServerDeleter 82 { operatorDBusServerDeleter83 void operator()(DBusServer *obj) const { dbus_server_unref(obj); } 84 }; 85 86 CDBusDispatcher *m_dispatcher = nullptr; 87 std::unique_ptr<DBusServer, DBusServerDeleter> m_server; 88 CDBusError m_lastError; 89 NewConnectionFunc m_newConnectionFunc; 90 }; 91 92 } 93 94 #endif // guard 95