2021-10-14 15:54:44 +02:00
|
|
|
/**
|
|
|
|
|
* Example of a D-Bus client which implements org.freedesktop.DBus.ObjectManager
|
|
|
|
|
*
|
|
|
|
|
* The example uses the generated stub API layer to listen to interfaces added to new objects under
|
|
|
|
|
* "org.sdbuscpp.examplemanager". If added, we access "org.sdbuscpp.ExampleManager.Planet1" to print
|
|
|
|
|
* info like this:
|
|
|
|
|
* /org/sdbuscpp/examplemanager/Planet1/Earth added: org.sdbuscpp.ExampleManager.Planet1
|
|
|
|
|
* Earth has a population of 7874965825.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "examplemanager-planet1-client-glue.h"
|
|
|
|
|
#include <sdbus-c++/sdbus-c++.h>
|
|
|
|
|
#include <iostream>
|
2026-01-15 14:38:33 +01:00
|
|
|
#include <utility>
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <memory>
|
2021-10-14 15:54:44 +02:00
|
|
|
|
2021-10-15 13:44:00 +02:00
|
|
|
class PlanetProxy final : public sdbus::ProxyInterfaces< org::sdbuscpp::ExampleManager::Planet1_proxy >
|
2021-10-14 15:54:44 +02:00
|
|
|
{
|
|
|
|
|
public:
|
refactor: add strong types to public API (#414)
This introduces strong types for `std::string`-based D-Bus types. This facilitates safer, less error-prone and more expressive API.
What previously was `auto proxy = createProxy("org.sdbuscpp.concatenator", "/org/sdbuscpp/concatenator");` is now written like `auto proxy = createProxy(ServiceName{"org.sdbuscpp.concatenator"}, ObjectPath{"/org/sdbuscpp/concatenator"});`.
These types are:
* `ObjectPath` type for the object path (the type has been around already but now is also used consistently in sdbus-c++ API for object path strings),
* `InterfaceName` type for D-Bus interface names,
* `BusName` (and its aliases `ServiceName` and `ConnectionName`) type for bus/service/connection names,
* `MemberName` (and its aliases `MethodName`, `SignalName` and `PropertyName`) type for D-Bus method, signal and property names,
* `Signature` type for the D-Bus signature (the type has been around already but now is also used consistently in sdbus-c++ API for signature strings),
* `Error::Name` type for D-Bus error names.
2024-03-29 13:23:44 +01:00
|
|
|
PlanetProxy(sdbus::IConnection& connection, sdbus::ServiceName destination, sdbus::ObjectPath path)
|
2026-01-15 14:38:33 +01:00
|
|
|
: ProxyInterfaces(connection, std::move(destination), std::move(path))
|
2021-10-14 15:54:44 +02:00
|
|
|
{
|
|
|
|
|
registerProxy();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 14:38:33 +01:00
|
|
|
PlanetProxy(const PlanetProxy&) = delete;
|
|
|
|
|
PlanetProxy& operator=(const PlanetProxy&) = delete;
|
|
|
|
|
PlanetProxy(PlanetProxy&&) = delete;
|
|
|
|
|
PlanetProxy& operator=(PlanetProxy&&) = delete;
|
|
|
|
|
|
2021-10-14 15:54:44 +02:00
|
|
|
~PlanetProxy()
|
|
|
|
|
{
|
|
|
|
|
unregisterProxy();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
refactor: add strong types to public API (#414)
This introduces strong types for `std::string`-based D-Bus types. This facilitates safer, less error-prone and more expressive API.
What previously was `auto proxy = createProxy("org.sdbuscpp.concatenator", "/org/sdbuscpp/concatenator");` is now written like `auto proxy = createProxy(ServiceName{"org.sdbuscpp.concatenator"}, ObjectPath{"/org/sdbuscpp/concatenator"});`.
These types are:
* `ObjectPath` type for the object path (the type has been around already but now is also used consistently in sdbus-c++ API for object path strings),
* `InterfaceName` type for D-Bus interface names,
* `BusName` (and its aliases `ServiceName` and `ConnectionName`) type for bus/service/connection names,
* `MemberName` (and its aliases `MethodName`, `SignalName` and `PropertyName`) type for D-Bus method, signal and property names,
* `Signature` type for the D-Bus signature (the type has been around already but now is also used consistently in sdbus-c++ API for signature strings),
* `Error::Name` type for D-Bus error names.
2024-03-29 13:23:44 +01:00
|
|
|
class ManagerProxy final : public sdbus::ProxyInterfaces<sdbus::ObjectManager_proxy>
|
2021-10-14 15:54:44 +02:00
|
|
|
{
|
|
|
|
|
public:
|
refactor: add strong types to public API (#414)
This introduces strong types for `std::string`-based D-Bus types. This facilitates safer, less error-prone and more expressive API.
What previously was `auto proxy = createProxy("org.sdbuscpp.concatenator", "/org/sdbuscpp/concatenator");` is now written like `auto proxy = createProxy(ServiceName{"org.sdbuscpp.concatenator"}, ObjectPath{"/org/sdbuscpp/concatenator"});`.
These types are:
* `ObjectPath` type for the object path (the type has been around already but now is also used consistently in sdbus-c++ API for object path strings),
* `InterfaceName` type for D-Bus interface names,
* `BusName` (and its aliases `ServiceName` and `ConnectionName`) type for bus/service/connection names,
* `MemberName` (and its aliases `MethodName`, `SignalName` and `PropertyName`) type for D-Bus method, signal and property names,
* `Signature` type for the D-Bus signature (the type has been around already but now is also used consistently in sdbus-c++ API for signature strings),
* `Error::Name` type for D-Bus error names.
2024-03-29 13:23:44 +01:00
|
|
|
ManagerProxy(sdbus::IConnection& connection, sdbus::ServiceName destination, sdbus::ObjectPath path)
|
|
|
|
|
: ProxyInterfaces(connection, destination, std::move(path))
|
|
|
|
|
, m_connection(connection)
|
2026-01-15 14:38:33 +01:00
|
|
|
, m_destination(std::move(destination))
|
2021-10-14 15:54:44 +02:00
|
|
|
{
|
|
|
|
|
registerProxy();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 14:38:33 +01:00
|
|
|
ManagerProxy(const ManagerProxy&) = delete;
|
|
|
|
|
ManagerProxy& operator=(const ManagerProxy&) = delete;
|
|
|
|
|
ManagerProxy(ManagerProxy&&) = delete;
|
|
|
|
|
ManagerProxy& operator=(ManagerProxy&&) = delete;
|
|
|
|
|
|
2021-10-14 15:54:44 +02:00
|
|
|
~ManagerProxy()
|
|
|
|
|
{
|
|
|
|
|
unregisterProxy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void handleExistingObjects()
|
|
|
|
|
{
|
refactor: add strong types to public API (#414)
This introduces strong types for `std::string`-based D-Bus types. This facilitates safer, less error-prone and more expressive API.
What previously was `auto proxy = createProxy("org.sdbuscpp.concatenator", "/org/sdbuscpp/concatenator");` is now written like `auto proxy = createProxy(ServiceName{"org.sdbuscpp.concatenator"}, ObjectPath{"/org/sdbuscpp/concatenator"});`.
These types are:
* `ObjectPath` type for the object path (the type has been around already but now is also used consistently in sdbus-c++ API for object path strings),
* `InterfaceName` type for D-Bus interface names,
* `BusName` (and its aliases `ServiceName` and `ConnectionName`) type for bus/service/connection names,
* `MemberName` (and its aliases `MethodName`, `SignalName` and `PropertyName`) type for D-Bus method, signal and property names,
* `Signature` type for the D-Bus signature (the type has been around already but now is also used consistently in sdbus-c++ API for signature strings),
* `Error::Name` type for D-Bus error names.
2024-03-29 13:23:44 +01:00
|
|
|
auto objectsInterfacesAndProperties = GetManagedObjects();
|
2021-10-14 15:54:44 +02:00
|
|
|
for (const auto& [object, interfacesAndProperties] : objectsInterfacesAndProperties) {
|
|
|
|
|
onInterfacesAdded(object, interfacesAndProperties);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void onInterfacesAdded( const sdbus::ObjectPath& objectPath
|
refactor: add strong types to public API (#414)
This introduces strong types for `std::string`-based D-Bus types. This facilitates safer, less error-prone and more expressive API.
What previously was `auto proxy = createProxy("org.sdbuscpp.concatenator", "/org/sdbuscpp/concatenator");` is now written like `auto proxy = createProxy(ServiceName{"org.sdbuscpp.concatenator"}, ObjectPath{"/org/sdbuscpp/concatenator"});`.
These types are:
* `ObjectPath` type for the object path (the type has been around already but now is also used consistently in sdbus-c++ API for object path strings),
* `InterfaceName` type for D-Bus interface names,
* `BusName` (and its aliases `ServiceName` and `ConnectionName`) type for bus/service/connection names,
* `MemberName` (and its aliases `MethodName`, `SignalName` and `PropertyName`) type for D-Bus method, signal and property names,
* `Signature` type for the D-Bus signature (the type has been around already but now is also used consistently in sdbus-c++ API for signature strings),
* `Error::Name` type for D-Bus error names.
2024-03-29 13:23:44 +01:00
|
|
|
, const std::map<sdbus::InterfaceName, std::map<sdbus::PropertyName, sdbus::Variant>>& interfacesAndProperties) override
|
2021-10-14 15:54:44 +02:00
|
|
|
{
|
|
|
|
|
std::cout << objectPath << " added:\t";
|
|
|
|
|
for (const auto& [interface, _] : interfacesAndProperties) {
|
|
|
|
|
std::cout << interface << " ";
|
|
|
|
|
}
|
2026-01-15 14:38:33 +01:00
|
|
|
std::cout << '\n';
|
2021-10-14 15:54:44 +02:00
|
|
|
|
|
|
|
|
// Parse and print some more info
|
2024-04-16 22:28:42 +02:00
|
|
|
auto planetInterface = interfacesAndProperties.find(sdbus::InterfaceName{org::sdbuscpp::ExampleManager::Planet1_proxy::INTERFACE_NAME});
|
2021-10-14 15:54:44 +02:00
|
|
|
if (planetInterface == interfacesAndProperties.end()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const auto& properties = planetInterface->second;
|
|
|
|
|
// get a property which was passed as part of the signal.
|
refactor: add strong types to public API (#414)
This introduces strong types for `std::string`-based D-Bus types. This facilitates safer, less error-prone and more expressive API.
What previously was `auto proxy = createProxy("org.sdbuscpp.concatenator", "/org/sdbuscpp/concatenator");` is now written like `auto proxy = createProxy(ServiceName{"org.sdbuscpp.concatenator"}, ObjectPath{"/org/sdbuscpp/concatenator"});`.
These types are:
* `ObjectPath` type for the object path (the type has been around already but now is also used consistently in sdbus-c++ API for object path strings),
* `InterfaceName` type for D-Bus interface names,
* `BusName` (and its aliases `ServiceName` and `ConnectionName`) type for bus/service/connection names,
* `MemberName` (and its aliases `MethodName`, `SignalName` and `PropertyName`) type for D-Bus method, signal and property names,
* `Signature` type for the D-Bus signature (the type has been around already but now is also used consistently in sdbus-c++ API for signature strings),
* `Error::Name` type for D-Bus error names.
2024-03-29 13:23:44 +01:00
|
|
|
const auto& name = properties.at(sdbus::PropertyName{"Name"}).get<std::string>();
|
2021-10-14 15:54:44 +02:00
|
|
|
// or create a proxy instance to the newly added object.
|
|
|
|
|
PlanetProxy planet(m_connection, m_destination, objectPath);
|
2026-01-15 14:38:33 +01:00
|
|
|
std::cout << name << " has a population of " << planet.GetPopulation() << ".\n" << '\n';
|
2021-10-14 15:54:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onInterfacesRemoved( const sdbus::ObjectPath& objectPath
|
refactor: add strong types to public API (#414)
This introduces strong types for `std::string`-based D-Bus types. This facilitates safer, less error-prone and more expressive API.
What previously was `auto proxy = createProxy("org.sdbuscpp.concatenator", "/org/sdbuscpp/concatenator");` is now written like `auto proxy = createProxy(ServiceName{"org.sdbuscpp.concatenator"}, ObjectPath{"/org/sdbuscpp/concatenator"});`.
These types are:
* `ObjectPath` type for the object path (the type has been around already but now is also used consistently in sdbus-c++ API for object path strings),
* `InterfaceName` type for D-Bus interface names,
* `BusName` (and its aliases `ServiceName` and `ConnectionName`) type for bus/service/connection names,
* `MemberName` (and its aliases `MethodName`, `SignalName` and `PropertyName`) type for D-Bus method, signal and property names,
* `Signature` type for the D-Bus signature (the type has been around already but now is also used consistently in sdbus-c++ API for signature strings),
* `Error::Name` type for D-Bus error names.
2024-03-29 13:23:44 +01:00
|
|
|
, const std::vector<sdbus::InterfaceName>& interfaces) override
|
2021-10-14 15:54:44 +02:00
|
|
|
{
|
|
|
|
|
std::cout << objectPath << " removed:\t";
|
|
|
|
|
for (const auto& interface : interfaces) {
|
|
|
|
|
std::cout << interface << " ";
|
|
|
|
|
}
|
2026-01-15 14:38:33 +01:00
|
|
|
std::cout << '\n';
|
2021-10-14 15:54:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sdbus::IConnection& m_connection;
|
refactor: add strong types to public API (#414)
This introduces strong types for `std::string`-based D-Bus types. This facilitates safer, less error-prone and more expressive API.
What previously was `auto proxy = createProxy("org.sdbuscpp.concatenator", "/org/sdbuscpp/concatenator");` is now written like `auto proxy = createProxy(ServiceName{"org.sdbuscpp.concatenator"}, ObjectPath{"/org/sdbuscpp/concatenator"});`.
These types are:
* `ObjectPath` type for the object path (the type has been around already but now is also used consistently in sdbus-c++ API for object path strings),
* `InterfaceName` type for D-Bus interface names,
* `BusName` (and its aliases `ServiceName` and `ConnectionName`) type for bus/service/connection names,
* `MemberName` (and its aliases `MethodName`, `SignalName` and `PropertyName`) type for D-Bus method, signal and property names,
* `Signature` type for the D-Bus signature (the type has been around already but now is also used consistently in sdbus-c++ API for signature strings),
* `Error::Name` type for D-Bus error names.
2024-03-29 13:23:44 +01:00
|
|
|
sdbus::ServiceName m_destination;
|
2021-10-14 15:54:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
auto connection = sdbus::createSessionBusConnection();
|
|
|
|
|
|
refactor: add strong types to public API (#414)
This introduces strong types for `std::string`-based D-Bus types. This facilitates safer, less error-prone and more expressive API.
What previously was `auto proxy = createProxy("org.sdbuscpp.concatenator", "/org/sdbuscpp/concatenator");` is now written like `auto proxy = createProxy(ServiceName{"org.sdbuscpp.concatenator"}, ObjectPath{"/org/sdbuscpp/concatenator"});`.
These types are:
* `ObjectPath` type for the object path (the type has been around already but now is also used consistently in sdbus-c++ API for object path strings),
* `InterfaceName` type for D-Bus interface names,
* `BusName` (and its aliases `ServiceName` and `ConnectionName`) type for bus/service/connection names,
* `MemberName` (and its aliases `MethodName`, `SignalName` and `PropertyName`) type for D-Bus method, signal and property names,
* `Signature` type for the D-Bus signature (the type has been around already but now is also used consistently in sdbus-c++ API for signature strings),
* `Error::Name` type for D-Bus error names.
2024-03-29 13:23:44 +01:00
|
|
|
sdbus::ServiceName destination{"org.sdbuscpp.examplemanager"};
|
|
|
|
|
sdbus::ObjectPath objectPath{"/org/sdbuscpp/examplemanager"};
|
|
|
|
|
auto managerProxy = std::make_unique<ManagerProxy>(*connection, std::move(destination), std::move(objectPath));
|
2021-10-14 15:54:44 +02:00
|
|
|
try {
|
|
|
|
|
managerProxy->handleExistingObjects();
|
|
|
|
|
}
|
|
|
|
|
catch (const sdbus::Error& e) {
|
|
|
|
|
if (e.getName() == "org.freedesktop.DBus.Error.ServiceUnknown") {
|
2026-01-15 14:38:33 +01:00
|
|
|
std::cout << "Waiting for server to start ..." << '\n';
|
2021-10-14 15:54:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connection->enterEventLoop();
|
|
|
|
|
return 0;
|
refactor: add strong types to public API (#414)
This introduces strong types for `std::string`-based D-Bus types. This facilitates safer, less error-prone and more expressive API.
What previously was `auto proxy = createProxy("org.sdbuscpp.concatenator", "/org/sdbuscpp/concatenator");` is now written like `auto proxy = createProxy(ServiceName{"org.sdbuscpp.concatenator"}, ObjectPath{"/org/sdbuscpp/concatenator"});`.
These types are:
* `ObjectPath` type for the object path (the type has been around already but now is also used consistently in sdbus-c++ API for object path strings),
* `InterfaceName` type for D-Bus interface names,
* `BusName` (and its aliases `ServiceName` and `ConnectionName`) type for bus/service/connection names,
* `MemberName` (and its aliases `MethodName`, `SignalName` and `PropertyName`) type for D-Bus method, signal and property names,
* `Signature` type for the D-Bus signature (the type has been around already but now is also used consistently in sdbus-c++ API for signature strings),
* `Error::Name` type for D-Bus error names.
2024-03-29 13:23:44 +01:00
|
|
|
}
|