2020-02-26 20:12:14 +00:00
|
|
|
#include <tooling/router_hive.hpp>
|
|
|
|
|
2020-02-27 21:16:46 +00:00
|
|
|
#include "llarp.h"
|
|
|
|
#include "llarp.hpp"
|
2020-02-28 16:29:15 +00:00
|
|
|
#include "util/thread/logic.hpp"
|
2020-02-27 01:18:38 +00:00
|
|
|
|
2020-02-26 22:05:04 +00:00
|
|
|
#include <chrono>
|
|
|
|
|
2020-02-28 04:01:14 +00:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2020-02-26 20:12:14 +00:00
|
|
|
namespace tooling
|
|
|
|
{
|
|
|
|
|
2020-02-26 22:05:04 +00:00
|
|
|
void
|
2020-02-27 22:05:25 +00:00
|
|
|
RouterHive::AddRouter(const std::shared_ptr<llarp::Config> & config)
|
2020-02-26 22:05:04 +00:00
|
|
|
{
|
2020-02-27 22:05:25 +00:00
|
|
|
llarp_main* ctx = llarp_main_init_from_config(config->Copy());
|
|
|
|
if(llarp_main_setup(ctx) == 0)
|
|
|
|
{
|
|
|
|
llarp::Context::Get(ctx)->InjectHive(this);
|
|
|
|
routers.push_back(ctx);
|
|
|
|
}
|
2020-02-26 22:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-28 23:24:16 +00:00
|
|
|
RouterHive::StartRouters()
|
2020-02-26 22:05:04 +00:00
|
|
|
{
|
|
|
|
for (llarp_main* ctx : routers)
|
|
|
|
{
|
2020-02-28 23:24:16 +00:00
|
|
|
routerMainThreads.emplace_back([ctx](){
|
|
|
|
llarp_main_run(ctx, llarp_main_runtime_opts{false, false, false});
|
2020-02-28 18:44:27 +00:00
|
|
|
});
|
2020-02-28 09:50:33 +00:00
|
|
|
std::this_thread::sleep_for(20ms);
|
2020-02-26 22:05:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RouterHive::StopRouters()
|
|
|
|
{
|
|
|
|
|
2020-02-29 22:53:54 +00:00
|
|
|
llarp::LogInfo("Signalling all routers to stop");
|
2020-02-26 22:05:04 +00:00
|
|
|
for (llarp_main* ctx : routers)
|
|
|
|
{
|
|
|
|
llarp_main_signal(ctx, 2 /* SIGINT */);
|
|
|
|
}
|
|
|
|
|
2020-02-29 22:53:54 +00:00
|
|
|
llarp::LogInfo("Waiting on routers to be stopped");
|
2020-02-26 22:05:04 +00:00
|
|
|
for (llarp_main* ctx : routers)
|
|
|
|
{
|
|
|
|
while(llarp_main_is_running(ctx))
|
|
|
|
{
|
|
|
|
std::this_thread::sleep_for(10ms);
|
|
|
|
}
|
|
|
|
}
|
2020-02-28 01:23:36 +00:00
|
|
|
|
2020-02-29 22:53:54 +00:00
|
|
|
llarp::LogInfo("Joining all router threads");
|
2020-02-28 01:23:36 +00:00
|
|
|
for (auto& thread : routerMainThreads)
|
|
|
|
{
|
|
|
|
while (not thread.joinable())
|
|
|
|
{
|
|
|
|
std::this_thread::sleep_for(500ms);
|
|
|
|
}
|
|
|
|
thread.join();
|
|
|
|
}
|
2020-02-28 23:24:16 +00:00
|
|
|
|
2020-02-29 22:53:54 +00:00
|
|
|
llarp::LogInfo("RouterHive::StopRouters finished");
|
2020-02-26 22:05:04 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 20:12:14 +00:00
|
|
|
void
|
2020-02-27 21:16:46 +00:00
|
|
|
RouterHive::NotifyEvent(RouterEventPtr event)
|
2020-02-26 20:12:14 +00:00
|
|
|
{
|
2020-02-29 22:53:54 +00:00
|
|
|
std::lock_guard<std::mutex> guard{eventQueueMutex};
|
2020-02-26 20:12:14 +00:00
|
|
|
|
2020-02-29 22:53:54 +00:00
|
|
|
eventQueue.push(std::move(event));
|
2020-02-26 20:12:14 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 02:19:47 +00:00
|
|
|
RouterEventPtr
|
|
|
|
RouterHive::GetNextEvent()
|
|
|
|
{
|
2020-02-29 22:53:54 +00:00
|
|
|
std::lock_guard<std::mutex> guard{eventQueueMutex};
|
|
|
|
|
|
|
|
if (not eventQueue.empty())
|
2020-02-28 04:01:14 +00:00
|
|
|
{
|
2020-02-29 22:53:54 +00:00
|
|
|
auto ptr = std::move(eventQueue.front());
|
|
|
|
eventQueue.pop();
|
|
|
|
return ptr;
|
2020-02-28 04:01:14 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
2020-02-28 02:19:47 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 16:29:15 +00:00
|
|
|
void
|
|
|
|
RouterHive::VisitRouter(size_t index, std::function<void(Context_ptr)> visit)
|
|
|
|
{
|
|
|
|
if(index >= routers.size())
|
|
|
|
{
|
|
|
|
visit(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto * r = routers[index];
|
|
|
|
auto ctx = llarp::Context::Get(r);
|
|
|
|
LogicCall(ctx->logic, [visit, ctx]() {
|
|
|
|
visit(ctx);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-26 20:12:14 +00:00
|
|
|
} // namespace tooling
|