-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdbusplus-server-example-asio.cpp
42 lines (35 loc) · 1.06 KB
/
sdbusplus-server-example-asio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <cstdlib>
#include <iostream>
#include <sdbusplus/asio/connection.hpp>
#include <sdbusplus/asio/object_server.hpp>
#include <string>
int exampleMethodCallback(const std::string& arg)
{
std::cout << ">> com.example.sdbusplus.asio.ExampleMethod: " << arg << "\n";
int converted = 0;
try
{
converted = std::stoi(arg);
}
catch (std::invalid_argument&)
{
throw sdbusplus::exception::SdBusError(-EINVAL,
"prefix");
}
std::cout << "<< com.example.sdbusplus.asio.ExampleMethod: "
<< converted << "\n";
return converted;
}
int main(int argc, char* argv[])
{
boost::asio::io_context io;
auto bus = std::make_shared<sdbusplus::asio::connection>(io);
sdbusplus::asio::object_server server(bus);
auto iface = server.add_interface(
"/com/example/sdbusplus/asio", "com.example.sdbusplus.asio");
iface->register_method("ExampleMethod", exampleMethodCallback);
iface->initialize();
bus->request_name("com.example.sdbusplus.asio");
io.run();
exit(EXIT_SUCCESS);
}