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