Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
IpcService.h
1 /*
2 * If not stated otherwise in this file or this component's LICENSE file the
3 * following copyright and licenses apply:
4 *
5 * Copyright 2020 Sky UK
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 /*
20  * IpcService.h
21  *
22  * Created on: 5 Jun 2015
23  * Author: riyadh
24  */
25 
26 #ifndef AI_IPC_IPCSERVICE_H
27 #define AI_IPC_IPCSERVICE_H
28 
29 #include "IpcCommon.h"
30 #include "IIpcService.h"
31 
32 #include "DbusConnection.h"
33 #include "IDbusUserIdSenderIdCache.h"
34 
35 #include <ThreadedDispatcher.h>
36 #include <Common/Interface.h>
37 
38 #include <dbus/dbus.h>
39 
40 #include <future>
41 #include <string>
42 #include <thread>
43 #include <list>
44 #include <mutex>
45 #include <set>
46 #include <atomic>
47 
49  , public std::enable_shared_from_this<IpcService>
50 {
51 public:
52  enum BusType
53  {
54  SessionBus,
55  SystemBus
56  };
57 
58 public:
59  IpcService(BusType busType, const std::string& serviceName, int defaultTimeoutMs = -1);
60  IpcService(const std::string& dbusAddress, const std::string& serviceName, int defaultTimeoutMs = -1);
61 
62  ~IpcService() override;
63 
64  virtual bool isValid() const override;
65 
66  virtual std::shared_ptr<AI_IPC::IAsyncReplyGetter> invokeMethod(const AI_IPC::Method& method, const AI_IPC::VariantList& args, int timeoutMs = -1) override;
67 
68  virtual bool invokeMethod(const AI_IPC::Method& method, const AI_IPC::VariantList& args, AI_IPC::VariantList& replyArgs, int timeoutMs = -1) override;
69 
70  virtual bool emitSignal(const AI_IPC::Signal& signal, const AI_IPC::VariantList& args) override;
71 
72  virtual std::string registerMethodHandler(const AI_IPC::Method& method, const AI_IPC::MethodHandler& handler) override;
73 
74  virtual std::string registerSignalHandler(const AI_IPC::Signal& signal, const AI_IPC::SignalHandler& handler) override;
75 
76  virtual bool unregisterHandler(const std::string& regId) override;
77 
78  virtual bool enableMonitor(const std::set<std::string>& matchRules, const AI_IPC::MonitorHandler& handler) override;
79 
80  virtual bool disableMonitor() override;
81 
82  virtual void flush() override;
83 
84  virtual bool start() override;
85 
86  virtual bool stop() override;
87 
88  bool isRegisteredObjectPath(const std::string& path);
89 
90  virtual bool isServiceAvailable(const std::string& serviceName) const override;
91 
92  std::string getBusAddress() const override;
93 
94 private:
95 
96  bool invokeMethodAndGetReply(DBusMessage *dbusSendMsg, AI_IPC::VariantList& replyArgs);
97 
98  DBusHandlerResult handleDbusMessageCb(DBusMessage *message);
99 
100  DBusHandlerResult handleDbusMessage(DBusMessage *message);
101 
102  DBusHandlerResult handleDbusSignal(const AI_IPC::Signal& signal, const AI_IPC::VariantList& argList);
103 
104  DBusHandlerResult handleDbusMethodCall(const AI_IPC::Method& method, const AI_IPC::VariantList& argList, DBusMessage *message);
105 
106  void unregisterHandlers();
107 
108  void registerObjectPath(const std::string& path);
109 
110  void unregisterObjectPath(const std::string& path);
111 
112  bool isDbusMessageAllowed(const std::string& sender, const std::string& interface);
113 
114  std::string mServiceName;
115 
116  std::string mBusAddress;
117 
118  std::shared_ptr<AI_IPC::DbusConnection> mDbusConnection;
119 
120  std::map<std::string, int> mObjectPaths;
121 
122  std::map<std::string, std::pair<AI_IPC::Method, AI_IPC::MethodHandler>> mMethodHandlers;
123 
124  std::map<std::string, std::pair<AI_IPC::Signal, AI_IPC::SignalHandler>> mSignalHandlers;
125 
126  AICommon::ThreadedDispatcher mHandlerDispatcher;
127 
128  std::mutex mMutex;
129 
130  std::atomic<bool> mRunning;
131 
132  int mNextSignalHandlerRegId;
133 
134  const int mDefaultTimeoutMs;
135 
136  bool mValid;
137 
138 #if (AI_BUILD_TYPE == AI_DEBUG)
139 
140  DBusHandlerResult handleDbusMonitorEvent(DBusMessage *dbusMsg);
141 
142  std::atomic<bool> mInMonitorMode;
143 
144  AI_IPC::MonitorHandler mMonitorCb;
145 
146  std::set<std::string> mMonitorMatchRules;
147 
148 #endif // if (AI_BUILD_TYPE == AI_DEBUG)
149 };
150 
151 #endif /* AI_IPC_IPCSERVICE_H */
A dispatcher that does all the work on a single, separate thread started in constructor.
Definition: ThreadedDispatcher.h:46
IPC service that enables us to invoke remote method and emit signals as well as to handle incoming me...
Definition: IIpcService.h:42
Definition: IpcService.h:50
virtual bool start() override
Start IPC service.
Definition: IpcService.cpp:718
virtual void flush() override
Flushes all messages out.
Definition: IpcService.cpp:707
virtual std::string registerMethodHandler(const AI_IPC::Method &method, const AI_IPC::MethodHandler &handler) override
Register a method handler.
Definition: IpcService.cpp:338
std::string getBusAddress() const override
Returns the dbus address the service is using.
Definition: IpcService.cpp:985
virtual bool enableMonitor(const std::set< std::string > &matchRules, const AI_IPC::MonitorHandler &handler) override
Enables monitor mode on the IPC service, this will effectively disable all registered method and sign...
Definition: IpcService.cpp:856
virtual bool unregisterHandler(const std::string &regId) override
Unregister a method or signal handler.
Definition: IpcService.cpp:435
virtual std::string registerSignalHandler(const AI_IPC::Signal &signal, const AI_IPC::SignalHandler &handler) override
Register a signal handler.
Definition: IpcService.cpp:399
virtual bool disableMonitor() override
Disables monitor mode and restores normal behaviour.
Definition: IpcService.cpp:917
virtual bool stop() override
Stop IPC service.
Definition: IpcService.cpp:740
virtual std::shared_ptr< AI_IPC::IAsyncReplyGetter > invokeMethod(const AI_IPC::Method &method, const AI_IPC::VariantList &args, int timeoutMs=-1) override
Invoke a method and get reply asynchronously.
Definition: IpcService.cpp:225
virtual bool isServiceAvailable(const std::string &serviceName) const override
Checks if the named service is available on the bus.
Definition: IpcService.cpp:971
virtual bool isValid() const override
Returns true if we initialised ourselves successfully.
Definition: IpcService.cpp:220
virtual bool emitSignal(const AI_IPC::Signal &signal, const AI_IPC::VariantList &args) override
Emit a signal.
Definition: IpcService.cpp:301
Method identified by a service, object, interface and method name itself.
Definition: IpcCommon.h:90
Method identified by object, interface and signal name itself.
Definition: IpcCommon.h:105