lokinet/llarp/tooling/router_hive.hpp

96 lines
1.9 KiB
C++
Raw Normal View History

#pragma once
#include "router_event.hpp"
#include <llarp.hpp>
#include <config/config.hpp>
#include <tooling/hive_context.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;
2020-06-25 21:59:42 +00:00
} // namespace llarp
2020-02-28 16:29:15 +00:00
namespace tooling
{
struct HiveRouter; // Hive's version of Router
struct RouterHive
{
using Context_ptr = std::shared_ptr<HiveContext>;
2020-03-07 01:20:11 +00:00
private:
void
StartRouters(bool isRelay);
void
2020-06-25 21:59:42 +00:00
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);
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();
// 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*
2020-06-24 20:07:15 +00:00
GetRelay(const llarp::RouterID& id, bool needMutexLock = true);
std::vector<size_t>
RelayConnectedRelays();
std::vector<llarp::RouterContact>
GetRelayRCs();
2020-06-24 20:07:15 +00:00
std::mutex routerMutex;
std::unordered_map<llarp::RouterID, Context_ptr> relays;
std::unordered_map<llarp::RouterID, Context_ptr> clients;
std::vector<std::thread> routerMainThreads;
std::mutex eventQueueMutex;
std::deque<RouterEventPtr> eventQueue;
};
2020-03-07 01:20:11 +00:00
} // namespace tooling