1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
// GDBus++ - glib2 GDBus C++ wrapper
//
// SPDX-License-Identifier: AGPL-3.0-only
//
// Copyright (C) OpenVPN Inc <sales@openvpn.net>
// Copyright (C) David Sommerseth <davids@openvpn.net>
//
/**
* @file signals/group.hpp
*
* @brief Declaration of the DBus::Signals::Group, which is a helper
* class to provide a simple API to send various type of D-Bus
* signals from a single object using different methods.
* See tests/signal-group.cpp for an example.
*/
#pragma once
#include <map>
#include <memory>
#include <vector>
#include "../connection.hpp"
#include "../object/path.hpp"
#include "emit.hpp"
namespace DBus {
namespace Signals {
/**
* This defines a single signal variable member and the D-Bus data type
* it uses
*/
struct SignalArgument
{
public:
std::string name; ///< D-Bus signal argument name
std::string type; ///< D-Bus signal argument data type
};
/**
* A collection of SignalArgument elements, which combined
* describes all the arguments (variables) sent by a specific
* D-Bus signal
*/
using SignalArgList = std::vector<struct SignalArgument>;
/**
* Generate the D-Bus signal signature (composed data type) of a
* SignalArgList
*
* @param list SignalArgList to compose a data type signature for
* @return const std::string
*/
const std::string SignalArgSignature(const SignalArgList &list);
/**
* The Signals::Group class is a helper class to easily create
* an API in other classes to map C++ methods to sending specific
* and predefined D-Bus signals.
*
* This class will take care of ensuring only known signals are
* sent and that the data they send is formatted correctly.
*/
class Group
{
public:
using Ptr = std::shared_ptr<Group>;
/**
* Create a new Signals::Group container. This object will keep an
* overview of all registered signals and their signal signature, to
* be able to generate the D-Bus introspection data and to validate
* signal parameters before sending them.
*
* This class is not intended to be used directly, but through another
* class inheriting this class.
*
* @code
*
* class MySignalGroup : public DBus::Signals::Group
* {
* public:
* using Ptr = std::shared_ptr<MySignalGroup>;
*
* MySignalGroup(DBus::Connection::Ptr connection)
* : DBus::Signals::Group(connection)
* {
* }
* };
*
* int main()
* {
* // ...
* auto connection = DBus::Connection::Create(DBus::BusType::Session)
* auto my_signals = DBus::Signals::Group::Create<MySignalGroup>(connection);
* // ...
* }
*
* @endcode
*
* @tparam C Class inheriting Signals::Group, which is being
* instantiated
* @tparam Args Argument template for the constructor of the class being
* instantiated
* @param conn DBus::Connection which signals will be sent through
* @param all The list of arguments forwarded to the constructor
* @return std::shared_ptr<C> to the newly created object
*/
template <typename C, typename... Args>
[[nodiscard]] static std::shared_ptr<C> Create(DBus::Connection::Ptr conn, Args &&...all)
{
return std::shared_ptr<C>(new C(conn, std::forward<Args>(all)...));
}
virtual ~Group() noexcept = default;
/**
* Register a new signal this instantiated object supports
*
* This method takes a std::vector of the SignalArgument struct, which
* can be done inline:
*
* @code
* RegisterSignal("signalName", {{"arg1", "s"}, {"arg2", "i"}});
* @endcode
*
* The line above will allow a signal named 'signalName' to be sent
* via this object, where it takes two arguments - arg1 (string) and
* arg2 (int)
*
* @param signal_name std::string of the signal name to register
* @param signal_type SignalArgList containing all the arguments this signal
* will contain
*/
void RegisterSignal(const std::string &signal_name, const SignalArgList &signal_type);
/**
* Create a new Signals::Signal object and tie it to default signal target
* list
*
* Note: The Signals::Signal constructor expects a Signal::Emit::Ptr
* as the first argument. This argument is handled automatically by
* this method. Because of this, the Signals::Signal implementation
* must implement the Signal::Emit::Ptr as the first argument in its
* implementation
*
* @tparam C Signal::Signal implementation class
* @tparam T Argument forward type
* @param args Arguments to the Signals::Signal implementation's
* constructor, without the Signal::Emit::Ptr argument
*
* @return std::shared_ptr<C> to the created Signals::Signal object
* implementation
*/
template <typename C, typename... T>
std::shared_ptr<C> CreateSignal(T &&...args)
{
auto sig = std::shared_ptr<C>(new C(signal_groups.at("__default__"),
std::forward<T>(args)...));
RegisterSignal(sig->GetName(), sig->GetArguments());
return sig;
}
/**
* Generates the D-Bus introspection data for all the registered
* signals. This is typically not used directly, but called via the
* @DBus::Object::Base::GenerateIntrospection() method.
*
* @return const std::string containing the XML introspection data
*/
const std::string GenerateIntrospection();
/**
* Update the D-Bus object path used when sending signals
*
* @param new_path
*/
void ModifyPath(const Object::Path &new_path) noexcept;
/**
* Add a signal recipient target. Empty string are allowed, which
* will be treated as a "wildcard". If all are empty, it will be
* considered broadcast.
*
* At least one target must be added.
*
* This method is a bit different from the one found in
* Signals::Emit::AddTarget(). This one being provided in Signals::Groups
* only takes the busname as the target; the object path and interface
* is already declared via the constructor.
*
* @param busname std::string of the recipient of a signal
*/
void AddTarget(const std::string &busname);
/**
* Sends the D-Bus signals with the provided parameters
*
* This calls @Emit::SendGVariant() with the same arguments once
* the signal name and the expected D-Bus data type signature has
* been validated.
*
* @param signal_name std::string of the D-Bus signal to send
* @param param GVariant glib2 object containing the signal
* data payload
*/
void SendGVariant(const std::string &signal_name, GVariant *param);
/**
* Create a separate distribution list for a group of signal targets
*
* @param groupname std::string with a name for the distribution list
*/
void GroupCreate(const std::string &groupname);
/**
* Deletes a specific distribution list
*
* @param groupname std::string with the distribution list to remove
*/
void GroupRemove(const std::string &groupname);
/**
* Adds a D-Bus bus name where signals will be sent for a specific
* distribution list
*
* @param groupname std::string with the group name to add the recipient
* @param busname std::String with the bus name of the recipient
*/
void GroupAddTarget(const std::string &groupname, const std::string &busname);
/**
* Similar to GroupAddTarget(), where it takes a std::vector of recipients
* to add to the group list.
*
* @param groupname std::string with the group name to add the recipient
* @param list std::vector<std::String> with bus names of the recipients
*/
void GroupAddTargetList(const std::string &groupname, const std::vector<std::string> &list);
/**
* Removes all the recipients in a distribution list
*
* @param groupname
*/
void GroupClearTargets(const std::string &groupname);
/**
* Create a new Signals::Signal object and tie it to the Signal::Emit
* target group used when sending the signal from this object.
*
* Note: The Signals::Signal constructor expects a Signal::Emit::Ptr
* as the first argument. This argument is handled automatically by
* this method. Because of this, the Signals::Signal implementation
* must implement the Signal::Emit::Ptr as the first argument in its
* implementation
*
* @tparam C Signal::Signal implementation class
* @tparam T Argument forward type
* @param groupname std::string with the group name to tie this signal
* object to
* @param args Arguments to the Signals::Signal implementation's
* constructor, without the Signal::Emit::Ptr argument
*
* @return std::shared_ptr<C> to the created Signals::Signal object
* implementation
*/
template <typename C, typename... T>
std::shared_ptr<C> GroupCreateSignal(const std::string &groupname, T &&...args)
{
auto sig = std::shared_ptr<C>(new C(get_group_emitter(groupname),
std::forward<T>(args)...));
RegisterSignal(sig->GetName(), sig->GetArguments());
return sig;
}
/**
* Sends a signal to all recipients in a single distribution list
*
* @param groupname std::string with the group name of recipients to signal
* @param signal_name std::string of the D-Bus signal to send
* @param param GVariant glib2 object containing the signal
* data payload
*/
void GroupSendGVariant(const std::string &groupname,
const std::string &signal_name,
GVariant *param);
protected:
/**
* Signals::Group contructor
*
* @param conn DBus::Connection object where signals will be sent
* @param object_path Object::Path of the D-Bus object the signal references
* @param object_interface std::string of the interface scope of the D-Bus object
*/
Group(DBus::Connection::Ptr conn,
const Object::Path &object_path,
const std::string &object_interface);
private:
/// D-Bus connection used to emit signals
DBus::Connection::Ptr connection{};
/**
* All signals registered via RegisterSignal ends up here. This is
* used to gather all the needed information for the D-Bus introspection
* generation
*/
std::map<std::string, SignalArgList> registered_signals;
/**
* This is a look-up cache of all the D-Bus type string used for a
* specific signal. This is created based on the data collected in
* registered_signals.
*/
std::map<std::string, std::string> type_cache;
/// D-Bus object path these signals are sent from from
Object::Path object_path;
/// D-Bus object interface these signals belongs to
const std::string object_interface;
/**
* Signal groups; each named group has their own Emit object with
* their own set of signal targets.
*
* The calls not using a group name explicitly uses the "__default__"
* group name. This group name is reserved and may not be used by
* any of the Group methods.
*/
std::map<std::string, Signals::Emit::Ptr> signal_groups{};
/**
* Private: Retrieve the Signals::Emit object for a specific signal
* target group
*
* @param groupname std::string with the groupname to lookup
* @param internal bool flag, the caller is an internal object caller,
* which may have access to the __default__ group.
*
* @return Signals::Emit::Ptr Pointer to the Emit object for this signal
* target group
*/
Signals::Emit::Ptr get_group_emitter(const std::string &groupname,
const bool internal = false) const;
};
} // namespace Signals
} // namespace DBus
|