Use inheritance to handle Hive injection

pull/1312/head
Stephen Shelton 4 years ago
parent b0d8568452
commit 552dcce5fd
No known key found for this signature in database
GPG Key ID: EE4BADACCE8B631C

@ -15,13 +15,6 @@
struct llarp_ev_loop; struct llarp_ev_loop;
#ifdef LOKINET_HIVE
namespace tooling
{
struct RouterHive;
} // namespace tooling
#endif
namespace llarp namespace llarp
{ {
class Logic; class Logic;
@ -97,11 +90,6 @@ namespace llarp
llarp_ev_loop_ptr __netloop, llarp_ev_loop_ptr __netloop,
std::shared_ptr<Logic> logic); std::shared_ptr<Logic> logic);
#ifdef LOKINET_HIVE
void
InjectHive(tooling::RouterHive* hive);
#endif
private: private:
void void
SigINT(); SigINT();

@ -204,14 +204,6 @@ namespace llarp
llarp::LogDebug("free logic"); llarp::LogDebug("free logic");
logic.reset(); logic.reset();
} }
#ifdef LOKINET_HIVE
void
Context::InjectHive(tooling::RouterHive* hive)
{
router->hive = hive;
}
#endif
} // namespace llarp } // namespace llarp
extern "C" extern "C"

@ -14,7 +14,7 @@
#include <peerstats/peer_db.hpp> #include <peerstats/peer_db.hpp>
#ifdef LOKINET_HIVE #ifdef LOKINET_HIVE
#include "tooling/router_hive.hpp" #include "tooling/router_event.hpp"
#endif #endif
struct llarp_buffer_t; struct llarp_buffer_t;
@ -292,14 +292,23 @@ namespace llarp
virtual void virtual void
GossipRCIfNeeded(const RouterContact rc) = 0; 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> template <class EventType, class... Params>
void void
NotifyRouterEvent([[maybe_unused]] Params&&... args) const NotifyRouterEvent([[maybe_unused]] Params&&... args) const
{ {
#ifdef LOKINET_HIVE // TODO: no-op when appropriate
hive->NotifyEvent(std::make_unique<EventType>(std::forward<Params>(args)...)); auto event = std::make_unique<EventType>(args...);
#endif 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 } // namespace llarp

@ -1240,4 +1240,11 @@ namespace llarp
LogDebug("Message failed sending to ", remote); LogDebug("Message failed sending to ", remote);
} }
} }
void
Router::HandleRouterEvent(tooling::RouterEventPtr event) const
{
LogDebug(event->ToString());
}
} // namespace llarp } // namespace llarp

