lokinet/llarp/tooling/router_hive.hpp
2020-07-02 10:25:10 -06:00

96 lines
1.9 KiB
C++

#pragma once
#include <tooling/router_event.hpp>
#include <llarp.h>
#include <config/config.hpp>
#include <tooling/hive_context.hpp>
#include <vector>
#include <deque>
#include <thread>
#include <mutex>
struct llarp_config;
struct llarp_main;
namespace llarp
{
struct Context;
} // namespace llarp
namespace tooling
{
struct HiveRouter; // Hive's version of Router
struct RouterHive
{
using Context_ptr = std::shared_ptr<HiveContext>;
private:
void
StartRouters(bool isRelay);
void
AddRouter(const std::shared_ptr<llarp::Config>& config, bool isRelay);
/// safely visit router (asynchronously)
void
VisitRouter(Context_ptr ctx, std::function<void(Context_ptr)> visit);
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
NotifyEvent(RouterEventPtr event);
RouterEventPtr
GetNextEvent();
std::deque<RouterEventPtr>
GetAllEvents();
// functions to safely visit each relay and/or client's HiveContext
void
ForEachRelay(std::function<void(Context_ptr)> visit);
void
ForEachClient(std::function<void(Context_ptr)> visit);
void
ForEachRouter(std::function<void(Context_ptr)> visit);
HiveRouter*
GetRelay(const llarp::RouterID& id, bool needMutexLock = true);
std::vector<size_t>
RelayConnectedRelays();
std::vector<llarp::RouterContact>
GetRelayRCs();
std::mutex routerMutex;
std::unordered_map<llarp::RouterID, Context_ptr, llarp::RouterID::Hash> relays;
std::unordered_map<llarp::RouterID, Context_ptr, llarp::RouterID::Hash> clients;
std::vector<std::thread> routerMainThreads;
std::mutex eventQueueMutex;
std::deque<RouterEventPtr> eventQueue;
};
} // namespace tooling