Remove llarp C API usage from RouterHive

pull/1312/head
Stephen Shelton 4 years ago
parent 84c83a2400
commit b0d8568452
No known key found for this signature in database
GPG Key ID: EE4BADACCE8B631C

@ -5,6 +5,7 @@
#include <util/types.hpp> #include <util/types.hpp>
#include <ev/ev.hpp> #include <ev/ev.hpp>
#include <nodedb.hpp> #include <nodedb.hpp>
#include <crypto/crypto.hpp>
#include <iostream> #include <iostream>
#include <map> #include <map>
@ -26,8 +27,6 @@ namespace llarp
class Logic; class Logic;
struct AbstractRouter; struct AbstractRouter;
struct Config; struct Config;
struct Crypto;
struct CryptoManager;
struct RouterContact; struct RouterContact;
namespace thread namespace thread
{ {

@ -24,8 +24,6 @@
#include <lokimq/address.h> #include <lokimq/address.h>
struct llarp_config;
namespace llarp namespace llarp
{ {
using SectionValues_t = llarp::ConfigParser::SectionValues_t; using SectionValues_t = llarp::ConfigParser::SectionValues_t;
@ -224,9 +222,6 @@ namespace llarp
std::string std::string
generateBaseRouterConfig(fs::path defaultDataDir); generateBaseRouterConfig(fs::path defaultDataDir);
llarp_config*
Copy() const;
}; };
void void

@ -30,9 +30,7 @@ namespace llarp
bool bool
Context::Configure(const RuntimeOptions& opts, std::optional<fs::path> dataDir) Context::Configure(const RuntimeOptions& opts, std::optional<fs::path> dataDir)
{ {
if (config) if (not config)
throw std::runtime_error("Re-configure not supported");
config = std::make_unique<Config>(); config = std::make_unique<Config>();
fs::path defaultDataDir = dataDir ? *dataDir : GetDefaultDataDir(); fs::path defaultDataDir = dataDir ? *dataDir : GetDefaultDataDir();

@ -18,25 +18,16 @@ namespace tooling
{ {
auto& container = (isRelay ? relays : clients); auto& container = (isRelay ? relays : clients);
llarp_main* ctx = llarp_main_init_from_config(config->Copy(), isRelay); Context_ptr context = std::make_shared<llarp::Context>();
auto result = llarp_main_setup(ctx, isRelay); context->config = std::make_unique<llarp::Config>(*config.get());
if (result == 0) context->Configure(isRelay, {});
{ context->Setup(isRelay);
auto context = llarp::Context::Get(ctx);
auto routerId = llarp::RouterID(context->router->pubkey()); auto routerId = llarp::RouterID(context->router->pubkey());
context->InjectHive(this); context->InjectHive(this);
container[routerId] = ctx; container[routerId] = context;
std::cout << "Generated router with ID " << routerId << std::endl; 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));
}
}
void void
RouterHive::AddRelay(const std::shared_ptr<llarp::Config>& config) RouterHive::AddRelay(const std::shared_ptr<llarp::Config>& config)
@ -58,7 +49,7 @@ namespace tooling
for (auto [routerId, ctx] : container) for (auto [routerId, ctx] : container)
{ {
routerMainThreads.emplace_back([=]() { 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); std::this_thread::sleep_for(2ms);
} }
@ -82,24 +73,24 @@ namespace tooling
llarp::LogInfo("Signalling all routers to stop"); llarp::LogInfo("Signalling all routers to stop");
for (auto [routerId, ctx] : relays) for (auto [routerId, ctx] : relays)
{ {
llarp_main_signal(ctx, 2 /* SIGINT */); LogicCall(ctx->logic, [ctx]() { ctx->HandleSignal(SIGINT); });
} }
for (auto [routerId, ctx] : clients) 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"); llarp::LogInfo("Waiting on routers to be stopped");
for (auto [routerId, ctx] : relays) for (auto [routerId, ctx] : relays)
{ {
while (llarp_main_is_running(ctx)) while (ctx->IsUp())
{ {
std::this_thread::sleep_for(10ms); std::this_thread::sleep_for(10ms);
} }
} }
for (auto [routerId, ctx] : clients) for (auto [routerId, ctx] : clients)
{ {
while (llarp_main_is_running(ctx)) while (ctx->IsUp())
{ {
std::this_thread::sleep_for(10ms); std::this_thread::sleep_for(10ms);
} }
@ -154,9 +145,8 @@ namespace tooling
} }
void void
RouterHive::VisitRouter(llarp_main* router, std::function<void(Context_ptr)> visit) RouterHive::VisitRouter(Context_ptr ctx, std::function<void(Context_ptr)> visit)
{ {
auto ctx = llarp::Context::Get(router);
LogicCall(ctx->logic, [visit, ctx]() { visit(ctx); }); LogicCall(ctx->logic, [visit, ctx]() { visit(ctx); });
} }
@ -170,7 +160,7 @@ namespace tooling
if (itr == relays.end()) if (itr == relays.end())
return nullptr; return nullptr;
auto ctx = llarp::Context::Get(itr->second); auto ctx = itr->second;
return ctx->router.get(); return ctx->router.get();
} }
@ -186,9 +176,8 @@ namespace tooling
size_t done_count = 0; size_t done_count = 0;
for (auto [routerId, ctx] : relays) for (auto [routerId, ctx] : relays)
{ {
auto context = llarp::Context::Get(ctx); LogicCall(ctx->logic, [&, i, ctx]() {
LogicCall(context->logic, [&, i, context]() { size_t count = ctx->router->NumberOfConnectedRouters();
size_t count = context->router->NumberOfConnectedRouters();
std::lock_guard<std::mutex> guard{results_lock}; std::lock_guard<std::mutex> guard{results_lock};
results[i] = count; results[i] = count;
done_count++; done_count++;
@ -222,8 +211,7 @@ namespace tooling
size_t i = 0; size_t i = 0;
for (auto [routerId, ctx] : relays) for (auto [routerId, ctx] : relays)
{ {
auto context = llarp::Context::Get(ctx); results[i] = ctx->router->rc();
results[i] = context->router->rc();
i++; i++;
} }
return results; return results;

@ -34,9 +34,9 @@ namespace tooling
void void
AddRouter(const std::shared_ptr<llarp::Config>& config, bool isRelay); AddRouter(const std::shared_ptr<llarp::Config>& config, bool isRelay);
/// safely visit router /// safely visit router (asynchronously)
void void
VisitRouter(llarp_main* router, std::function<void(Context_ptr)> visit); VisitRouter(Context_ptr ctx, std::function<void(Context_ptr)> visit);
public: public:
RouterHive() = default; RouterHive() = default;
@ -90,8 +90,8 @@ namespace tooling
GetRelayRCs(); GetRelayRCs();
std::mutex routerMutex; std::mutex routerMutex;
std::unordered_map<llarp::RouterID, llarp_main*, llarp::RouterID::Hash> relays; std::unordered_map<llarp::RouterID, Context_ptr, llarp::RouterID::Hash> relays;
std::unordered_map<llarp::RouterID, llarp_main*, llarp::RouterID::Hash> clients; std::unordered_map<llarp::RouterID, Context_ptr, llarp::RouterID::Hash> clients;
std::vector<std::thread> routerMainThreads; std::vector<std::thread> routerMainThreads;

@ -9,10 +9,8 @@ namespace llarp
{ {
using Context_ptr = std::shared_ptr<Context>; using Context_ptr = std::shared_ptr<Context>;
py::class_<Context, Context_ptr>(mod, "Context") py::class_<Context, Context_ptr>(mod, "Context")
.def( .def("Setup", [](Context_ptr self, bool isRelay) { self->Setup(isRelay); })
"Setup", .def("Run", [](Context_ptr self) -> int { return self->Run(RuntimeOptions{}); })
[](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("Stop", [](Context_ptr self) { self->CloseAsync(); }) .def("Stop", [](Context_ptr self) { self->CloseAsync(); })
.def("IsUp", &Context::IsUp) .def("IsUp", &Context::IsUp)
.def("IsRelay", [](Context_ptr self) -> bool { return self->router->IsServiceNode(); }) .def("IsRelay", [](Context_ptr self) -> bool { return self->router->IsServiceNode(); })

Loading…
Cancel
Save