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-07-01 19:46:52 +00:00
|
|
|
#include <tooling/hive_context.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-06-25 21:59:42 +00:00
|
|
|
} // namespace llarp
|
2020-02-28 16:29:15 +00:00
|
|
|
|
2020-02-26 20:12:14 +00:00
|
|
|
namespace tooling
|
|
|
|
{
|
2020-06-29 15:44:13 +00:00
|
|
|
struct HiveRouter; // Hive's version of Router
|
|
|
|
|
2020-02-26 20:12:14 +00:00
|
|
|
struct RouterHive
|
|
|
|
{
|
2020-07-01 19:46:52 +00:00
|
|
|
using Context_ptr = std::shared_ptr<HiveContext>;
|
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-06-15 18:53:17 +00:00
|
|
|
StartRouters(bool isRelay);
|
2020-03-02 22:10:01 +00:00
|
|
|
|
|
|
|
void
|
2020-06-25 21:59:42 +00:00
|
|
|
AddRouter(const std::shared_ptr<llarp::Config>& config, bool isRelay);
|
2020-03-02 22:10:01 +00:00
|
|
|
|
2020-06-30 15:45:26 +00:00
|
|
|
/// safely visit router (asynchronously)
|
2020-03-02 22:10:01 +00:00
|
|
|
void
|
2020-06-30 15:45:26 +00:00
|
|
|
VisitRouter(Context_ptr ctx, 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-07-01 19:46:52 +00:00
|
|
|
// functions to safely visit each relay and/or client's HiveContext
|
2020-03-02 22:10:01 +00:00
|
|
|
void
|
2020-07-01 19:46:52 +00:00
|
|
|
ForEachRelay(std::function<void(Context_ptr)> visit);
|
2020-06-17 21:42:22 +00:00
|
|
|
void
|
2020-07-01 19:46:52 +00:00
|
|
|
ForEachClient(std::function<void(Context_ptr)> visit);
|
2020-06-17 21:42:22 +00:00
|
|
|
void
|
2020-07-01 19:46:52 +00:00
|
|
|
ForEachRouter(std::function<void(Context_ptr)> visit);
|
2020-02-26 20:12:14 +00:00
|
|
|
|
2020-07-01 19:46:52 +00:00
|
|
|
HiveRouter*
|
2020-06-24 20:07:15 +00:00
|
|
|
GetRelay(const llarp::RouterID& id, bool needMutexLock = true);
|
2020-03-02 22:10:01 +00:00
|
|
|
|
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-06-24 20:07:15 +00:00
|
|
|
std::mutex routerMutex;
|
2020-06-30 15:45:26 +00:00
|
|
|
std::unordered_map<llarp::RouterID, Context_ptr, llarp::RouterID::Hash> relays;
|
|
|
|
std::unordered_map<llarp::RouterID, Context_ptr, llarp::RouterID::Hash> 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
|