File: bus-watcher.cpp

package info (click to toggle)
gdbuspp 3-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,116 kB
  • sloc: cpp: 9,462; python: 477; sh: 215; makefile: 5
file content (76 lines) | stat: -rw-r--r-- 1,933 bytes parent folder | download | duplicates (2)
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
//  GDBus++ - glib2 GDBus C++ wrapper
//
//  SPDX-License-Identifier: AGPL-3.0-only
//
//  Copyright (C)  OpenVPN Inc <sales@openvpn.net>
//  Copyright (C)  Răzvan Cojocaru <razvan.cojocaru@openvpn.com>
//

/**
 * @file   bus-watcher.cpp
 *
 * @brief  Trivial D-Bus bus watcher example.
 *
 */

#include <future>
#include <iostream>

#include "../gdbuspp/bus-watcher.hpp"
#include "../gdbuspp/mainloop.hpp"
#include "test-constants.hpp"


int main(int argc, char **argv)
{
    using namespace std::literals;

    try
    {
        auto loop = DBus::MainLoop::Create();

        const char busname[] = "net.openvpn.gdbuspp.test.simple";
        DBus::BusWatcher watcher(DBus::BusType::SESSION, busname);

        watcher.SetNameDisappearedHandler(
            [&](const std::string &bus_name)
            {
                std::cout << "Bus name disappeared: " << bus_name << std::endl;
                loop->Stop();
            });

        auto async_mainloop = std::async(std::launch::async,
                                         [&]
                                         {
                                             loop->Run();
                                         });

        std::cout << "Waiting for bus name " << busname
                  << " to appear ..." << std::endl;

        if (!watcher.WaitFor(10s))
        {
            loop->Stop();
            async_mainloop.get();

            std::cerr << "Timeout waiting for " << busname << " to appear!"
                      << std::endl;
            return 1;
        }

        std::cout << busname << " appeared!" << std::endl;

        async_mainloop.get();
    }
    catch (const DBus::Exception &excp)
    {
        std::cout << "EXCEPTION (DBus): " << excp.what() << std::endl;
        return 9;
    }
    catch (const std::exception &excp)
    {
        std::cout << "EXCEPTION: " << excp.what() << std::endl;
        return 8;
    }
    return 0;
}