Skip to content

Commit

Permalink
neighbors: load state on startup from json file
Browse files Browse the repository at this point in the history
  • Loading branch information
astibal committed Apr 20, 2024
1 parent eba2c1c commit ca38879
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/service/core/smithproxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::thread>([] () {
CRYPTO_set_mem_functions( mempool_alloc, mempool_realloc, mempool_free);
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/service/core/smithproxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ca38879

Please sign in to comment.