Patrick Williams | 4edb785 | 2017-07-03 11:50:11 -0500 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <sdbusplus/server.hpp> |
| 3 | #include <net/poettering/Calculator/server.hpp> |
| 4 | #include <net/poettering/Calculator/error.hpp> |
| 5 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 6 | using Calculator_inherit = |
| 7 | sdbusplus::server::object_t<sdbusplus::net::poettering::server::Calculator>; |
Patrick Williams | 4edb785 | 2017-07-03 11:50:11 -0500 | [diff] [blame] | 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) : |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 14 | Calculator_inherit(bus, path) |
| 15 | { |
| 16 | } |
Patrick Williams | 4edb785 | 2017-07-03 11:50:11 -0500 | [diff] [blame] | 17 | |
| 18 | /** Multiply (x*y), update lastResult */ |
| 19 | int64_t multiply(int64_t x, int64_t y) override |
| 20 | { |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 21 | return lastResult(x * y); |
Patrick Williams | 4edb785 | 2017-07-03 11:50:11 -0500 | [diff] [blame] | 22 | } |
| 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 Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 37 | return lastResult(x / y); |
Patrick Williams | 4edb785 | 2017-07-03 11:50:11 -0500 | [diff] [blame] | 38 | } |
| 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 | |
| 50 | int 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 Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 67 | while (1) |
Patrick Williams | 4edb785 | 2017-07-03 11:50:11 -0500 | [diff] [blame] | 68 | { |
| 69 | b.process_discard(); // discard any unhandled messages |
| 70 | b.wait(); |
| 71 | } |
| 72 | |
| 73 | return 0; |
| 74 | } |