@ -530,6 +530,9 @@ namespace llarp
MessageSent(const RouterID& remote, SendStatus status); MessageSent(const RouterID& remote, SendStatus status);
protected: protected:
virtual void
HandleRouterEvent(tooling::RouterEventPtr event) const override;
virtual bool virtual bool
disableGossipingRC_TestingOnly() disableGossipingRC_TestingOnly()
{ {

@ -4,13 +4,31 @@
namespace tooling namespace tooling
{ {
HiveContext::HiveContext(RouterHive* hive) : m_hive(hive)
{
}
std::unique_ptr<llarp::AbstractRouter> std::unique_ptr<llarp::AbstractRouter>
HiveContext::makeRouter( HiveContext::makeRouter(
std::shared_ptr<llarp::thread::ThreadPool> worker, std::shared_ptr<llarp::thread::ThreadPool> worker,
llarp_ev_loop_ptr netloop, llarp_ev_loop_ptr netloop,
std::shared_ptr<llarp::Logic> logic) 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 } // namespace tooling

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <llarp.hpp> #include <llarp.hpp>
#include <tooling/hive_router.hpp>
namespace tooling namespace tooling
{ {
@ -8,11 +9,23 @@ namespace tooling
/// perform custom behavior which might be undesirable in production code. /// perform custom behavior which might be undesirable in production code.
struct HiveContext : public llarp::Context struct HiveContext : public llarp::Context
{ {
HiveContext(RouterHive* hive);
std::unique_ptr<llarp::AbstractRouter> std::unique_ptr<llarp::AbstractRouter>
makeRouter( makeRouter(
std::shared_ptr<llarp::thread::ThreadPool> worker, std::shared_ptr<llarp::thread::ThreadPool> worker,
llarp_ev_loop_ptr netloop, llarp_ev_loop_ptr netloop,
std::shared_ptr<llarp::Logic> logic) override; 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 } // namespace tooling

@ -1,12 +1,15 @@
#include <tooling/hive_router.hpp> #include <tooling/hive_router.hpp>
#include <tooling/router_hive.hpp>
namespace tooling namespace tooling
{ {
HiveRouter::HiveRouter( HiveRouter::HiveRouter(
std::shared_ptr<llarp::thread::ThreadPool> worker, std::shared_ptr<llarp::thread::ThreadPool> worker,
llarp_ev_loop_ptr netloop, llarp_ev_loop_ptr netloop,
std::shared_ptr<llarp::Logic> logic) std::shared_ptr<llarp::Logic> logic,
: Router(worker, netloop, logic) RouterHive* hive)
: Router(worker, netloop, logic), m_hive(hive)
{ {
} }
@ -28,4 +31,10 @@ namespace tooling
m_disableGossiping = true; m_disableGossiping = true;
} }
void
HiveRouter::HandleRouterEvent(RouterEventPtr event) const
{
m_hive->NotifyEvent(std::move(event));
}
} // namespace tooling } // namespace tooling

@ -13,7 +13,8 @@ namespace tooling
HiveRouter( HiveRouter(
std::shared_ptr<llarp::thread::ThreadPool> worker, std::shared_ptr<llarp::thread::ThreadPool> worker,
llarp_ev_loop_ptr netloop, llarp_ev_loop_ptr netloop,
std::shared_ptr<llarp::Logic> logic); std::shared_ptr<llarp::Logic> logic,
RouterHive* hive);
virtual ~HiveRouter() = default; virtual ~HiveRouter() = default;
@ -29,6 +30,10 @@ namespace tooling
protected: protected:
bool m_disableGossiping = false; bool m_disableGossiping = false;
RouterHive* m_hive = nullptr;
virtual void
HandleRouterEvent(RouterEventPtr event) const override;
}; };
} // namespace tooling } // namespace tooling

@ -14,17 +14,19 @@ using namespace std::chrono_literals;
namespace tooling namespace tooling
{ {
void 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);
llarp::RuntimeOptions opts;
opts.isRouter = isRouter;
Context_ptr context = std::make_shared<llarp::Context>(); Context_ptr context = std::make_shared<HiveContext>(this);
context->config = std::make_unique<llarp::Config>(*config.get()); context->config = std::make_unique<llarp::Config>(*config.get());
context->Configure(isRelay, {}); context->Configure(opts, {});
context->Setup(isRelay); context->Setup(opts);
auto routerId = llarp::RouterID(context->router->pubkey()); auto routerId = llarp::RouterID(context->router->pubkey());
context->InjectHive(this);
container[routerId] = context; container[routerId] = context;
std::cout << "Generated router with ID " << routerId << std::endl; std::cout << "Generated router with ID " << routerId << std::endl;
} }
@ -150,7 +152,7 @@ namespace tooling
LogicCall(ctx->logic, [visit, ctx]() { visit(ctx); }); LogicCall(ctx->logic, [visit, ctx]() { visit(ctx); });
} }
llarp::AbstractRouter* HiveRouter*
RouterHive::GetRelay(const llarp::RouterID& id, bool needMutexLock) RouterHive::GetRelay(const llarp::RouterID& id, bool needMutexLock)
{ {
auto guard = auto guard =
@ -161,7 +163,7 @@ namespace tooling
return nullptr; return nullptr;
auto ctx = itr->second; auto ctx = itr->second;
return ctx->router.get(); return ctx->getRouterAsHiveRouter();
} }
std::vector<size_t> std::vector<size_t>
@ -218,34 +220,7 @@ namespace tooling
} }
void void
RouterHive::ForEachRelayRouter(std::function<void(llarp::AbstractRouter*)> visit) RouterHive::ForEachRelay(std::function<void(Context_ptr)> 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)
{ {
for (auto [routerId, ctx] : relays) for (auto [routerId, ctx] : relays)
{ {
@ -254,7 +229,7 @@ namespace tooling
} }
void void
RouterHive::ForEachClientContext(std::function<void(Context_ptr)> visit) RouterHive::ForEachClient(std::function<void(Context_ptr)> visit)
{ {
for (auto [routerId, ctx] : clients) for (auto [routerId, ctx] : clients)
{ {
@ -264,10 +239,10 @@ namespace tooling
/// safely visit every router context /// safely visit every router context
void void
RouterHive::ForEachRouterContext(std::function<void(Context_ptr)> visit) RouterHive::ForEachRouter(std::function<void(Context_ptr)> visit)
{ {
ForEachRelayContext(visit); ForEachRelay(visit);
ForEachClientContext(visit); ForEachClient(visit);
} }
} // namespace tooling } // namespace tooling

@ -4,6 +4,7 @@
#include <llarp.h> #include <llarp.h>
#include <config/config.hpp> #include <config/config.hpp>
#include <tooling/hive_context.hpp>
#include <vector> #include <vector>
#include <deque> #include <deque>
@ -16,7 +17,6 @@ struct llarp_main;
namespace llarp namespace llarp
{ {
struct Context; struct Context;
struct AbstractRouter;
} // namespace llarp } // namespace llarp
namespace tooling namespace tooling
@ -25,7 +25,7 @@ namespace tooling
struct RouterHive struct RouterHive
{ {
using Context_ptr = std::shared_ptr<llarp::Context>; using Context_ptr = std::shared_ptr<HiveContext>;
private: private:
void void
@ -65,22 +65,15 @@ namespace tooling
std::deque<RouterEventPtr> std::deque<RouterEventPtr>
GetAllEvents(); 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 void
ForEachRelayRouter(std::function<void(llarp::AbstractRouter*)> visit); ForEachRelay(std::function<void(Context_ptr)> visit);
void void
ForEachClientRouter(std::function<void(llarp::AbstractRouter*)> visit); ForEachClient(std::function<void(Context_ptr)> visit);
void void
ForEachRouterRouter(std::function<void(llarp::AbstractRouter*)> visit); ForEachRouter(std::function<void(Context_ptr)> visit);
void HiveRouter*
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*
GetRelay(const llarp::RouterID& id, bool needMutexLock = true); GetRelay(const llarp::RouterID& id, bool needMutexLock = true);
std::vector<size_t> std::vector<size_t>

@ -1,5 +1,6 @@
#include "common.hpp" #include "common.hpp"
#include <llarp.hpp> #include <llarp.hpp>
#include <tooling/hive_context.hpp>
#include <router/router.cpp> #include <router/router.cpp>
#include "llarp/handlers/pyhandler.hpp" #include "llarp/handlers/pyhandler.hpp"
namespace llarp namespace llarp
@ -9,7 +10,11 @@ namespace llarp
{ {
using Context_ptr = std::shared_ptr<Context>; using Context_ptr = std::shared_ptr<Context>;
py::class_<Context, Context_ptr>(mod, "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("Run", [](Context_ptr self) -> int { return self->Run(RuntimeOptions{}); })
.def("Stop", [](Context_ptr self) { self->CloseAsync(); }) .def("Stop", [](Context_ptr self) { self->CloseAsync(); })
.def("IsUp", &Context::IsUp) .def("IsUp", &Context::IsUp)

@ -19,12 +19,9 @@ namespace tooling
.def("StartRelays", &RouterHive::StartRelays) .def("StartRelays", &RouterHive::StartRelays)
.def("StartClients", &RouterHive::StartClients) .def("StartClients", &RouterHive::StartClients)
.def("StopAll", &RouterHive::StopRouters) .def("StopAll", &RouterHive::StopRouters)
.def("ForEachRelayContext", &RouterHive::ForEachRelayContext) .def("ForEachRelay", &RouterHive::ForEachRelay)
.def("ForEachClientContext", &RouterHive::ForEachClientContext) .def("ForEachClient", &RouterHive::ForEachClient)
.def("ForEachRouterContext", &RouterHive::ForEachRouterContext) .def("ForEachRouter", &RouterHive::ForEachRouter)
.def("ForEachRelayRouter", &RouterHive::ForEachRelayRouter)
.def("ForEachClientRouter", &RouterHive::ForEachClientRouter)
.def("ForEachRouterRouter", &RouterHive::ForEachRouterRouter)
.def("GetNextEvent", &RouterHive::GetNextEvent) .def("GetNextEvent", &RouterHive::GetNextEvent)
.def("GetAllEvents", &RouterHive::GetAllEvents) .def("GetAllEvents", &RouterHive::GetAllEvents)
.def("RelayConnectedRelays", &RouterHive::RelayConnectedRelays) .def("RelayConnectedRelays", &RouterHive::RelayConnectedRelays)

@ -87,7 +87,7 @@ def tally_rc_received_for_peer(hive, routerId):
numFound += stats.numDistinctRCsReceived numFound += stats.numDistinctRCsReceived
hive.ForEachRelayRouter(visit) hive.ForEachRelay(visit)
return numFound; return numFound;

Loading…
Cancel
Save