Debian Codegraph @

src/Network/Swift/dbusdispatcher.h

raw symbols SCIP
1 /*
2 * SPDX-FileCopyrightText: (C) 2019-2022 swift Project Community / Contributors (https://swift-project.org/)
3 * SPDX-FileCopyrightText: (C) 2019-2022 Lars Toenning <[email protected]>
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #pragma once
8
9 #include "dbuscallbacks.h"
10
11 #include <dbus/dbus.h>
12 #include <event2/event.h>
13
14 #include <memory>
15 #include <unordered_map>
16 #include <vector>
17
18 namespace flightgear::swift {
19
20 class WatchHandler;
21 class TimeoutHandler;
22 class CDBusConnection;
23 class CDBusDispatcher;
24
25 //! Dispatchable Interface
26 class IDispatchable
27 {
28 public:
29 //! Default constructor
30 IDispatchable() = default;
31
32 //! Default destructor
33 virtual ~IDispatchable() = default;
34
35 //! Dispatch execution method
36 virtual void dispatch() = 0;
37
38 private:
39 friend CDBusDispatcher;
40 };
41
42 //! DBus Dispatcher
43 class CDBusDispatcher
44 {
45 public:
46 //! Constructor
47 CDBusDispatcher();
48
49 //! Destructor
50 virtual ~CDBusDispatcher();
51
52 //! Add dispatchable object
53 void add(IDispatchable* dispatchable);
54
55 //! Remove dispatchable object
56 void remove(IDispatchable* dispatchable);
57
58 //! Waits for events to be dispatched and handles them
59 void waitAndRun();
60
61 //! Dispatches ready handlers and returns without waiting
62 void runOnce();
63
64 private:
65 friend class WatchHandler;
66 friend class TimeoutHandler;
67 friend class Timer;
68 friend class CDBusConnection;
69 friend class CDBusServer;
70
71 struct EventBaseDeleter {
72 void operator()(event_base* obj) const { event_base_free(obj); }
73 };
74
75 using WatchCallbacks = DBusAsyncCallbacks<DBusWatch>;
76 using TimeoutCallbacks = DBusAsyncCallbacks<DBusTimeout>;
77
78 void dispatch();
79
80 dbus_bool_t dbusAddWatch(DBusWatch* watch);
81 void dbusRemoveWatch(DBusWatch* watch);
82 void dbusWatchToggled(DBusWatch* watch);
83
84 dbus_bool_t dbusAddTimeout(DBusTimeout* timeout);
85 void dbusRemoveTimeout(DBusTimeout* timeout);
86 void dbusTimeoutToggled(DBusTimeout* timeout);
87
88 WatchCallbacks m_watchCallbacks;
89 TimeoutCallbacks m_timeoutCallbacks;
90 std::unordered_multimap<evutil_socket_t, std::unique_ptr<WatchHandler>> m_watchers;
91 std::vector<std::unique_ptr<TimeoutHandler>> m_timeouts;
92 std::unique_ptr<event_base, EventBaseDeleter> m_eventBase;
93
94 std::vector<IDispatchable*> m_dispatchList;
95 };
96 } // namespace flightgear::swift