2023-01-10 15:58:26 +00:00
|
|
|
#pragma once
|
|
|
|
|
2023-10-24 13:18:03 +00:00
|
|
|
#include "json_bt.hpp"
|
2023-01-10 15:58:26 +00:00
|
|
|
#include "rpc_request_decorators.hpp"
|
|
|
|
#include "rpc_request_definitions.hpp"
|
2023-10-24 13:18:03 +00:00
|
|
|
#include "rpc_request_parser.hpp"
|
|
|
|
#include "rpc_server.hpp"
|
|
|
|
|
2023-01-10 15:58:26 +00:00
|
|
|
#include <llarp/config/config.hpp>
|
2023-09-15 14:55:32 +00:00
|
|
|
#include <llarp/router/router.hpp>
|
2023-10-24 13:18:03 +00:00
|
|
|
|
2023-01-10 15:58:26 +00:00
|
|
|
#include <oxen/log/omq_logger.hpp>
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <oxenmq/address.h>
|
|
|
|
#include <oxenmq/oxenmq.h>
|
|
|
|
|
|
|
|
#include <string_view>
|
2023-01-10 15:58:26 +00:00
|
|
|
|
|
|
|
namespace llarp::rpc
|
|
|
|
{
|
|
|
|
using nlohmann::json;
|
|
|
|
|
|
|
|
template <typename RPC>
|
|
|
|
auto
|
|
|
|
make_invoke()
|
|
|
|
{
|
|
|
|
return [](oxenmq::Message& m, RPCServer& server) {
|
|
|
|
EndpointHandler<RPC> handler{server, m.send_later()};
|
|
|
|
auto& rpc = handler.rpc;
|
|
|
|
|
|
|
|
if (m.data.size() > 1)
|
2023-01-27 23:08:43 +00:00
|
|
|
{
|
|
|
|
m.send_reply(nlohmann::json{
|
|
|
|
{"error",
|
|
|
|
"Bad Request: RPC requests must have at most one data part (received {})"_format(
|
|
|
|
m.data.size())}}
|
|
|
|
.dump());
|
|
|
|
return;
|
|
|
|
}
|
2023-01-10 15:58:26 +00:00
|
|
|
// parsing input as bt or json
|
|
|
|
// hand off to parse_request (overloaded versions)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (m.data.empty() or m.data[0].empty())
|
|
|
|
{
|
|
|
|
parse_request(rpc, nlohmann::json::object());
|
|
|
|
}
|
|
|
|
else if (m.data[0].front() == 'd')
|
|
|
|
{
|
|
|
|
rpc.set_bt();
|
|
|
|
parse_request(rpc, oxenc::bt_dict_consumer{m.data[0]});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parse_request(rpc, nlohmann::json::parse(m.data[0]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
2023-01-27 23:08:43 +00:00
|
|
|
m.send_reply(nlohmann::json{{"Failed to parse request parameters: "s + e.what()}}.dump());
|
2023-01-10 15:58:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not std::is_base_of_v<Immediate, RPC>)
|
|
|
|
{
|
|
|
|
server.m_Router.loop()->call_soon(std::move(handler));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
handler();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp::rpc
|