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-03-02 22:10:01 +00:00
|
|
|
RouterHive::AddRouter(const std::shared_ptr<llarp::Config> & config, std::vector<llarp_main *> *routers)
|
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);
|
2020-03-02 22:10:01 +00:00
|
|
|
routers->push_back(ctx);
|
2020-02-27 22:05:25 +00:00
|
|
|
}
|
2020-02-26 22:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-03-02 22:10:01 +00:00
|
|
|
RouterHive::AddRelay(const std::shared_ptr<llarp::Config> & config)
|
|
|
|
{
|
|
|
|
AddRouter(config, &relays);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RouterHive::AddClient(const std::shared_ptr<llarp::Config> & config)
|
2020-02-26 22:05:04 +00:00
|
|
|
{
|
2020-03-02 22:10:01 +00:00
|
|
|
AddRouter(config, &clients);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RouterHive::StartRouters(std::vector<llarp_main *> *routers)
|
|
|
|
{
|
|
|
|
for (llarp_main* ctx : *routers)
|
2020-02-26 22:05:04 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 22:10:01 +00:00
|
|
|
void
|
|
|
|
RouterHive::StartRelays()
|
|
|
|
{
|
|
|
|
StartRouters(&relays);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RouterHive::StartClients()
|
|
|
|
{
|
|
|
|
StartRouters(&clients);
|
|
|
|
}
|
|
|
|
|
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-03-02 22:10:01 +00:00
|
|
|
for (llarp_main* ctx : relays)
|
|
|
|
{
|
|
|
|
llarp_main_signal(ctx, 2 /* SIGINT */);
|
|
|
|
}
|
|
|
|
for (llarp_main* ctx : clients)
|
2020-02-26 22:05:04 +00:00
|
|
|
{
|
|
|
|
llarp_main_signal(ctx, 2 /* SIGINT */);
|
|
|
|
}
|
|
|
|
|
2020-02-29 22:53:54 +00:00
|
|
|
llarp::LogInfo("Waiting on routers to be stopped");
|
2020-03-02 22:10:01 +00:00
|
|
|
for (llarp_main* ctx : relays)
|
|
|
|
{
|
|
|
|
while(llarp_main_is_running(ctx))
|
|
|
|
{
|
|
|
|
std::this_thread::sleep_for(10ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (llarp_main* ctx : clients)
|
2020-02-26 22:05:04 +00:00
|
|
|
{
|
|
|
|
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-03-03 00:42:06 +00:00
|
|
|
eventQueue.push_back(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());
|
2020-03-03 00:42:06 +00:00
|
|
|
eventQueue.pop_front();
|
2020-02-29 22:53:54 +00:00
|
|
|
return ptr;
|
2020-02-28 04:01:14 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
2020-02-28 02:19:47 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 00:42:06 +00:00
|
|
|
std::deque<RouterEventPtr>
|
|
|
|
RouterHive::GetAllEvents()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard{eventQueueMutex};
|
|
|
|
|
|
|
|
std::deque<RouterEventPtr> events;
|
|
|
|
if (not eventQueue.empty())
|
|
|
|
{
|
|
|
|
eventQueue.swap(events);
|
|
|
|
}
|
|
|
|
return events;
|
|
|
|
}
|
|
|
|
|
2020-02-28 16:29:15 +00:00
|
|
|
void
|
2020-03-02 22:10:01 +00:00
|
|
|
RouterHive::VisitRouter(llarp_main *router, std::function<void(Context_ptr)> visit)
|
|
|
|
{
|
|
|
|
auto ctx = llarp::Context::Get(router);
|
|
|
|
LogicCall(ctx->logic, [visit, ctx]() {
|
|
|
|
visit(ctx);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RouterHive::VisitRelay(size_t index, std::function<void(Context_ptr)> visit)
|
2020-02-28 16:29:15 +00:00
|
|
|
{
|
2020-03-02 22:10:01 +00:00
|
|
|
if(index >= relays.size())
|
2020-02-28 16:29:15 +00:00
|
|
|
{
|
|
|
|
visit(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
2020-03-02 22:10:01 +00:00
|
|
|
VisitRouter(relays[index], visit);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RouterHive::VisitClient(size_t index, std::function<void(Context_ptr)> visit)
|
|
|
|
{
|
|
|
|
if(index >= clients.size())
|
|
|
|
{
|
|
|
|
visit(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
VisitRouter(clients[index], visit);
|
2020-02-28 16:29:15 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 20:12:14 +00:00
|
|
|
} // namespace tooling
|