From 858860b9aefd10746671af6bdfbcb161a8049474 Mon Sep 17 00:00:00 2001 From: bastien ollier Date: Tue, 26 Sep 2023 11:04:34 +0200 Subject: [PATCH] test rpc --- CMakeLists.txt | 21 ++++++++++++++++++++- src/host.cpp | 14 ++++---------- src/host.hpp | 6 +++--- src/main.cpp | 29 ++++++++++++++++++++++++++--- 4 files changed, 53 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a99602d..97dad67 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,14 @@ project(planificador) add_compile_options(-Wall -Wextra -pedantic) include(FetchContent) + +FetchContent_Declare( + rpclib + GIT_REPOSITORY https://github.com/rpclib/rpclib.git + GIT_TAG master +) +FetchContent_MakeAvailable(rpclib) + FetchContent_Declare( tomlplusplus GIT_REPOSITORY https://github.com/marzer/tomlplusplus.git @@ -13,7 +21,18 @@ FetchContent_Declare( FetchContent_MakeAvailable(tomlplusplus) file(GLOB nameFile "src/*.cpp") -add_executable(planificador src/host.cpp src/runner.cpp src/config.cpp src/main.cpp ) + +add_library(rpclib_lib STATIC IMPORTED) +set_target_properties(rpclib_lib PROPERTIES IMPORTED_LOCATION "${rpclib_BINARY_DIR}/librpc.a") + +add_executable(planificador src/host.cpp src/runner.cpp src/config.cpp src/main.cpp) target_compile_features(planificador PUBLIC cxx_std_17) include_directories(${tomlplusplus_SOURCE_DIR}/include) +include_directories(${rpclib_SOURCE_DIR}/include) + +SET(THREADS_PREFER_PTHREAD_FLAG ON) +find_package(Threads REQUIRED) +target_link_libraries(planificador PRIVATE Threads::Threads) +# Lier votre exĂ©cutable avec la bibliothèque rpclib +target_link_libraries(planificador PRIVATE rpclib_lib) diff --git a/src/host.cpp b/src/host.cpp index 479281f..0d4a455 100644 --- a/src/host.cpp +++ b/src/host.cpp @@ -4,20 +4,14 @@ namespace sk { host::host(const std::string &ip, unsigned int connectionsMax) : ip{ip}, connections{0}, connectionsMax{connectionsMax}, runners{} {} -void host::addConnection(sk::runner& runner) { +void host::addConnection(sk::runner &runner) { runners.push(runner); connections += 1; } -const std::string& host::getIp() const { - return ip; -} +const std::string &host::getIp() const { return ip; } -unsigned int host::getNbConnections() const { - return connections; -} +unsigned int host::getNbConnections() const { return connections; } -unsigned int host::getNbConnectionsMax() const { - return connectionsMax; -} +unsigned int host::getNbConnectionsMax() const { return connectionsMax; } } diff --git a/src/host.hpp b/src/host.hpp index f5fe88f..1d5d62e 100644 --- a/src/host.hpp +++ b/src/host.hpp @@ -1,8 +1,8 @@ #pragma once -#include +#include "runner.hpp" #include -#include "runner.hpp" +#include namespace sk { class host { @@ -15,7 +15,7 @@ class host { public: host(const std::string &ip, unsigned int connectionsMax); - void addConnection(sk::runner& runner); + void addConnection(sk::runner &runner); const std::string &getIp() const; diff --git a/src/main.cpp b/src/main.cpp index 7672bb8..cf74864 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,6 +31,8 @@ sk::runner_backend detect_backend() { return sk::runner_backend::BubbleWrap; } +#include "rpc/server.h" + int main() { std::vector listsHosts = sk::config::loadHostsFromToml("../conf.toml"); std::priority_queue hosts(listsHosts.begin(), listsHosts.end()); @@ -39,8 +41,7 @@ int main() { std::cerr << "Pas de host" << std::endl; } - std::queue queue; - queue.push(sk::program{"echo $(( 1 + 2 ))", "ghcr.io/moshell-lang/moshell:master"}); + /*queue.push(sk::program{"echo $(( 1 + 2 ))", "ghcr.io/moshell-lang/moshell:master"}); sk::runner runner(detect_backend()); while (!queue.empty()) { @@ -49,6 +50,28 @@ int main() { std::cout << "out: " << res.out << "\n"; std::cout << "err: " << res.err << "\n"; queue.pop(); - } + }*/ + + + rpc::server srv(8080); + + srv.bind("add", [](const std::string& prog, const std::string& image) { + std::cout<<"log"< queue; + queue.push(sk::program{prog, image}); + sk::runner runner(detect_backend()); + + const sk::program ¤t = queue.front(); + sk::run_result res = runner.run_blocking(current); + std::cout << "out: " << res.out << "\n"; + std::cout << "err: " << res.err << "\n"; + queue.pop(); + + return res.out; + }); + + srv.run(); + + return 0; }