diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d2378f..a99602d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ FetchContent_Declare( FetchContent_MakeAvailable(tomlplusplus) file(GLOB nameFile "src/*.cpp") -add_executable(planificador src/host.cpp src/runner.cpp src/main.cpp) +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) diff --git a/conf.toml b/conf.toml new file mode 100644 index 0000000..812326e --- /dev/null +++ b/conf.toml @@ -0,0 +1,12 @@ +[serveurs] +[serveurs.serveur1] +ip = '100.000.000.000' +nbContainerMax = 10 + +[serveurs.serveur2] +ip = '200.000.000.000' +nbContainerMax = 20 + +[serveurs.serveur3] +ip = '300.000.000.000' +nbContainerMax = 30 \ No newline at end of file diff --git a/src/config.cpp b/src/config.cpp new file mode 100644 index 0000000..209081f --- /dev/null +++ b/src/config.cpp @@ -0,0 +1,32 @@ +#include "config.hpp" + + +std::vector sk::config::loadHostsFromToml(const fs::path& confFile) { + std::vector hosts; + + auto config = toml::parse_file( confFile.string() ); + toml::table *server_section = config.get_as("serveurs"); + if (server_section == nullptr) { + return hosts; + } + + // Parcourir les serveurs + for (const auto& [server_name_key, server_info_value] : *server_section) { + std::string server_name{server_name_key}; + toml::table *server_info_ptr = server_info_value.as_table(); + if (server_info_ptr == nullptr) { + + } + toml::table &server_info = *server_info_ptr; + + if(server_info.get_as("ip") && server_info.get_as("nbContainerMax")){ + std::string server_ip = server_info.get_as("ip")->get(); + int server_max_containers = server_info.get_as("nbContainerMax")->get(); + + hosts.push_back(sk::host(server_ip,server_max_containers)); + } + } + + return hosts; +} + diff --git a/src/config.hpp b/src/config.hpp new file mode 100644 index 0000000..9962395 --- /dev/null +++ b/src/config.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include +#include +#include + +#include "host.hpp" +namespace fs = std::filesystem; + +namespace sk{ +class config{ + public: + static std::vector loadHostsFromToml(const fs::path& confFile); + +}; +} \ No newline at end of file diff --git a/src/host.cpp b/src/host.cpp index 011cd13..8961514 100644 --- a/src/host.cpp +++ b/src/host.cpp @@ -4,9 +4,19 @@ namespace sk { host::host(const std::string &ip, unsigned int connectionsMax) : ip{ip}, connections{0}, connectionsMax{connectionsMax} {} -void host::addConnection() { connections += 1; } +void host::addConnection() { + connections += 1; +} -unsigned int host::getNbConnections() const { return connections; } +const std::string& host::getIp() const { + return ip; +} -unsigned int host::getNbConnectionsMax() const { return connectionsMax; } +unsigned int host::getNbConnections() const { + return connections; +} + +unsigned int host::getNbConnectionsMax() const { + return connectionsMax; +} } diff --git a/src/host.hpp b/src/host.hpp index 949789f..866e496 100644 --- a/src/host.hpp +++ b/src/host.hpp @@ -12,6 +12,9 @@ class host { public: host(const std::string &ip, unsigned int connectionsMax); void addConnection(); + + const std::string& getIp() const; + unsigned int getNbConnections() const; unsigned int getNbConnectionsMax() const; diff --git a/src/main.cpp b/src/main.cpp index f2fcf37..9ee304e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,53 +2,25 @@ #include "runner.hpp" #include #include - -#include #include #include "host.hpp" -#include -#include -#include -#include - -namespace fs = std::filesystem; - -std::vector makeMachinesFromToml(const fs::path &confFile) { - std::vector hosts; +#include "config.hpp" - auto config = toml::parse_file(confFile.string()); - toml::table *server_section = config.get_as("serveurs"); - // Parcourir les serveurs - for (const auto &[server_name_key, server_info_value] : *server_section) { - std::string server_name{server_name_key}; - toml::table *server_info_ptr = server_info_value.as_table(); - if (server_info_ptr == nullptr) { - } - toml::table &server_info = *server_info_ptr; - if (server_info.get_as("ip") && - server_info.get_as("nbContainerMax")) { - std::string server_ip = - server_info.get_as("ip")->get(); - int server_max_containers = - server_info.get_as("nbContainerMax")->get(); - - hosts.push_back(sk::host(server_ip, server_max_containers)); - } - } +int main() { + std::vector hosts = sk::config::loadHostsFromToml("../conf.toml"); - return hosts; -} + if(hosts.empty()){ + std::cerr<<"Pas de host"< hosts = makeMachinesFromToml("conf.toml"); - for (const auto &host : hosts) { - std::cout << host.getNbConnectionsMax() << std::endl; - } + for(const auto &host : hosts){ + std::cout< queue; + /*std::queue queue; queue.push(sk::program{"echo $(( 1 + 2 ))", "ghcr.io/moshell-lang/moshell:master"}); @@ -59,6 +31,6 @@ int main() { std::cout << "out: " << res.out << "\n"; std::cout << "err: " << res.err << "\n"; queue.pop(); - } + }*/ return 0; }