lokinet/llarp/tooling/router_hive.hpp

118 lines
2.2 KiB
C++
Raw Normal View History

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