---
title: llarp/tooling/router_hive.hpp

---

# llarp/tooling/router_hive.hpp



## Namespaces

| Name           |
| -------------- |
| **[llarp](Namespaces/namespacellarp.md)** <br>[crypto.hpp] |
| **[tooling](Namespaces/namespacetooling.md)**  |

## Classes

|                | Name           |
| -------------- | -------------- |
| struct | **[tooling::RouterHive](Classes/structtooling_1_1RouterHive.md)**  |




## Source code

```cpp
#pragma once

#include "router_event.hpp"

#include <llarp.hpp>
#include <llarp/config/config.hpp>
#include <llarp/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);

    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> relays;
    std::unordered_map<llarp::RouterID, Context_ptr> clients;

    std::vector<std::thread> routerMainThreads;

    std::mutex eventQueueMutex;
    std::deque<RouterEventPtr> eventQueue;
  };

}  // namespace tooling
```


-------------------------------

Updated on 2023-04-27 at 04:17:18 +0000
