1 #include <net/poettering/Calculator/client.hpp>
2 #include <net/poettering/Calculator/server.hpp>
3 #include <sdbusplus/server.hpp>
4 
5 #include <iostream>
6 #include <string_view>
7 
8 using Calculator_inherit =
9     sdbusplus::server::object_t<sdbusplus::server::net::poettering::Calculator>;
10 
11 /** Example implementation of net.poettering.Calculator */
12 struct Calculator : Calculator_inherit
13 {
14     /** Constructor */
CalculatorCalculator15     Calculator(sdbusplus::bus_t& bus, const char* path) :
16         Calculator_inherit(bus, path)
17     {}
18 
19     /** Multiply (x*y), update lastResult */
multiplyCalculator20     int64_t multiply(int64_t x, int64_t y) override
21     {
22         return lastResult(x * y);
23     }
24 
25     /** Divide (x/y), update lastResult
26      *
27      *  Throws DivisionByZero on error.
28      */
divideCalculator29     int64_t divide(int64_t x, int64_t y) override
30     {
31         using sdbusplus::error::net::poettering::Calculator::DivisionByZero;
32         if (y == 0)
33         {
34             status(State::Error);
35             throw DivisionByZero();
36         }
37 
38         return lastResult(x / y);
39     }
40 
41     /** Clear lastResult, broadcast 'Cleared' signal */
clearCalculator42     void clear() override
43     {
44         auto v = lastResult();
45         lastResult(0);
46         cleared(v);
47         return;
48     }
49 };
50 
main()51 int main()
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, Calculator::instance_path};
57 
58     // Reserve the dbus service name : net.poettering.Calculator
59     b.request_name(Calculator::default_service);
60 
61     // Create a calculator object at /net/poettering/calculator
62     Calculator c1{b, Calculator::instance_path};
63 
64     // Handle dbus processing forever.
65     b.process_loop();
66 }
67