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