5#include <xmlrpc-c/registry.hpp>
6#include <xmlrpc-c/server_abyss.hpp>
15class RpcMethodHdlSs :
public xmlrpc_c::method
18 explicit RpcMethodHdlSs(std::function<std::optional<std::string>(
const std::string&)>& handler)
24 void execute(
const xmlrpc_c::paramList& params, xmlrpc_c::value*
const retval)
override
27 assert(params.size() == 1);
28 const std::string arg{params.getString(0)};
30 const std::optional<std::string> res = handler(arg);
32 if (res.has_value()) {
33 *retval = xmlrpc_c::value_string(res.value());
35 *retval = xmlrpc_c::value_nil();
40 std::function<std::optional<std::string>(
const std::string&)> handler;
58 :
rpc_server(xmlrpc_c::serverAbyss::constrOpt()
60 .portNumber(listen_port)
61 .serverOwnsSignals(false))
67 :
rpc_server(xmlrpc_c::serverAbyss::constrOpt()
102 if (priv->has_started) {
106 assert(!priv->rpc_server_thread);
107 priv->rpc_server_thread = std::make_unique<std::thread>([
this]() { priv->rpc_server.run(); });
108 priv->has_started =
true;
113 if (!priv->has_started || !priv->rpc_server_thread) {
117 priv->rpc_server.terminate();
121 std::string_view name,
122 std::function<std::optional<std::string>(
const std::string&)>
125 priv->rpc_registry.addMethod(std::string{name},
126 static_cast<xmlrpc_c::method*
>(
new RpcMethodHdlSs{handler}));
XmlRpcServer(uint16_t listen_port)
Create an XML-RPC server which listens on all interfaces on the given port.
void register_method_handler(std::string_view name, std::function< std::optional< std::string >(const std::string &)> handler)
Register a method handler.
void stop()
Stop the XML-RPC server.
void start()
Start the XML-RPC server.
Private implementation of XmlRpcServer.
XmlRpcServerPrivate(uint16_t listen_port)
Constructor for private XmlRpcServer implementation.
XmlRpcServerPrivate(const struct sockaddr *sa, socklen_t sa_len)
Cosntructor for private XmlRpcServer implementation.
xmlrpc_c::serverAbyss rpc_server
std::unique_ptr< std::thread > rpc_server_thread
xmlrpc_c::registry rpc_registry
XmlRpcServerPrivate(const XmlRpcServerPrivate &)=delete