1 #include <iostream>
2 #include <sdbusplus/server.hpp>
3 #include <net/poettering/Calculator/server.hpp>
4 #include <net/poettering/Calculator/error.hpp>
5 
6 using Calculator_inherit = sdbusplus::server::object_t<
7         sdbusplus::net::poettering::server::Calculator>;
8 
9 /** Example implementation of net.poettering.Calculator */
10 struct Calculator : Calculator_inherit
11 {
12     /** Constructor */
13     Calculator(sdbusplus::bus::bus& bus, const char* path) :
14         Calculator_inherit(bus, path) { }
15 
16     /** Multiply (x*y), update lastResult */
17     int64_t multiply(int64_t x, int64_t y) override
18     {
19         return lastResult(x*y);
20     }
21 
22     /** Divide (x/y), update lastResult
23      *
24      *  Throws DivisionByZero on error.
25      */
26     int64_t divide(int64_t x, int64_t y) override
27     {
28         using sdbusplus::net::poettering::Calculator::Error::DivisionByZero;
29         if (y == 0)
30         {
31             status(State::Error);
32             throw DivisionByZero();
33         }
34 
35         return lastResult(x/y);
36     }
37 
38     /** Clear lastResult, broadcast 'Cleared' signal */
39     void clear() override
40     {
41         auto v = lastResult();
42         lastResult(0);
43         cleared(v);
44         return;
45     }
46 };
47 
48 int main()
49 {
50     // Define a dbus path location to place the object.
51     constexpr auto path = "/net/poettering/calculator";
52 
53     // Create a new bus and affix an object manager for the subtree path we
54     // intend to place objects at..
55     auto b = sdbusplus::bus::new_default();
56     sdbusplus::server::manager_t m{b, path};
57 
58     // Reserve the dbus service name : net.poettering.Calculator
59     b.request_name("net.poettering.Calculator");
60 
61     // Create a calculator object at /net/poettering/calculator
62     Calculator c1{b, path};
63 
64     // Handle dbus processing forever.
65     while(1)
66     {
67         b.process_discard(); // discard any unhandled messages
68         b.wait();
69     }
70 
71     return 0;
72 }
73