2020-02-26 20:12:14 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <tooling/router_event.hpp>
|
|
|
|
|
2020-02-26 22:05:04 +00:00
|
|
|
#include <llarp.h>
|
2020-02-27 22:05:25 +00:00
|
|
|
#include <config/config.hpp>
|
2020-02-26 20:12:14 +00:00
|
|
|
|
2020-02-27 01:18:38 +00:00
|
|
|
#include <vector>
|
2020-03-03 00:42:06 +00:00
|
|
|
#include <deque>
|
2020-02-27 01:18:38 +00:00
|
|
|
#include <thread>
|
2020-02-29 22:53:54 +00:00
|
|
|
#include <mutex>
|
2020-02-27 01:18:38 +00:00
|
|
|
|
2020-02-26 22:05:04 +00:00
|
|
|
struct llarp_config;
|
2020-02-27 01:18:38 +00:00
|
|
|
struct llarp_main;
|
2020-02-26 22:05:04 +00:00
|
|
|
|
2020-03-02 22:10:01 +00:00
|
|
|
namespace llarp
|
2020-02-28 16:29:15 +00:00
|
|
|
{
|
|
|
|
struct Context;
|
|
|
|
}
|
|
|
|
|
2020-02-26 20:12:14 +00:00
|
|
|
namespace tooling
|
|
|
|
{
|
|
|
|
struct RouterHive
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
using Context_ptr = std::shared_ptr<llarp::Context>;
|
2020-03-02 22:10:01 +00:00
|
|
|
|
2020-03-07 01:20:11 +00:00
|
|
|
private:
|
2020-03-02 22:10:01 +00:00
|
|
|
void
|
2020-04-24 17:10:05 +00:00
|
|
|
StartRouters(std::vector<llarp_main*>* routers, bool isRelay);
|
2020-03-02 22:10:01 +00:00
|
|
|
|
|
|
|
void
|
2020-04-07 23:50:50 +00:00
|
|
|
AddRouter(
|
|
|
|
const std::shared_ptr<llarp::Config>& config,
|
|
|
|
std::vector<llarp_main*>* routers,
|
|
|
|
bool isRelay);
|
2020-03-02 22:10:01 +00:00
|
|
|
|
|
|
|
/// safely visit router
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
VisitRouter(llarp_main* router, std::function<void(Context_ptr)> visit);
|
2020-03-02 22:10:01 +00:00
|
|
|
|
|
|
|
/// safely visit relay at index N
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
VisitRelay(size_t index, std::function<void(Context_ptr)> visit);
|
2020-03-02 22:10:01 +00:00
|
|
|
|
|
|
|
/// safely visit client at index N
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
VisitClient(size_t index, std::function<void(Context_ptr)> visit);
|
2020-03-02 22:10:01 +00:00
|
|
|
|
2020-03-07 01:20:11 +00:00
|
|
|
public:
|
2020-02-29 22:53:54 +00:00
|
|
|
RouterHive() = default;
|
2020-02-26 20:12:14 +00:00
|
|
|
|
2020-02-26 22:05:04 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
AddRelay(const std::shared_ptr<llarp::Config>& conf);
|
2020-03-02 22:10:01 +00:00
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
AddClient(const std::shared_ptr<llarp::Config>& conf);
|
2020-03-02 22:10:01 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
StartRelays();
|
2020-02-26 22:05:04 +00:00
|
|
|
|
2020-02-26 20:12:14 +00:00
|
|
|
void
|
2020-03-02 22:10:01 +00:00
|
|
|
StartClients();
|
2020-02-27 01:18:38 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
StopRouters();
|
|
|
|
|
|
|
|
void
|
2020-02-27 21:16:46 +00:00
|
|
|
NotifyEvent(RouterEventPtr event);
|
2020-02-26 20:12:14 +00:00
|
|
|
|
2020-02-28 02:19:47 +00:00
|
|
|
RouterEventPtr
|
|
|
|
GetNextEvent();
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::deque<RouterEventPtr>
|
2020-03-03 00:42:06 +00:00
|
|
|
GetAllEvents();
|
|
|
|
|
2020-03-02 22:10:01 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
ForEachRelay(std::function<void(Context_ptr)> visit)
|
2020-03-02 22:10:01 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (size_t idx = 0; idx < relays.size(); ++idx)
|
2020-03-02 22:10:01 +00:00
|
|
|
{
|
|
|
|
VisitRelay(idx, visit);
|
|
|
|
}
|
|
|
|
}
|
2020-02-26 20:12:14 +00:00
|
|
|
|
2020-03-02 22:10:01 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
ForEachClient(std::function<void(Context_ptr)> visit)
|
2020-02-28 16:29:15 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (size_t idx = 0; idx < clients.size(); ++idx)
|
2020-02-28 16:29:15 +00:00
|
|
|
{
|
2020-03-02 22:10:01 +00:00
|
|
|
VisitClient(idx, visit);
|
2020-02-28 16:29:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 22:10:01 +00:00
|
|
|
/// safely visit every router context
|
2020-02-28 16:29:15 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
ForEachRouter(std::function<void(Context_ptr)> visit)
|
2020-03-02 22:10:01 +00:00
|
|
|
{
|
|
|
|
ForEachRelay(visit);
|
|
|
|
ForEachClient(visit);
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<size_t>
|
2020-03-04 05:57:07 +00:00
|
|
|
RelayConnectedRelays();
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<llarp::RouterContact>
|
2020-03-04 05:57:07 +00:00
|
|
|
GetRelayRCs();
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<llarp_main*> relays;
|
|
|
|
std::vector<llarp_main*> clients;
|
2020-02-27 01:18:38 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<std::thread> routerMainThreads;
|
2020-02-26 20:12:14 +00:00
|
|
|
|
2020-02-29 22:53:54 +00:00
|
|
|
std::mutex eventQueueMutex;
|
2020-04-07 18:38:56 +00:00
|
|
|
std::deque<RouterEventPtr> eventQueue;
|
2020-02-26 20:12:14 +00:00
|
|
|
};
|
|
|
|
|
2020-03-07 01:20:11 +00:00
|
|
|
} // namespace tooling
|