summaryrefslogtreecommitdiffstats
path: root/example/calculator-server.cpp
blob: f128bf14e5258aea35fe5adbb6261e014d27c230 (plain)
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
#include <iostream>
#include <net/poettering/Calculator/error.hpp>
#include <net/poettering/Calculator/server.hpp>
#include <sdbusplus/server.hpp>

using Calculator_inherit =
    sdbusplus::server::object_t<sdbusplus::net::poettering::server::Calculator>;

/** Example implementation of net.poettering.Calculator */
struct Calculator : Calculator_inherit
{
    /** Constructor */
    Calculator(sdbusplus::bus::bus& bus, const char* path) :
        Calculator_inherit(bus, path)
    {
    }

    /** Multiply (x*y), update lastResult */
    int64_t multiply(int64_t x, int64_t y) override
    {
        return lastResult(x * y);
    }

    /** Divide (x/y), update lastResult
     *
     *  Throws DivisionByZero on error.
     */
    int64_t divide(int64_t x, int64_t y) override
    {
        using sdbusplus::net::poettering::Calculator::Error::DivisionByZero;
        if (y == 0)
        {
            status(State::Error);
            throw DivisionByZero();
        }

        return lastResult(x / y);
    }

    /** Clear lastResult, broadcast 'Cleared' signal */
    void clear() override
    {
        auto v = lastResult();
        lastResult(0);
        cleared(v);
        return;
    }
};

int main()
{
    // Define a dbus path location to place the object.
    constexpr auto path = "/net/poettering/calculator";

    // Create a new bus and affix an object manager for the subtree path we
    // intend to place objects at..
    auto b = sdbusplus::bus::new_default();
    sdbusplus::server::manager_t m{b, path};

    // Reserve the dbus service name : net.poettering.Calculator
    b.request_name("net.poettering.Calculator");

    // Create a calculator object at /net/poettering/calculator
    Calculator c1{b, path};

    // Handle dbus processing forever.
    while (1)
    {
        b.process_discard(); // discard any unhandled messages
        b.wait();
    }

    return 0;
}
OpenPOWER on IntegriCloud