Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
DobbyIpcBus.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 2016 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  * File: DobbyIpcBus.h
21  *
22  */
23 #ifndef DOBBYIPCBUS_H
24 #define DOBBYIPCBUS_H
25 
26 #include <IIpcService.h>
27 
28 #include <map>
29 #include <deque>
30 #include <mutex>
31 #include <thread>
32 #include <functional>
33 #include <condition_variable>
34 
35 
36 typedef std::function<void(bool)> ServiceHandlerFn;
37 
38 
39 // -----------------------------------------------------------------------------
58 {
59 public:
60  DobbyIpcBus(const std::string& dbusAddress,
61  const std::shared_ptr<AI_IPC::IIpcService>& ipcService);
62  DobbyIpcBus();
63  ~DobbyIpcBus();
64 
65 public:
66  bool connect(const std::string& address);
67  void disconnect();
68 
69 public:
70  const std::string& address() const;
71  const std::string& socketPath() const;
72 
73 public:
74  std::shared_ptr<AI_IPC::IAsyncReplyGetter> invokeMethod(const AI_IPC::Method& method,
75  const AI_IPC::VariantList& args,
76  int timeoutMs) const;
77 
78  bool invokeMethod(const AI_IPC::Method& method,
79  const AI_IPC::VariantList& args,
80  AI_IPC::VariantList& replyArgs) const;
81 
82  bool emitSignal(const AI_IPC::Signal& signal,
83  const AI_IPC::VariantList& args) const;
84 
85 public:
86  bool serviceAvailable(const std::string& serviceName) const;
87 
88 public:
89  int registerServiceHandler(const std::string& serviceName,
90  const ServiceHandlerFn& handlerFunc);
91 
92  int registerSignalHandler(const AI_IPC::Signal& signal,
93  const AI_IPC::SignalHandler& handlerFunc);
94 
95  void unregisterHandler(int handlerId);
96 
97 
98 private:
99  void disconnectNoLock();
100 
101  void registerServiceWatcher();
102  void serviceNameChanged(const AI_IPC::VariantList& args);
103 
104  void serviceChangeThread();
105 
106 private:
107  static std::string socketPathFromAddress(const std::string& address);
108 
109 
110 private:
111  mutable std::mutex mLock;
112 
113  std::shared_ptr<AI_IPC::IIpcService> mService;
114 
115  std::string mDbusAddress;
116  std::string mDbusSocketPath;
117 
118  int mHandlerId;
119 
120  std::string mServiceSignal;
121 
122 private:
123  typedef struct tagServiceHandler
124  {
125  tagServiceHandler(const std::string& name_,
126  const ServiceHandlerFn& handler_)
127  : name(name_), handler(handler_)
128  { }
129 
130  std::string name;
131  ServiceHandlerFn handler;
132  } ServiceHandler;
133 
134  std::map<int, ServiceHandler> mServiceHandlers;
135 
136  typedef struct tagSignalHandler
137  {
138  tagSignalHandler(const std::string& regId_,
139  const AI_IPC::Signal& signal_,
140  const AI_IPC::SignalHandler& handler_)
141  : regId(regId_), signal(signal_), handler(handler_)
142  { }
143 
144  std::string regId;
145  AI_IPC::Signal signal;
146  AI_IPC::SignalHandler handler;
147  } SignalHandler;
148 
149  std::map<int, SignalHandler> mSignalHandlers;
150 
151 private:
152  std::thread mServiceChangeThread;
153  std::mutex mServiceChangeLock;
154  std::condition_variable mServiceChangeCond;
155 
156  typedef struct tagServiceChangeEvent
157  {
158  enum EventType { Terminate, ServiceAdded, ServiceRemoved };
159 
160  tagServiceChangeEvent(EventType type_)
161  : type(type_)
162  { }
163  tagServiceChangeEvent(EventType type_, const std::string &serviceName_)
164  : type(type_)
165  , serviceName(serviceName_)
166  { }
167 
168  const EventType type;
169  std::string serviceName;
171 
172  std::deque<ServiceChangeEvent> mServiceChangeQueue;
173 };
174 
175 
176 
177 
178 #endif // !defined(DOBBYIPCBUS_H)
Wraps an IPC service object on a given bus.
Definition: DobbyIpcBus.h:58
void serviceChangeThread()
Thread function that receives notifications on service changes and then calls the install handler.
Definition: DobbyIpcBus.cpp:643
bool serviceAvailable(const std::string &serviceName) const
Queries if the given service is available on the bus.
Definition: DobbyIpcBus.cpp:447
void disconnect()
Simply disconnects from the bus.
Definition: DobbyIpcBus.cpp:265
void registerServiceWatcher()
Install a signal handler to detect services arriving / leaving the bus.
Definition: DobbyIpcBus.cpp:350
bool connect(const std::string &address)
Tries to connect to the bus at the given address.
Definition: DobbyIpcBus.cpp:177
const std::string & address() const
Simply returns the dbus address if we have one.
Definition: DobbyIpcBus.cpp:80
void disconnectNoLock()
Disconnects the service from the bus.
Definition: DobbyIpcBus.cpp:283
int registerSignalHandler(const AI_IPC::Signal &signal, const AI_IPC::SignalHandler &handlerFunc)
Registers a callback function that will be called when the given signal is received on the bus.
Definition: DobbyIpcBus.cpp:509
bool emitSignal(const AI_IPC::Signal &signal, const AI_IPC::VariantList &args) const
Sends out a signal over dbus.
Definition: DobbyIpcBus.cpp:426
int registerServiceHandler(const std::string &serviceName, const ServiceHandlerFn &handlerFunc)
Registers a callback function that will be called when the given service is added or removed from the...
Definition: DobbyIpcBus.cpp:482
const std::string & socketPath() const
Returns just the socket path of the dbus address.
Definition: DobbyIpcBus.cpp:92
void serviceNameChanged(const AI_IPC::VariantList &args)
Callback function called when dbus has informed us that a name on the bus has changed.
Definition: DobbyIpcBus.cpp:599
static std::string socketPathFromAddress(const std::string &address)
Utility function to extract the socket path from the dbus address string.
Definition: DobbyIpcBus.cpp:110
std::shared_ptr< AI_IPC::IAsyncReplyGetter > invokeMethod(const AI_IPC::Method &method, const AI_IPC::VariantList &args, int timeoutMs) const
Invokes the ipc method.
Definition: DobbyIpcBus.cpp:379
void unregisterHandler(int handlerId)
Unregisters a signal or service handler.
Definition: DobbyIpcBus.cpp:543
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
Definition: DobbyIpcBus.h:157
Definition: DobbyIpcBus.h:124
Definition: DobbyIpcBus.h:137