File: obj-manager-server.cpp

package info (click to toggle)
sdbus-cpp 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,296 kB
  • sloc: cpp: 9,859; xml: 170; ansic: 135; makefile: 27
file content (108 lines) | stat: -rw-r--r-- 3,588 bytes parent folder | download
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
/**
 * Example of a D-Bus server which implements org.freedesktop.DBus.ObjectManager
 *
 * The example uses the generated stub API layer to register an object manager under "org.sdbuscpp.examplemanager"
 * and add objects underneath which implement "org.sdbuscpp.ExampleManager.Planet1".
 *
 * We add and remove objects after a few seconds and print info like this:
 * Creating PlanetAdaptor in 5 4 3 2 1
 * Creating PlanetAdaptor in 5 4 3 2 1
 * Creating PlanetAdaptor in 5 4 3 2 1
 * Removing PlanetAdaptor in 5 4 3 2 1
 * Removing PlanetAdaptor in 5 4 3 2 1
 */

#include "examplemanager-planet1-server-glue.h"
#include <sdbus-c++/sdbus-c++.h>
#include <iostream>
#include <memory>
#include <thread>
#include <chrono>

class ManagerAdaptor : public sdbus::AdaptorInterfaces< sdbus::ObjectManager_adaptor >
{
public:
    ManagerAdaptor(sdbus::IConnection& connection, std::string path)
    : AdaptorInterfaces(connection, std::move(path))
    {
        registerAdaptor();
    }

    ~ManagerAdaptor()
    {
        unregisterAdaptor();
    }
};

class PlanetAdaptor final : public sdbus::AdaptorInterfaces< org::sdbuscpp::ExampleManager::Planet1_adaptor,
                                                sdbus::ManagedObject_adaptor,
                                                sdbus::Properties_adaptor >
{
public:
    PlanetAdaptor(sdbus::IConnection& connection, std::string path, std::string name, uint64_t poulation)
    : AdaptorInterfaces(connection, std::move(path))
    , m_name(std::move(name))
    , m_population(poulation)
    {
        registerAdaptor();
        emitInterfacesAddedSignal({org::sdbuscpp::ExampleManager::Planet1_adaptor::INTERFACE_NAME});
    }

    ~PlanetAdaptor()
    {
        emitInterfacesRemovedSignal({org::sdbuscpp::ExampleManager::Planet1_adaptor::INTERFACE_NAME});
        unregisterAdaptor();
    }

    uint64_t GetPopulation() override
    {
        return m_population;
    }

    std::string Name() override
    {
        return m_name;
    }

private:
    std::string m_name;
    uint64_t m_population;
};

void printCountDown(const std::string& message, int seconds)
{
    std::cout << message << std::flush;
    for (int i = seconds; i > 0; i--) {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        std::cout << i << " " << std::flush;
    }
    std::cout << std::endl;
}

int main()
{
    auto connection = sdbus::createSessionBusConnection();
    connection->requestName("org.sdbuscpp.examplemanager");
    connection->enterEventLoopAsync();

    auto manager = std::make_unique<ManagerAdaptor>(*connection, "/org/sdbuscpp/examplemanager");
    while (true)
    {
        printCountDown("Creating PlanetAdaptor in ", 5);
        auto earth = std::make_unique<PlanetAdaptor>(*connection, "/org/sdbuscpp/examplemanager/Planet1/Earth", "Earth", 7'874'965'825);
        printCountDown("Creating PlanetAdaptor in ", 5);
        auto trantor = std::make_unique<PlanetAdaptor>(*connection, "/org/sdbuscpp/examplemanager/Planet1/Trantor", "Trantor", 40'000'000'000);
        printCountDown("Creating PlanetAdaptor in ", 5);
        auto laconia = std::make_unique<PlanetAdaptor>(*connection, "/org/sdbuscpp/examplemanager/Planet1/Laconia", "Laconia", 231'721);
        printCountDown("Removing PlanetAdaptor in ", 5);
        earth.reset();
        printCountDown("Removing PlanetAdaptor in ", 5);
        trantor.reset();
        printCountDown("Removing PlanetAdaptor in ", 5);
        laconia.reset();
    }

    connection->releaseName("org.sdbuscpp.examplemanager");
    connection->leaveEventLoop();
    return 0;
}