Add specialized subclass of Router for Hive

pull/1312/head
Stephen Shelton 4 years ago
parent 2b2c41fdf6
commit 84c83a2400
No known key found for this signature in database
GPG Key ID: EE4BADACCE8B631C

@ -90,6 +90,14 @@ namespace llarp
bool
CallSafe(std::function<void(void)> f);
/// Creates a router. Can be overridden to allow a different class of router
/// to be created instead. Defaults to llarp::Router.
virtual std::unique_ptr<AbstractRouter>
makeRouter(
std::shared_ptr<llarp::thread::ThreadPool> worker,
llarp_ev_loop_ptr __netloop,
std::shared_ptr<Logic> logic);
#ifdef LOKINET_HIVE
void
InjectHive(tooling::RouterHive* hive);

@ -227,7 +227,11 @@ if(TESTNET)
endif()
if(WITH_HIVE)
target_sources(liblokinet PRIVATE tooling/router_hive.cpp)
target_sources(liblokinet PRIVATE
tooling/router_hive.cpp
tooling/hive_router.cpp
tooling/hive_context.cpp
)
endif()
target_link_libraries(liblokinet PUBLIC cxxopts lokinet-platform lokinet-util lokinet-cryptography)

@ -90,7 +90,7 @@ namespace llarp
crypto = std::make_unique<sodium::CryptoLibSodium>();
cryptoManager = std::make_unique<CryptoManager>(crypto.get());
router = std::make_unique<Router>(mainloop, logic);
router = makeRouter(worker, mainloop, logic);
nodedb = std::make_unique<llarp_nodedb>(
nodedb_dir, [r = router.get()](auto call) { r->QueueDiskIO(std::move(call)); });
@ -105,6 +105,15 @@ namespace llarp
throw std::runtime_error("Config::Setup() failed to load database");
}
std::unique_ptr<AbstractRouter>
Context::makeRouter(
std::shared_ptr<llarp::thread::ThreadPool> worker,
llarp_ev_loop_ptr netloop,
std::shared_ptr<Logic> logic)
{
return std::make_unique<Router>(worker, netloop, logic);
}
int
Context::Run(const RuntimeOptions& opts)
{

@ -49,7 +49,7 @@
namespace llarp
{
struct Router final : public AbstractRouter
struct Router : public AbstractRouter
{
llarp_time_t _lastPump = 0s;
bool ready;
@ -318,7 +318,7 @@ namespace llarp
explicit Router(llarp_ev_loop_ptr __netloop, std::shared_ptr<Logic> logic);
~Router() override;
virtual ~Router() override;
bool
HandleRecvLinkMessageBuffer(ILinkSession* from, const llarp_buffer_t& msg) override;

@ -0,0 +1,16 @@
#include <tooling/hive_context.hpp>
#include <tooling/hive_router.hpp>
namespace tooling
{
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);
}
} // namespace tooling

@ -0,0 +1,18 @@
#pragma once
#include <llarp.hpp>
namespace tooling
{
/// HiveContext is a subclass of llarp::Context which allows RouterHive to
/// perform custom behavior which might be undesirable in production code.
struct HiveContext : public llarp::Context
{
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;
};
} // namespace tooling

@ -0,0 +1,31 @@
#include <tooling/hive_router.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)
{
}
bool
HiveRouter::disableGossipingRC_TestingOnly()
{
return m_disableGossiping;
}
void
HiveRouter::disableGossiping()
{
m_disableGossiping = false;
}
void
HiveRouter::enableGossiping()
{
m_disableGossiping = true;
}
} // namespace tooling

@ -0,0 +1,34 @@
#pragma once
#include <router/router.hpp>
namespace tooling
{
/// HiveRouter is a subclass of Router which overrides specific behavior in
/// order to perform testing-related functions. It exists largely to prevent
/// this behavior (which may often be "dangerous") from leaking into release
/// code.
struct HiveRouter : public llarp::Router
{
HiveRouter(
std::shared_ptr<llarp::thread::ThreadPool> worker,
llarp_ev_loop_ptr netloop,
std::shared_ptr<llarp::Logic> logic);
virtual ~HiveRouter() = default;
/// Override logic to prevent base Router class from gossiping its RC.
virtual bool
disableGossipingRC_TestingOnly() override;
void
disableGossiping();
void
enableGossiping();
protected:
bool m_disableGossiping = false;
};
} // namespace tooling

@ -21,6 +21,8 @@ namespace llarp
namespace tooling
{
struct HiveRouter; // Hive's version of Router
struct RouterHive
{
using Context_ptr = std::shared_ptr<llarp::Context>;

@ -1,6 +1,7 @@
#include "common.hpp"
#include "router/abstractrouter.hpp"
#include "tooling/hive_router.hpp"
namespace llarp
{
@ -12,4 +13,16 @@ namespace llarp
.def("Stop", &AbstractRouter::Stop)
.def("peerDb", &AbstractRouter::peerDb);
}
} // namespace llarp
namespace tooling
{
void
HiveRouter_Init(py::module& mod)
{
py::class_<HiveRouter>(mod, "HiveRouter")
.def("disableGossiping", &HiveRouter::disableGossiping)
.def("enableGossiping", &HiveRouter::enableGossiping);
}
} // namespace tooling

Loading…
Cancel
Save