mirror of https://github.com/oxen-io/lokinet
Use inheritance to handle Hive injection
This commit is contained in:
parent
b0d8568452
commit
552dcce5fd
|
@ -15,13 +15,6 @@
|
|||
|
||||
struct llarp_ev_loop;
|
||||
|
||||
#ifdef LOKINET_HIVE
|
||||
namespace tooling
|
||||
{
|
||||
struct RouterHive;
|
||||
} // namespace tooling
|
||||
#endif
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
class Logic;
|
||||
|
@ -97,11 +90,6 @@ namespace llarp
|
|||
llarp_ev_loop_ptr __netloop,
|
||||
std::shared_ptr<Logic> logic);
|
||||
|
||||
#ifdef LOKINET_HIVE
|
||||
void
|
||||
InjectHive(tooling::RouterHive* hive);
|
||||
#endif
|
||||
|
||||
private:
|
||||
void
|
||||
SigINT();
|
||||
|
|
|
@ -204,14 +204,6 @@ namespace llarp
|
|||
llarp::LogDebug("free logic");
|
||||
logic.reset();
|
||||
}
|
||||
|
||||
#ifdef LOKINET_HIVE
|
||||
void
|
||||
Context::InjectHive(tooling::RouterHive* hive)
|
||||
{
|
||||
router->hive = hive;
|
||||
}
|
||||
#endif
|
||||
} // namespace llarp
|
||||
|
||||
extern "C"
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
#include <peerstats/peer_db.hpp>
|
||||
|
||||
#ifdef LOKINET_HIVE
|
||||
#include "tooling/router_hive.hpp"
|
||||
#include "tooling/router_event.hpp"
|
||||
#endif
|
||||
|
||||
struct llarp_buffer_t;
|
||||
|
@ -292,14 +292,23 @@ namespace llarp
|
|||
virtual void
|
||||
GossipRCIfNeeded(const RouterContact rc) = 0;
|
||||
|
||||
/// Templated convenience function to generate a RouterHive event and
|
||||
/// delegate to non-templated (and overridable) function for handling.
|
||||
template <class EventType, class... Params>
|
||||
void
|
||||
NotifyRouterEvent([[maybe_unused]] Params&&... args) const
|
||||
{
|
||||
#ifdef LOKINET_HIVE
|
||||
hive->NotifyEvent(std::make_unique<EventType>(std::forward<Params>(args)...));
|
||||
#endif
|
||||
// TODO: no-op when appropriate
|
||||
auto event = std::make_unique<EventType>(args...);
|
||||
HandleRouterEvent(std::move(event));
|
||||
}
|
||||
|
||||
protected:
|
||||
/// Virtual function to handle RouterEvent. HiveRouter overrides this in
|
||||
/// order to inject the event. The default implementation in Router simply
|
||||
/// logs it.
|
||||
virtual void
|
||||
HandleRouterEvent(tooling::RouterEventPtr event) const = 0;
|
||||
};
|
||||
} // namespace llarp
|
||||
|
||||
|
|
|
@ -1240,4 +1240,11 @@ namespace llarp
|
|||
LogDebug("Message failed sending to ", remote);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Router::HandleRouterEvent(tooling::RouterEventPtr event) const
|
||||
{
|
||||
LogDebug(event->ToString());
|
||||
}
|
||||
|
||||
} // namespace llarp
|
||||
|
|
|
@ -530,6 +530,9 @@ namespace llarp
|
|||
MessageSent(const RouterID& remote, SendStatus status);
|
||||
|
||||
protected:
|
||||
virtual void
|
||||
HandleRouterEvent(tooling::RouterEventPtr event) const override;
|
||||
|
||||
virtual bool
|
||||
disableGossipingRC_TestingOnly()
|
||||
{
|
||||
|
|
|
@ -4,13 +4,31 @@
|
|||
|
||||
namespace tooling
|
||||
{
|
||||
HiveContext::HiveContext(RouterHive* hive) : m_hive(hive)
|
||||
{
|
||||
}
|
||||
|
||||
std::unique_ptr<llarp::AbstractRouter>
|
||||
HiveContext::makeRouter(
|
||||
std::shared_ptr<llarp::thread::ThreadPool> worker,
|
||||
llarp_ev_loop_ptr netloop,
|
||||
std::shared_ptr<llarp::Logic> logic)
|
||||
{
|
||||
return std::make_unique<HiveRouter>(worker, netloop, logic);
|
||||
return std::make_unique<HiveRouter>(worker, netloop, logic, m_hive);
|
||||
}
|
||||
|
||||
HiveRouter*
|
||||
HiveContext::getRouterAsHiveRouter()
|
||||
{
|
||||
if (not router)
|
||||
return nullptr;
|
||||
|
||||
HiveRouter* hiveRouter = dynamic_cast<HiveRouter*>(router.get());
|
||||
|
||||
if (hiveRouter == nullptr)
|
||||
throw std::runtime_error("HiveContext has a router not of type HiveRouter");
|
||||
|
||||
return hiveRouter;
|
||||
}
|
||||
|
||||
} // namespace tooling
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <llarp.hpp>
|
||||
#include <tooling/hive_router.hpp>
|
||||
|
||||
namespace tooling
|
||||
{
|
||||
|
@ -8,11 +9,23 @@ namespace tooling
|
|||
/// perform custom behavior which might be undesirable in production code.
|
||||
struct HiveContext : public llarp::Context
|
||||
{
|
||||
HiveContext(RouterHive* hive);
|
||||
|
||||
std::unique_ptr<llarp::AbstractRouter>
|
||||
makeRouter(
|
||||
std::shared_ptr<llarp::thread::ThreadPool> worker,
|
||||
llarp_ev_loop_ptr netloop,
|
||||
std::shared_ptr<llarp::Logic> logic) override;
|
||||
|
||||
/// Get this context's router as a HiveRouter.
|
||||
///
|
||||
/// Returns nullptr if there is no router or throws an exception if the
|
||||
/// router is somehow not an instance of HiveRouter.
|
||||
HiveRouter*
|
||||
getRouterAsHiveRouter();
|
||||
|
||||
protected:
|
||||
RouterHive* m_hive = nullptr;
|
||||
};
|
||||
|
||||
} // namespace tooling
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
#include <tooling/hive_router.hpp>
|
||||
|
||||
#include <tooling/router_hive.hpp>
|
||||
|
||||
namespace tooling
|
||||
{
|
||||
HiveRouter::HiveRouter(
|
||||
std::shared_ptr<llarp::thread::ThreadPool> worker,
|
||||
llarp_ev_loop_ptr netloop,
|
||||
std::shared_ptr<llarp::Logic> logic)
|
||||
: Router(worker, netloop, logic)
|
||||
std::shared_ptr<llarp::Logic> logic,
|
||||
RouterHive* hive)
|
||||
: Router(worker, netloop, logic), m_hive(hive)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -28,4 +31,10 @@ namespace tooling
|
|||
m_disableGossiping = true;
|
||||
}
|
||||
|
||||
void
|
||||
HiveRouter::HandleRouterEvent(RouterEventPtr event) const
|
||||
{
|
||||
m_hive->NotifyEvent(std::move(event));
|
||||
}
|
||||
|
||||
} // namespace tooling
|
||||
|
|
|
@ -13,7 +13,8 @@ namespace tooling
|
|||
HiveRouter(
|
||||
std::shared_ptr<llarp::thread::ThreadPool> worker,
|
||||
llarp_ev_loop_ptr netloop,
|
||||
std::shared_ptr<llarp::Logic> logic);
|
||||
std::shared_ptr<llarp::Logic> logic,
|
||||
RouterHive* hive);
|
||||
|
||||
virtual ~HiveRouter() = default;
|
||||
|
||||
|
@ -29,6 +30,10 @@ namespace tooling
|
|||
|
||||
protected:
|
||||
bool m_disableGossiping = false;
|
||||
RouterHive* m_hive = nullptr;
|
||||
|
||||
virtual void
|
||||
HandleRouterEvent(RouterEventPtr event) const override;
|
||||
};
|
||||
|
||||
} // namespace tooling
|
||||
|
|
|
@ -14,17 +14,19 @@ using namespace std::chrono_literals;
|
|||
namespace tooling
|
||||
{
|
||||
void
|
||||
RouterHive::AddRouter(const std::shared_ptr<llarp::Config>& config, bool isRelay)
|
||||
RouterHive::AddRouter(const std::shared_ptr<llarp::Config>& config, bool isRouter)
|
||||
{
|
||||
auto& container = (isRelay ? relays : clients);
|
||||
auto& container = (isRouter ? relays : clients);
|
||||
|
||||
Context_ptr context = std::make_shared<llarp::Context>();
|
||||
llarp::RuntimeOptions opts;
|
||||
opts.isRouter = isRouter;
|
||||
|
||||
Context_ptr context = std::make_shared<HiveContext>(this);
|
||||
context->config = std::make_unique<llarp::Config>(*config.get());
|
||||
context->Configure(isRelay, {});
|
||||
context->Setup(isRelay);
|
||||
context->Configure(opts, {});
|
||||
context->Setup(opts);
|
||||
|
||||
auto routerId = llarp::RouterID(context->router->pubkey());
|
||||
context->InjectHive(this);
|
||||
container[routerId] = context;
|
||||
std::cout << "Generated router with ID " << routerId << std::endl;
|
||||
}
|
||||
|
@ -150,7 +152,7 @@ namespace tooling
|
|||
LogicCall(ctx->logic, [visit, ctx]() { visit(ctx); });
|
||||
}
|
||||
|
||||
llarp::AbstractRouter*
|
||||
HiveRouter*
|
||||
RouterHive::GetRelay(const llarp::RouterID& id, bool needMutexLock)
|
||||
{
|
||||
auto guard =
|
||||
|
@ -161,7 +163,7 @@ namespace tooling
|
|||
return nullptr;
|
||||
|
||||
auto ctx = itr->second;
|
||||
return ctx->router.get();
|
||||
return ctx->getRouterAsHiveRouter();
|
||||
}
|
||||
|
||||
std::vector<size_t>
|
||||
|
@ -218,34 +220,7 @@ namespace tooling
|
|||
}
|
||||
|
||||
void
|
||||
RouterHive::ForEachRelayRouter(std::function<void(llarp::AbstractRouter*)> visit)
|
||||
{
|
||||
std::lock_guard<std::mutex> guard{routerMutex};
|
||||
for (auto [routerId, ctx] : relays)
|
||||
{
|
||||
visit(GetRelay(routerId, false));
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
RouterHive::ForEachClientRouter(std::function<void(llarp::AbstractRouter*)> visit)
|
||||
{
|
||||
std::lock_guard<std::mutex> guard{routerMutex};
|
||||
for (auto [routerId, ctx] : clients)
|
||||
{
|
||||
visit(GetRelay(routerId, false));
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
RouterHive::ForEachRouterRouter(std::function<void(llarp::AbstractRouter*)> visit)
|
||||
{
|
||||
ForEachRelayRouter(visit);
|
||||
ForEachClientRouter(visit);
|
||||
}
|
||||
|
||||
void
|
||||
RouterHive::ForEachRelayContext(std::function<void(Context_ptr)> visit)
|
||||
RouterHive::ForEachRelay(std::function<void(Context_ptr)> visit)
|
||||
{
|
||||
for (auto [routerId, ctx] : relays)
|
||||
{
|
||||
|
@ -254,7 +229,7 @@ namespace tooling
|
|||
}
|
||||
|
||||
void
|
||||
RouterHive::ForEachClientContext(std::function<void(Context_ptr)> visit)
|
||||
RouterHive::ForEachClient(std::function<void(Context_ptr)> visit)
|
||||
{
|
||||
for (auto [routerId, ctx] : clients)
|
||||
{
|
||||
|
@ -264,10 +239,10 @@ namespace tooling
|
|||
|
||||
/// safely visit every router context
|
||||
void
|
||||
RouterHive::ForEachRouterContext(std::function<void(Context_ptr)> visit)
|
||||
RouterHive::ForEachRouter(std::function<void(Context_ptr)> visit)
|
||||
{
|
||||
ForEachRelayContext(visit);
|
||||
ForEachClientContext(visit);
|
||||
ForEachRelay(visit);
|
||||
ForEachClient(visit);
|
||||
}
|
||||
|
||||
} // namespace tooling
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include <llarp.h>
|
||||
#include <config/config.hpp>
|
||||
#include <tooling/hive_context.hpp>
|
||||
|
||||
#include <vector>
|
||||
#include <deque>
|
||||
|
@ -16,7 +17,6 @@ struct llarp_main;
|
|||
namespace llarp
|
||||
{
|
||||
struct Context;
|
||||
struct AbstractRouter;
|
||||
} // namespace llarp
|
||||
|
||||
namespace tooling
|
||||
|
@ -25,7 +25,7 @@ namespace tooling
|
|||
|
||||
struct RouterHive
|
||||
{
|
||||
using Context_ptr = std::shared_ptr<llarp::Context>;
|
||||
using Context_ptr = std::shared_ptr<HiveContext>;
|
||||
|
||||
private:
|
||||
void
|
||||
|
@ -65,22 +65,15 @@ namespace tooling
|
|||
std::deque<RouterEventPtr>
|
||||
GetAllEvents();
|
||||
|
||||
// functions to safely visit each relay and/or client's AbstractRouter or Context
|
||||
// functions to safely visit each relay and/or client's HiveContext
|
||||
void
|
||||
ForEachRelayRouter(std::function<void(llarp::AbstractRouter*)> visit);
|
||||
ForEachRelay(std::function<void(Context_ptr)> visit);
|
||||
void
|
||||
ForEachClientRouter(std::function<void(llarp::AbstractRouter*)> visit);
|
||||
ForEachClient(std::function<void(Context_ptr)> visit);
|
||||
void
|
||||
ForEachRouterRouter(std::function<void(llarp::AbstractRouter*)> visit);
|
||||
ForEachRouter(std::function<void(Context_ptr)> visit);
|
||||
|
||||
void
|
||||
ForEachRelayContext(std::function<void(Context_ptr)> visit);
|
||||
void
|
||||
ForEachClientContext(std::function<void(Context_ptr)> visit);
|
||||
void
|
||||
ForEachRouterContext(std::function<void(Context_ptr)> visit);
|
||||
|
||||
llarp::AbstractRouter*
|
||||
HiveRouter*
|
||||
GetRelay(const llarp::RouterID& id, bool needMutexLock = true);
|
||||
|
||||
std::vector<size_t>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include "common.hpp"
|
||||
#include <llarp.hpp>
|
||||
#include <tooling/hive_context.hpp>
|
||||
#include <router/router.cpp>
|
||||
#include "llarp/handlers/pyhandler.hpp"
|
||||
namespace llarp
|
||||
|
@ -9,7 +10,11 @@ namespace llarp
|
|||
{
|
||||
using Context_ptr = std::shared_ptr<Context>;
|
||||
py::class_<Context, Context_ptr>(mod, "Context")
|
||||
.def("Setup", [](Context_ptr self, bool isRelay) { self->Setup(isRelay); })
|
||||
.def(
|
||||
"Setup",
|
||||
[](Context_ptr self, bool isRouter) {
|
||||
self->Setup({false, false, isRouter});
|
||||
})
|
||||
.def("Run", [](Context_ptr self) -> int { return self->Run(RuntimeOptions{}); })
|
||||
.def("Stop", [](Context_ptr self) { self->CloseAsync(); })
|
||||
.def("IsUp", &Context::IsUp)
|
||||
|
|
|
@ -19,12 +19,9 @@ namespace tooling
|
|||
.def("StartRelays", &RouterHive::StartRelays)
|
||||
.def("StartClients", &RouterHive::StartClients)
|
||||
.def("StopAll", &RouterHive::StopRouters)
|
||||
.def("ForEachRelayContext", &RouterHive::ForEachRelayContext)
|
||||
.def("ForEachClientContext", &RouterHive::ForEachClientContext)
|
||||
.def("ForEachRouterContext", &RouterHive::ForEachRouterContext)
|
||||
.def("ForEachRelayRouter", &RouterHive::ForEachRelayRouter)
|
||||
.def("ForEachClientRouter", &RouterHive::ForEachClientRouter)
|
||||
.def("ForEachRouterRouter", &RouterHive::ForEachRouterRouter)
|
||||
.def("ForEachRelay", &RouterHive::ForEachRelay)
|
||||
.def("ForEachClient", &RouterHive::ForEachClient)
|
||||
.def("ForEachRouter", &RouterHive::ForEachRouter)
|
||||
.def("GetNextEvent", &RouterHive::GetNextEvent)
|
||||
.def("GetAllEvents", &RouterHive::GetAllEvents)
|
||||
.def("RelayConnectedRelays", &RouterHive::RelayConnectedRelays)
|
||||
|
|
|
@ -87,7 +87,7 @@ def tally_rc_received_for_peer(hive, routerId):
|
|||
|
||||
numFound += stats.numDistinctRCsReceived
|
||||
|
||||
hive.ForEachRelayRouter(visit)
|
||||
hive.ForEachRelay(visit)
|
||||
|
||||
return numFound;
|
||||
|
||||
|
|
Loading…
Reference in New Issue