[rpc] new methods in api

:Release Notes:
added new implicit rpc method:
"get-available-methods"

get_count
get_method_name_by_id
get_method_names

:Detailed Notes:
-

:Testing Performed:
weak coverage

:QA Notes:
-

:Issues Addressed:
TG-4 #done
This commit is contained in:
Artur Mukhamadiev 2026-03-19 20:57:52 +03:00
parent e3b5be0f66
commit 4343dd13e7
5 changed files with 77 additions and 21 deletions

View File

@ -23,12 +23,22 @@ class CRPC_EXPORT RpcServer {
using Handler = std::function<nlohmann::json(const nlohmann::json &)>;
using callback_t = rpc_string *(*)(rpc_string *);
public:
/// @note +1 method implicitly added: get-available-methods
RpcServer();
void register_method(const std::string &name, Handler handler);
void register_method(const std::string &name, callback_t handler);
uint64_t get_count() noexcept;
std::span<std::string_view> get_method_names() noexcept;
std::string_view get_method_name_by_id(uint64_t id) noexcept;
///@param request_str json rpc 2.0 formatted string
[[nodiscard]] std::string process(const std::string &request_str);
private:
std::vector<std::string_view> handler_names_;
std::map<std::string, Handler> handlers_;
};

View File

@ -29,6 +29,8 @@ typedef rpc_string*(*callback_t)(rpc_string*);
CRPC_EXPORT void crpc_init(const char* config_path);
CRPC_EXPORT void crpc_deinit();
CRPC_EXPORT rpc_string* crpc_get_method_name_by_id(uint64_t id);
CRPC_EXPORT uint64_t crpc_get_methods_count();
CRPC_EXPORT void crpc_add_method(callback_t cb, rpc_string* name);
#ifdef __cplusplus

View File

@ -1,4 +1,5 @@
#include "cloud_point_rpc/rpc_server.hpp"
#include <cstdint>
#include <glog/logging.h>
using json = nlohmann::json;
@ -17,8 +18,15 @@ json create_success(const json &result, const json &id) {
}
} // namespace
RpcServer::RpcServer() {
register_method("get-available-methods", [&](const json&) {
return get_method_names();
});
}
void RpcServer::register_method(const std::string &name, Handler handler) {
handlers_[name] = std::move(handler);
handler_names_.push_back(handlers_.find(name)->first);
}
void RpcServer::register_method(const std::string &name, callback_t handler) {
@ -28,6 +36,23 @@ void RpcServer::register_method(const std::string &name, callback_t handler) {
rpc_string *res = handler(&tmp);
return {res->s};
};
handler_names_.push_back(handlers_.find(name)->first);
}
std::span<std::string_view> RpcServer::get_method_names() noexcept {
return this->handler_names_;
}
uint64_t RpcServer::get_count() noexcept {
return this->handler_names_.size();
}
std::string_view RpcServer::get_method_name_by_id(uint64_t id) noexcept {
if(id >= handler_names_.size()) {
LOG(ERROR) << __func__ << std::format(": called with id = {} which is bigger, than size={}", id, handler_names_.size());
return {};
}
return handler_names_.at(id);
}
std::string RpcServer::process(const std::string &request_str) {

View File

@ -1,12 +1,12 @@
#include "server_api.h"
#include "cloud_point_rpc/config.hpp"
#include "cloud_point_rpc/rpc_server.hpp"
#include "cloud_point_rpc/tcp_server.hpp"
#include <glog/logging.h>
#include "server_api.h"
#include <algorithm>
#include <glog/logging.h>
#include <list>
#include <memory>
#include <string>
#include <list>
static std::list<std::unique_ptr<rpc_string>> gc;
score::RpcServer rpc_server;
@ -18,9 +18,7 @@ const char* crpc_str_get_data(const rpc_string* that) {
return that->s.c_str();
}
uint64_t crpc_str_get_size(const rpc_string* that){
return that->s.size();
}
uint64_t crpc_str_get_size(const rpc_string *that) { return that->s.size(); }
rpc_string *crpc_str_create(const char *data, uint64_t size) {
gc.push_back(std::make_unique<rpc_string>(data, size));
@ -33,7 +31,6 @@ void crpc_str_destroy(rpc_string* that){
gc.erase(it);
}
void crpc_init(const char *config_path) {
google::InitGoogleLogging("CloudPointRPC");
if (config_path == nullptr) {
@ -43,10 +40,10 @@ void crpc_init(const char* config_path) {
auto config = score::ConfigLoader::load(config_path);
LOG(INFO) << "Loaded config from " << config_path;
server = std::make_unique<score::TcpServer>(config.server.ip, config.server.port,
server = std::make_unique<score::TcpServer>(
config.server.ip, config.server.port,
[&](const std::string &request) {
return rpc_server.process(
request);
return rpc_server.process(request);
});
server->start();
} catch (const std::exception &e) {
@ -64,5 +61,14 @@ void crpc_deinit() {
void crpc_add_method(callback_t cb, rpc_string *name) {
rpc_server.register_method(name->s, cb);
}
rpc_string *crpc_get_method_name_by_id(uint64_t id) {
auto value = rpc_server.get_method_name_by_id(id);
return crpc_str_create(value.data(), value.size());
}
uint64_t crpc_get_methods_count() {
return rpc_server.get_count();
}
}

View File

@ -61,3 +61,16 @@ TEST_F(RpcServerTest, InvalidJsonReturnsParseError) {
ASSERT_TRUE(response.contains("error"));
EXPECT_EQ(response["error"]["code"], -32700);
}
TEST_F(RpcServerTest, GetMethod) {
EXPECT_EQ(server.get_count(), 2);
EXPECT_EQ(server.get_method_name_by_id(1), "get-intrinsic-params");
EXPECT_EQ(server.get_method_names()[1], "get-intrinsic-params");
server.register_method("get-test-2", [&](const json&){
return "test";
});
EXPECT_EQ(server.get_count(), 3);
EXPECT_EQ(server.get_method_name_by_id(2), "get-test-2");
EXPECT_EQ(server.get_method_names()[2], "get-test-2");
}