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

@ -24,8 +24,6 @@
#include <lokimq/address.h>
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

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

@ -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<llarp::Context>();
context->config = std::make_unique<llarp::Config>(*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<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); });
}
@ -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<std::mutex> 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;

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

@ -9,10 +9,8 @@ namespace llarp
{
using Context_ptr = std::shared_ptr<Context>;
py::class_<Context, Context_ptr>(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(); })

Loading…
Cancel
Save