diff --git a/.gitignore b/.gitignore index 1e03b39..b4a92f2 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ subprojects/packagecache/ subprojects/yaml-cpp-0.8.0 subprojects/base64-0.5.2/ .venv/ +.worktrees/ diff --git a/include/server_api.h b/include/server_api.h index 2dde8e1..4aa7bd8 100644 --- a/include/server_api.h +++ b/include/server_api.h @@ -27,6 +27,7 @@ CRPC_EXPORT void crpc_str_destroy(rpc_string*); typedef rpc_string*(*callback_t)(rpc_string*); CRPC_EXPORT void crpc_init(const char* config_path); +CRPC_EXPORT void crpc_init_with_address(const char* ip, int port); CRPC_EXPORT void crpc_deinit(); CRPC_EXPORT void crpc_add_method(callback_t cb, rpc_string* name); diff --git a/src/meson.build b/src/meson.build index 0fdfb98..f372138 100644 --- a/src/meson.build +++ b/src/meson.build @@ -13,6 +13,7 @@ libcloud_point_rpc = shared_library( include_directories: inc, dependencies: [json_dep, thread_dep, glog_dep, yaml_dep, asio_dep, base64_dep], install: true, + install_rpath: '$ORIGIN', ) cloud_point_rpc_dep = declare_dependency( @@ -27,6 +28,7 @@ libcloud_point_rpc_test = shared_library( 'test_api.cpp', dependencies: cloud_point_rpc_dep, install: true, + install_rpath: '$ORIGIN', ) cloud_point_rpc_test_dep = declare_dependency( @@ -67,3 +69,11 @@ executable( link_args: '-pthread', install: true, ) + +# Minimal client example +executable( + 'minimal_client', + 'minimal_client.cpp', + dependencies: cloud_point_rpc_dep, + install: true, +) diff --git a/src/minimal_client.cpp b/src/minimal_client.cpp new file mode 100644 index 0000000..4855020 --- /dev/null +++ b/src/minimal_client.cpp @@ -0,0 +1,34 @@ +#include "cloud_point_rpc/config.hpp" +#include "cloud_point_rpc/tcp_connector.hpp" +#include +#include +#include + +int main(int argc, char *argv[]) { + google::InitGoogleLogging(argv[0]); + FLAGS_logtostderr = 1; + + std::string config_path = "config.yml"; + if (argc > 1) { + config_path = argv[1]; + } + + try { + auto config = score::ConfigLoader::load(config_path); + + score::TCPConnector connector(config.server.ip, + static_cast(config.server.port)); + + const std::string request = + R"({"jsonrpc":"2.0","method":"ping","params":{},"id":1})"; + + std::string response = connector.Send(request); + std::cout << response << std::endl; + + } catch (const std::exception &e) { + std::cerr << "Error: " << e.what() << std::endl; + return 1; + } + + return 0; +} diff --git a/src/server_api.cpp b/src/server_api.cpp index ff43d52..86eb5c5 100644 --- a/src/server_api.cpp +++ b/src/server_api.cpp @@ -76,6 +76,27 @@ void crpc_init(const char *config_path) { } } +void crpc_init_with_address(const char *ip, int port) { + if (!google::IsGoogleLoggingInitialized()) + google::InitGoogleLogging("CloudPointRPC"); + if (!ip) { + LOG(ERROR) << "ip was not provided"; + return; + } + try { + server = std::make_unique( + std::string(ip), static_cast(port), + [&](const std::string &request) { + std::lock_guard lock(server_mtx); + return rpc_server.process(request); + }); + server->start(); + LOG(INFO) << "Server started on " << ip << ":" << port; + } catch (const std::exception &e) { + LOG(ERROR) << "Fatal error: " << e.what(); + } +} + void crpc_deinit() { server.reset(); std::lock_guard lock(gc_mtx);