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-04-24 17:10:05 +00:00
|
|
|
#include "util/str.hpp"
|
2020-03-04 05:57:07 +00:00
|
|
|
#include "router/abstractrouter.hpp"
|
2020-02-27 01:18:38 +00:00
|
|
|
|
2020-02-26 22:05:04 +00:00
|
|
|
#include <chrono>
|
2020-03-04 05:57:07 +00:00
|
|
|
#include <algorithm>
|
2020-02-26 22:05:04 +00:00
|
|
|
|
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-04-07 18:38:56 +00:00
|
|
|
RouterHive::AddRouter(
|
2020-04-07 23:50:50 +00:00
|
|
|
const std::shared_ptr<llarp::Config>& config, std::vector<llarp_main*>* routers, bool isRelay)
|
2020-02-26 22:05:04 +00:00
|
|
|
{
|
2020-04-07 23:50:50 +00:00
|
|
|
llarp_main* ctx = llarp_main_init_from_config(config->Copy(), isRelay);
|
2020-04-24 17:10:05 +00:00
|
|
|
auto result = llarp_main_setup(ctx);
|
|
|
|
if (result == 0)
|
2020-02-27 22:05:25 +00:00
|
|
|
{
|
|
|
|
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-04-24 17:10:05 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error(llarp::stringify(
|
|
|
|
"Failed to add RouterHive ",
|
|
|
|
(isRelay ? "relay" : "client"),
|
|
|
|
", llarp_main_setup() returned ",
|
|
|
|
result));
|
|
|
|
}
|
2020-02-26 22:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
RouterHive::AddRelay(const std::shared_ptr<llarp::Config>& config)
|
2020-03-02 22:10:01 +00:00
|
|
|
{
|
2020-04-07 23:50:50 +00:00
|
|
|
AddRouter(config, &relays, true);
|
2020-03-02 22:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
RouterHive::AddClient(const std::shared_ptr<llarp::Config>& config)
|
2020-02-26 22:05:04 +00:00
|
|
|
{
|
2020-04-07 23:50:50 +00:00
|
|
|
AddRouter(config, &clients, false);
|
2020-03-02 22:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-24 17:10:05 +00:00
|
|
|
RouterHive::StartRouters(std::vector<llarp_main*>* routers, bool isRelay)
|
2020-03-02 22:10:01 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (llarp_main* ctx : *routers)
|
2020-02-26 22:05:04 +00:00
|
|
|
{
|
2020-04-24 17:10:05 +00:00
|
|
|
routerMainThreads.emplace_back([=]() {
|
|
|
|
llarp_main_run(ctx, llarp_main_runtime_opts{false, false, false, isRelay});
|
2020-02-28 18:44:27 +00:00
|
|
|
});
|
2020-03-04 00:28:23 +00:00
|
|
|
std::this_thread::sleep_for(2ms);
|
2020-02-26 22:05:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 22:10:01 +00:00
|
|
|
void
|
|
|
|
RouterHive::StartRelays()
|
|
|
|
{
|
2020-04-24 17:10:05 +00:00
|
|
|
StartRouters(&relays, true);
|
2020-03-02 22:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RouterHive::StartClients()
|
|
|
|
{
|
2020-04-24 17:10:05 +00:00
|
|
|
StartRouters(&clients, false);
|
2020-03-02 22:10:01 +00:00
|
|
|
}
|
|
|
|
|
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-04-07 18:38:56 +00:00
|
|
|
for (llarp_main* ctx : relays)
|
2020-03-02 22:10:01 +00:00
|
|
|
{
|
|
|
|
llarp_main_signal(ctx, 2 /* SIGINT */);
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
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-04-07 18:38:56 +00:00
|
|
|
for (llarp_main* ctx : relays)
|
2020-03-02 22:10:01 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
while (llarp_main_is_running(ctx))
|
2020-03-02 22:10:01 +00:00
|
|
|
{
|
|
|
|
std::this_thread::sleep_for(10ms);
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
for (llarp_main* ctx : clients)
|
2020-02-26 22:05:04 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
while (llarp_main_is_running(ctx))
|
2020-02-26 22:05:04 +00:00
|
|
|
{
|
|
|
|
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-04-07 18:38:56 +00:00
|
|
|
for (auto& thread : routerMainThreads)
|
2020-02-28 01:23:36 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
while (not thread.joinable())
|
2020-02-28 01:23:36 +00:00
|
|
|
{
|
|
|
|
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-04-07 18:38:56 +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-04-07 18:38:56 +00:00
|
|
|
std::lock_guard<std::mutex> guard{eventQueueMutex};
|
2020-02-29 22:53:54 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
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-04-07 18:38:56 +00:00
|
|
|
std::deque<RouterEventPtr>
|
2020-03-03 00:42:06 +00:00
|
|
|
RouterHive::GetAllEvents()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::lock_guard<std::mutex> guard{eventQueueMutex};
|
2020-03-03 00:42:06 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::deque<RouterEventPtr> events;
|
|
|
|
if (not eventQueue.empty())
|
2020-03-03 00:42:06 +00:00
|
|
|
{
|
|
|
|
eventQueue.swap(events);
|
|
|
|
}
|
|
|
|
return events;
|
|
|
|
}
|
|
|
|
|
2020-02-28 16:29:15 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
RouterHive::VisitRouter(llarp_main* router, std::function<void(Context_ptr)> visit)
|
2020-03-02 22:10:01 +00:00
|
|
|
{
|
|
|
|
auto ctx = llarp::Context::Get(router);
|
2020-03-07 01:20:11 +00:00
|
|
|
LogicCall(ctx->logic, [visit, ctx]() { visit(ctx); });
|
2020-03-02 22:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
RouterHive::VisitRelay(size_t index, std::function<void(Context_ptr)> visit)
|
2020-02-28 16:29:15 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +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
|
2020-04-07 18:38:56 +00:00
|
|
|
RouterHive::VisitClient(size_t index, std::function<void(Context_ptr)> visit)
|
2020-03-02 22:10:01 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (index >= clients.size())
|
2020-03-02 22:10:01 +00:00
|
|
|
{
|
|
|
|
visit(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
VisitRouter(clients[index], visit);
|
2020-02-28 16:29:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<size_t>
|
2020-03-04 05:57:07 +00:00
|
|
|
RouterHive::RelayConnectedRelays()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<size_t> results;
|
2020-03-04 05:57:07 +00:00
|
|
|
results.resize(relays.size());
|
|
|
|
std::mutex results_lock;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
size_t i = 0;
|
2020-03-04 05:57:07 +00:00
|
|
|
size_t done_count = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto relay : relays)
|
2020-03-04 05:57:07 +00:00
|
|
|
{
|
|
|
|
auto ctx = llarp::Context::Get(relay);
|
|
|
|
LogicCall(ctx->logic, [&, i, ctx]() {
|
|
|
|
size_t count = ctx->router->NumberOfConnectedRouters();
|
2020-04-07 18:38:56 +00:00
|
|
|
std::lock_guard<std::mutex> guard{results_lock};
|
2020-03-04 05:57:07 +00:00
|
|
|
results[i] = count;
|
|
|
|
done_count++;
|
|
|
|
});
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
while (true)
|
2020-03-04 05:57:07 +00:00
|
|
|
{
|
|
|
|
size_t read_done_count = 0;
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::lock_guard<std::mutex> guard{results_lock};
|
2020-03-04 05:57:07 +00:00
|
|
|
read_done_count = done_count;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (read_done_count == relays.size())
|
2020-03-04 05:57:07 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(100ms);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2020-04-24 17:10:05 +00:00
|
|
|
// TODO: DRY -- this smells a lot like RelayConnectedRelays()
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<llarp::RouterContact>
|
2020-03-04 05:57:07 +00:00
|
|
|
RouterHive::GetRelayRCs()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<llarp::RouterContact> results;
|
2020-03-04 05:57:07 +00:00
|
|
|
results.resize(relays.size());
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
size_t i = 0;
|
|
|
|
for (auto relay : relays)
|
2020-03-04 05:57:07 +00:00
|
|
|
{
|
|
|
|
auto ctx = llarp::Context::Get(relay);
|
2020-05-06 14:45:30 +00:00
|
|
|
results[i] = ctx->router->rc();
|
2020-03-04 05:57:07 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2020-03-07 01:20:11 +00:00
|
|
|
} // namespace tooling
|