| #include <net/poettering/Calculator/client.hpp> |
| #include <net/poettering/Calculator/error.hpp> |
| #include <net/poettering/Calculator/server.hpp> |
| #include <sdbusplus/server.hpp> |
| |
| #include <iostream> |
| #include <string_view> |
| |
| using Calculator_inherit = |
| sdbusplus::server::object_t<sdbusplus::server::net::poettering::Calculator>; |
| |
| /** Example implementation of net.poettering.Calculator */ |
| struct Calculator : Calculator_inherit |
| { |
| /** Constructor */ |
| Calculator(sdbusplus::bus_t& bus, const char* path) : |
| Calculator_inherit(bus, path) |
| {} |
| |
| /** Multiply (x*y), update lastResult */ |
| int64_t multiply(int64_t x, int64_t y) override |
| { |
| return lastResult(x * y); |
| } |
| |
| /** Divide (x/y), update lastResult |
| * |
| * Throws DivisionByZero on error. |
| */ |
| int64_t divide(int64_t x, int64_t y) override |
| { |
| using sdbusplus::net::poettering::Calculator::Error::DivisionByZero; |
| if (y == 0) |
| { |
| status(State::Error); |
| throw DivisionByZero(); |
| } |
| |
| return lastResult(x / y); |
| } |
| |
| /** Clear lastResult, broadcast 'Cleared' signal */ |
| void clear() override |
| { |
| auto v = lastResult(); |
| lastResult(0); |
| cleared(v); |
| return; |
| } |
| }; |
| |
| int main() |
| { |
| // Define a dbus path location to place the object. |
| constexpr auto path = "/net/poettering/calculator"; |
| |
| static_assert( |
| std::string_view( |
| sdbusplus::common::net::poettering::Calculator::interface) == |
| std::string_view(Calculator::interface)); |
| |
| // Create a new bus and affix an object manager for the subtree path we |
| // intend to place objects at.. |
| auto b = sdbusplus::bus::new_default(); |
| sdbusplus::server::manager_t m{b, path}; |
| |
| // Reserve the dbus service name : net.poettering.Calculator |
| b.request_name("net.poettering.Calculator"); |
| |
| // Create a calculator object at /net/poettering/calculator |
| Calculator c1{b, path}; |
| |
| // Handle dbus processing forever. |
| b.process_loop(); |
| } |