Introduce --background to only start JSON RPC

fixes #853
pull/855/head
Michael 5 years ago
parent 1af9ae255e
commit 15cb49c9bd
No known key found for this signature in database
GPG Key ID: 2D51757B47E2434C

@ -83,7 +83,7 @@ resolvePath(std::string conffname)
/// this sets up, configures and runs the main context /// this sets up, configures and runs the main context
static void static void
run_main_context(std::string conffname, bool multiThreaded, bool debugMode) run_main_context(std::string conffname, bool multiThreaded, bool backgroundMode)
{ {
// this is important, can downgrade from Info though // this is important, can downgrade from Info though
llarp::LogDebug("Running from: ", fs::current_path().string()); llarp::LogDebug("Running from: ", fs::current_path().string());
@ -97,10 +97,10 @@ run_main_context(std::string conffname, bool multiThreaded, bool debugMode)
#ifndef _WIN32 #ifndef _WIN32
signal(SIGHUP, handle_signal); signal(SIGHUP, handle_signal);
#endif #endif
code = llarp_main_setup(ctx, debugMode); code = llarp_main_setup(ctx);
llarp::util::SetThreadName("llarp-mainloop"); llarp::util::SetThreadName("llarp-mainloop");
if(code == 0) if(code == 0)
code = llarp_main_run(ctx); code = llarp_main_run(ctx, backgroundMode);
llarp_main_free(ctx); llarp_main_free(ctx);
} }
exit_code.set_value(code); exit_code.set_value(code);
@ -140,16 +140,16 @@ main(int argc, char *argv[])
("r,router", "generate router config", cxxopts::value<bool>()) ("r,router", "generate router config", cxxopts::value<bool>())
("f,force", "overwrite", cxxopts::value<bool>()) ("f,force", "overwrite", cxxopts::value<bool>())
("c,colour", "colour output", cxxopts::value<bool>()->default_value("true")) ("c,colour", "colour output", cxxopts::value<bool>()->default_value("true"))
("d,debug", "debug mode - UNENCRYPTED TRAFFIC", cxxopts::value<bool>()) ("b,background", "background mode (start, but do not connect to the network)", cxxopts::value<bool>())
("config","path to configuration file", cxxopts::value<std::string>()); ("config","path to configuration file", cxxopts::value<std::string>());
options.parse_positional("config"); options.parse_positional("config");
// clang-format on // clang-format on
bool genconfigOnly = false; bool genconfigOnly = false;
bool asRouter = false; bool asRouter = false;
bool overWrite = false; bool overWrite = false;
bool debugMode = false; bool backgroundMode = false;
std::string conffname; // suggestions: confFName? conf_fname? std::string conffname; // suggestions: confFName? conf_fname?
try try
@ -185,9 +185,9 @@ main(int argc, char *argv[])
genconfigOnly = true; genconfigOnly = true;
} }
if(result.count("debug") > 0) if(result.count("background") > 0)
{ {
debugMode = true; backgroundMode = true;
} }
if(result.count("force") > 0) if(result.count("force") > 0)
@ -319,7 +319,7 @@ main(int argc, char *argv[])
} }
std::thread main_thread{ std::thread main_thread{
std::bind(&run_main_context, conffname, multiThreaded, debugMode)}; std::bind(&run_main_context, conffname, multiThreaded, backgroundMode)};
auto ftr = exit_code.get_future(); auto ftr = exit_code.get_future();
do do
{ {

@ -41,11 +41,11 @@ extern "C"
/// setup main context, returns 0 on success /// setup main context, returns 0 on success
int int
llarp_main_setup(struct llarp_main *ptr, bool debugMode); llarp_main_setup(struct llarp_main *ptr);
/// run main context, returns 0 on success, blocks until program end /// run main context, returns 0 on success, blocks until program end
int int
llarp_main_run(struct llarp_main *ptr); llarp_main_run(struct llarp_main *ptr, bool backgroundMode);
/// free main context and end all operations /// free main context and end all operations
void void

@ -81,10 +81,10 @@ namespace llarp
GetDatabase(const byte_t *pk); GetDatabase(const byte_t *pk);
int int
Setup(bool debug = false); Setup();
int int
Run(); Run(bool daemonMode);
void void
HandleSignal(int sig); HandleSignal(int sig);

@ -192,40 +192,14 @@ __ ___ ____ _ _ ___ _ _ ____
} }
int int
Context::Setup(bool debug) Context::Setup()
{ {
llarp::LogInfo(LLARP_VERSION, " ", LLARP_RELEASE_MOTTO); llarp::LogInfo(LLARP_VERSION, " ", LLARP_RELEASE_MOTTO);
llarp::LogInfo("starting up"); llarp::LogInfo("starting up");
mainloop = llarp_make_ev_loop(); mainloop = llarp_make_ev_loop();
logic = std::make_shared< Logic >(); logic = std::make_shared< Logic >();
if(debug) crypto = std::make_unique< sodium::CryptoLibSodium >();
{
static std::string WARNING = R"(
__ ___ ____ _ _ ___ _ _ ____
\ \ / / \ | _ \| \ | |_ _| \ | |/ ___|
\ \ /\ / / _ \ | |_) | \| || || \| | | _
\ V V / ___ \| _ <| |\ || || |\ | |_| |
\_/\_/_/ \_\_| \_\_| \_|___|_| \_|\____|
This Lokinet session is not private!!
Sending traffic unencrypted!!
__ ___ ____ _ _ ___ _ _ ____
\ \ / / \ | _ \| \ | |_ _| \ | |/ ___|
\ \ /\ / / _ \ | |_) | \| || || \| | | _
\ V V / ___ \| _ <| |\ || || |\ | |_| |
\_/\_/_/ \_\_| \_\_| \_|___|_| \_|\____|
)";
std::cerr << WARNING << '\n';
crypto = std::make_unique< NoOpCrypto >();
}
else
{
crypto = std::make_unique< sodium::CryptoLibSodium >();
}
cryptoManager = std::make_unique< CryptoManager >(crypto.get()); cryptoManager = std::make_unique< CryptoManager >(crypto.get());
router = std::make_unique< Router >(worker, mainloop, logic); router = std::make_unique< Router >(worker, mainloop, logic);
@ -248,7 +222,7 @@ __ ___ ____ _ _ ___ _ _ ____
} }
int int
Context::Run() Context::Run(bool backgroundMode)
{ {
if(router == nullptr) if(router == nullptr)
{ {
@ -259,9 +233,15 @@ __ ___ ____ _ _ ___ _ _ ____
if(!WritePIDFile()) if(!WritePIDFile())
return 1; return 1;
// run // run
if(!router->Run(nodedb.get())) if(!router->StartJsonRpc())
return 1; return 1;
if(!backgroundMode)
{
if(!router->Run())
return 2;
}
// run net io thread // run net io thread
llarp::LogInfo("running mainloop"); llarp::LogInfo("running mainloop");
llarp_ev_loop_run_single_process(mainloop, logic); llarp_ev_loop_run_single_process(mainloop, logic);
@ -437,20 +417,20 @@ extern "C"
} }
int int
llarp_main_setup(struct llarp_main *ptr, bool debug) llarp_main_setup(struct llarp_main *ptr)
{ {
return ptr->ctx->Setup(debug); return ptr->ctx->Setup();
} }
int int
llarp_main_run(struct llarp_main *ptr) llarp_main_run(struct llarp_main *ptr, bool backgroundMode)
{ {
if(!ptr) if(!ptr)
{ {
llarp::LogError("No ptr passed in"); llarp::LogError("No ptr passed in");
return 1; return 1;
} }
return ptr->ctx->Run(); return ptr->ctx->Run(backgroundMode);
} }
void void

@ -127,7 +127,10 @@ namespace llarp
Configure(Config *conf, llarp_nodedb *nodedb) = 0; Configure(Config *conf, llarp_nodedb *nodedb) = 0;
virtual bool virtual bool
Run(struct llarp_nodedb *nodedb) = 0; StartJsonRpc() = 0;
virtual bool
Run() = 0;
/// stop running the router logic gracefully /// stop running the router logic gracefully
virtual void virtual void

@ -110,11 +110,18 @@ namespace llarp
util::StatusObject util::StatusObject
Router::ExtractStatus() const Router::ExtractStatus() const
{ {
return util::StatusObject{ if(_running)
{"dht", _dht->impl->ExtractStatus()}, {
{"services", _hiddenServiceContext.ExtractStatus()}, return util::StatusObject{
{"exit", _exitContext.ExtractStatus()}, {"dht", _dht->impl->ExtractStatus()},
{"links", _linkManager.ExtractStatus()}}; {"services", _hiddenServiceContext.ExtractStatus()},
{"exit", _exitContext.ExtractStatus()},
{"links", _linkManager.ExtractStatus()}};
}
else
{
return util::StatusObject{{"notRunning", true}};
}
} }
bool bool
@ -825,11 +832,10 @@ namespace llarp
} }
bool bool
Router::Run(struct llarp_nodedb *nodedb) Router::StartJsonRpc()
{ {
if(_running || _stopping) if(_running || _stopping)
return false; return false;
this->_nodedb = nodedb;
if(enableRPCServer) if(enableRPCServer)
{ {
@ -849,6 +855,16 @@ namespace llarp
} }
LogInfo("Bound RPC server to ", rpcBindAddr); LogInfo("Bound RPC server to ", rpcBindAddr);
} }
return true;
}
bool
Router::Run()
{
if(_running || _stopping)
return false;
if(whitelistRouters) if(whitelistRouters)
{ {
rpcCaller = std::make_unique< rpc::Caller >(this); rpcCaller = std::make_unique< rpc::Caller >(this);
@ -1001,7 +1017,7 @@ namespace llarp
_dht->impl->Nodes()->PutNode(rc); _dht->impl->Nodes()->PutNode(rc);
} }
LogInfo("have ", nodedb->num_loaded(), " routers"); LogInfo("have ", _nodedb->num_loaded(), " routers");
ScheduleTicker(1000); ScheduleTicker(1000);
_running.store(true); _running.store(true);

@ -327,7 +327,10 @@ namespace llarp
Configure(Config *conf, llarp_nodedb *nodedb = nullptr) override; Configure(Config *conf, llarp_nodedb *nodedb = nullptr) override;
bool bool
Run(struct llarp_nodedb *nodedb) override; StartJsonRpc() override;
bool
Run() override;
/// stop running the router logic gracefully /// stop running the router logic gracefully
void void

@ -195,6 +195,13 @@ namespace llarp
~Handler() override = default; ~Handler() override = default;
Response
StartRouter() const
{
const bool rc = router->Run();
return Response{{"status", rc}};
}
Response Response
DumpState() const DumpState() const
{ {
@ -271,6 +278,10 @@ namespace llarp
HandleJSONRPC(Method_t method, HandleJSONRPC(Method_t method,
ABSL_ATTRIBUTE_UNUSED const Params& params) override ABSL_ATTRIBUTE_UNUSED const Params& params) override
{ {
if(method == "llarp.admin.start")
{
return StartRouter();
}
if(method == "llarp.admin.link.neighboors") if(method == "llarp.admin.link.neighboors")
{ {
return ListNeighboors(); return ListNeighboors();

Loading…
Cancel
Save