From b0d8568452b5e518d74053aa12dfa9879f2f84d6 Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Tue, 30 Jun 2020 09:45:26 -0600 Subject: [PATCH] Remove llarp C API usage from RouterHive --- include/llarp.hpp | 3 +-- llarp/config/config.hpp | 5 ---- llarp/context.cpp | 6 ++--- llarp/tooling/router_hive.cpp | 50 +++++++++++++---------------------- llarp/tooling/router_hive.hpp | 8 +++--- pybind/llarp/context.cpp | 6 ++--- 6 files changed, 28 insertions(+), 50 deletions(-) diff --git a/include/llarp.hpp b/include/llarp.hpp index 4c07e764e..e6a22c520 100644 --- a/include/llarp.hpp +++ b/include/llarp.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -26,8 +27,6 @@ namespace llarp class Logic; struct AbstractRouter; struct Config; - struct Crypto; - struct CryptoManager; struct RouterContact; namespace thread { diff --git a/llarp/config/config.hpp b/llarp/config/config.hpp index 77d3e5de0..5f92571e9 100644 --- a/llarp/config/config.hpp +++ b/llarp/config/config.hpp @@ -24,8 +24,6 @@ #include -struct llarp_config; - namespace llarp { using SectionValues_t = llarp::ConfigParser::SectionValues_t; @@ -224,9 +222,6 @@ namespace llarp std::string generateBaseRouterConfig(fs::path defaultDataDir); - - llarp_config* - Copy() const; }; void diff --git a/llarp/context.cpp b/llarp/context.cpp index 7925e7836..9457235c2 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -30,10 +30,8 @@ namespace llarp bool Context::Configure(const RuntimeOptions& opts, std::optional dataDir) { - if (config) - throw std::runtime_error("Re-configure not supported"); - - config = std::make_unique(); + if (not config) + config = std::make_unique(); fs::path defaultDataDir = dataDir ? *dataDir : GetDefaultDataDir(); diff --git a/llarp/tooling/router_hive.cpp b/llarp/tooling/router_hive.cpp index 7001c70ea..3fca59d1f 100644 --- a/llarp/tooling/router_hive.cpp +++ b/llarp/tooling/router_hive.cpp @@ -18,24 +18,15 @@ namespace tooling { auto& container = (isRelay ? relays : clients); - llarp_main* ctx = llarp_main_init_from_config(config->Copy(), isRelay); - auto result = llarp_main_setup(ctx, isRelay); - if (result == 0) - { - auto context = llarp::Context::Get(ctx); - auto routerId = llarp::RouterID(context->router->pubkey()); - context->InjectHive(this); - container[routerId] = ctx; - std::cout << "Generated router with ID " << routerId << std::endl; - } - else - { - throw std::runtime_error(llarp::stringify( - "Failed to add RouterHive ", - (isRelay ? "relay" : "client"), - ", llarp_main_setup() returned ", - result)); - } + Context_ptr context = std::make_shared(); + context->config = std::make_unique(*config.get()); + context->Configure(isRelay, {}); + context->Setup(isRelay); + + auto routerId = llarp::RouterID(context->router->pubkey()); + context->InjectHive(this); + container[routerId] = context; + std::cout << "Generated router with ID " << routerId << std::endl; } void @@ -58,7 +49,7 @@ namespace tooling for (auto [routerId, ctx] : container) { routerMainThreads.emplace_back([=]() { - llarp_main_run(ctx, llarp_main_runtime_opts{false, false, false, isRelay}); + ctx->Run(llarp::RuntimeOptions{false, false, isRelay}); }); std::this_thread::sleep_for(2ms); } @@ -82,24 +73,24 @@ namespace tooling llarp::LogInfo("Signalling all routers to stop"); for (auto [routerId, ctx] : relays) { - llarp_main_signal(ctx, 2 /* SIGINT */); + LogicCall(ctx->logic, [ctx]() { ctx->HandleSignal(SIGINT); }); } for (auto [routerId, ctx] : clients) { - llarp_main_signal(ctx, 2 /* SIGINT */); + LogicCall(ctx->logic, [ctx]() { ctx->HandleSignal(SIGINT); }); } llarp::LogInfo("Waiting on routers to be stopped"); for (auto [routerId, ctx] : relays) { - while (llarp_main_is_running(ctx)) + while (ctx->IsUp()) { std::this_thread::sleep_for(10ms); } } for (auto [routerId, ctx] : clients) { - while (llarp_main_is_running(ctx)) + while (ctx->IsUp()) { std::this_thread::sleep_for(10ms); } @@ -154,9 +145,8 @@ namespace tooling } void - RouterHive::VisitRouter(llarp_main* router, std::function visit) + RouterHive::VisitRouter(Context_ptr ctx, std::function visit) { - auto ctx = llarp::Context::Get(router); LogicCall(ctx->logic, [visit, ctx]() { visit(ctx); }); } @@ -170,7 +160,7 @@ namespace tooling if (itr == relays.end()) return nullptr; - auto ctx = llarp::Context::Get(itr->second); + auto ctx = itr->second; return ctx->router.get(); } @@ -186,9 +176,8 @@ namespace tooling size_t done_count = 0; for (auto [routerId, ctx] : relays) { - auto context = llarp::Context::Get(ctx); - LogicCall(context->logic, [&, i, context]() { - size_t count = context->router->NumberOfConnectedRouters(); + LogicCall(ctx->logic, [&, i, ctx]() { + size_t count = ctx->router->NumberOfConnectedRouters(); std::lock_guard guard{results_lock}; results[i] = count; done_count++; @@ -222,8 +211,7 @@ namespace tooling size_t i = 0; for (auto [routerId, ctx] : relays) { - auto context = llarp::Context::Get(ctx); - results[i] = context->router->rc(); + results[i] = ctx->router->rc(); i++; } return results; diff --git a/llarp/tooling/router_hive.hpp b/llarp/tooling/router_hive.hpp index 924a0554e..05590cb36 100644 --- a/llarp/tooling/router_hive.hpp +++ b/llarp/tooling/router_hive.hpp @@ -34,9 +34,9 @@ namespace tooling void AddRouter(const std::shared_ptr& config, bool isRelay); - /// safely visit router + /// safely visit router (asynchronously) void - VisitRouter(llarp_main* router, std::function visit); + VisitRouter(Context_ptr ctx, std::function visit); public: RouterHive() = default; @@ -90,8 +90,8 @@ namespace tooling GetRelayRCs(); std::mutex routerMutex; - std::unordered_map relays; - std::unordered_map clients; + std::unordered_map relays; + std::unordered_map clients; std::vector routerMainThreads; diff --git a/pybind/llarp/context.cpp b/pybind/llarp/context.cpp index e2e8084be..2c60098fe 100644 --- a/pybind/llarp/context.cpp +++ b/pybind/llarp/context.cpp @@ -9,10 +9,8 @@ namespace llarp { using Context_ptr = std::shared_ptr; py::class_(mod, "Context") - .def( - "Setup", - [](Context_ptr self, bool isRelay) -> bool { return self->Setup(isRelay) == 0; }) - .def("Run", [](Context_ptr self) -> int { return self->Run(llarp_main_runtime_opts{}); }) + .def("Setup", [](Context_ptr self, bool isRelay) { self->Setup(isRelay); }) + .def("Run", [](Context_ptr self) -> int { return self->Run(RuntimeOptions{}); }) .def("Stop", [](Context_ptr self) { self->CloseAsync(); }) .def("IsUp", &Context::IsUp) .def("IsRelay", [](Context_ptr self) -> bool { return self->router->IsServiceNode(); })