From ca3887919fbdd22d692ab40c1dc2309bacb97cea Mon Sep 17 00:00:00 2001 From: ales stibal Date: Sat, 20 Apr 2024 17:41:47 +0200 Subject: [PATCH] neighbors: load state on startup from json file --- src/service/core/smithproxy.cpp | 44 +++++++++++++++++++++++++++++++++ src/service/core/smithproxy.hpp | 3 +++ 2 files changed, 47 insertions(+) diff --git a/src/service/core/smithproxy.cpp b/src/service/core/smithproxy.cpp index bff1170..6299bd7 100644 --- a/src/service/core/smithproxy.cpp +++ b/src/service/core/smithproxy.cpp @@ -335,6 +335,10 @@ void SmithProxy::run() { std::string friendly_thread_name_redir_udp = string_format("sxy_rdu_%d",CfgFactory::get()->tenant_index); + if(not state_load()) { + Log::get()->events().insert(ERR, "state was not fully loaded"); + } + // cli_loop uses select :( cli_thread = std::make_shared([] () { CRYPTO_set_mem_functions( mempool_alloc, mempool_realloc, mempool_free); @@ -521,6 +525,46 @@ void SmithProxy::state_save_neighbors(std::string const& fnm) const { } } + +bool SmithProxy::state_load() { + std::string state_dir; + std::string tenant_name = "default"; + + { + auto fac = CfgFactory::get(); + auto lc_ = std::scoped_lock(fac->lock()); + state_dir = fac->capture_local.dir; + tenant_name = fac->tenant_name; + } + + auto nbr_file = string_format("%s/nbr-%s.json", state_dir.c_str(), tenant_name.c_str()); + bool nbr_loaded = state_load_neighbors(nbr_file); + + if(nbr_loaded) { + return true; + } + + return false; +} + +bool SmithProxy::state_load_neighbors(std::string const& fnm) { + + auto ifs = std::ifstream(fnm); + try { + nlohmann::json js = nlohmann::json::parse(ifs); + NbrHood::instance().ser_json_in(js); + + Log::get()->events().insert(INF, "neighbors file loaded successfully"); + return true; + } + catch(nlohmann::json::exception const& e) { + _err("state_load_neighbors: error: %s", e.what()); + Log::get()->events().insert(ERR, "neighbors file not loaded"); + } + return false; +} + + void SmithProxy::join_all() { auto join_thread_list = [](auto thread_list) { diff --git a/src/service/core/smithproxy.hpp b/src/service/core/smithproxy.hpp index 109e6dd..8740fc7 100644 --- a/src/service/core/smithproxy.hpp +++ b/src/service/core/smithproxy.hpp @@ -128,7 +128,10 @@ class SmithProxy : public Service { void run() override; void state_save() const; + bool state_load(); + void state_save_neighbors(std::string const& fnm) const; + bool state_load_neighbors(std::string const& fnm); void kill_proxies(); void stop() override;