diff --git a/include/llarp.hpp b/include/llarp.hpp index a630708e2..5f22343e4 100644 --- a/include/llarp.hpp +++ b/include/llarp.hpp @@ -87,6 +87,14 @@ namespace llarp bool Configure(); + /// close async + void + Close(); + + /// wait until closed and done + void + Wait(); + /// call a function in logic thread /// return true if queued for calling /// return false if not queued for calling @@ -117,6 +125,7 @@ namespace llarp std::string configfile; std::string pidfile; + std::unique_ptr< std::promise< void > > closeWaiter; }; } // namespace llarp diff --git a/jni/lokinet_config.cpp b/jni/lokinet_config.cpp index 48db9be7f..c73cdee3c 100644 --- a/jni/lokinet_config.cpp +++ b/jni/lokinet_config.cpp @@ -14,8 +14,7 @@ extern "C" } JNIEXPORT void JNICALL - Java_network_loki_lokinet_LokinetConfig_Free(JNIEnv* env, jclass, - jobject buf) + Java_network_loki_lokinet_LokinetConfig_Free(JNIEnv* env, jclass, jobject buf) { llarp_config_free(FromBuffer< llarp_config >(env, buf)); } diff --git a/jni/lokinet_daemon.cpp b/jni/lokinet_daemon.cpp index 22f22f2cb..8db0324fb 100644 --- a/jni/lokinet_daemon.cpp +++ b/jni/lokinet_daemon.cpp @@ -31,7 +31,7 @@ extern "C" return JNI_FALSE; if(llarp_main_configure(ptr, config)) return JNI_TRUE; - return JNI_FALSE; + return llarp_main_setup(ptr) == 0 ? JNI_TRUE : JNI_FALSE; } JNIEXPORT jint JNICALL diff --git a/jni/lokinet_jni_common.hpp b/jni/lokinet_jni_common.hpp index d2753a6af..4ddea9095 100644 --- a/jni/lokinet_jni_common.hpp +++ b/jni/lokinet_jni_common.hpp @@ -23,8 +23,7 @@ VisitStringAsStringView(JNIEnv* env, jobject str, V visit) const size_t length = env->GetArrayLength(stringJbytes); jbyte* pBytes = env->GetByteArrayElements(stringJbytes, NULL); - T result = - visit(llarp::string_view((const char*)pBytes, length)); + T result = visit(llarp::string_view((const char*)pBytes, length)); env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT); env->DeleteLocalRef(stringJbytes); diff --git a/llarp/context.cpp b/llarp/context.cpp index 2c7197669..e32f27cc5 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -240,10 +240,34 @@ __ ___ ____ _ _ ___ _ _ ____ // run net io thread llarp::LogInfo("running mainloop"); llarp_ev_loop_run_single_process(mainloop, logic); - // waits for router graceful stop + if(closeWaiter) + { + // inform promise if called by Stop + closeWaiter->set_value(); + } return 0; } + void + Context::Close() + { + /// already closing + if(closeWaiter) + return; + if(CallSafe(std::bind(&Context::HandleSignal, this, SIGINT))) + closeWaiter = std::make_unique< std::promise< void > >(); + } + + void + Context::Wait() + { + if(closeWaiter) + { + closeWaiter->get_future().wait(); + closeWaiter.reset(); + } + } + bool Context::WritePIDFile() const { @@ -433,6 +457,14 @@ extern "C" return nullptr; } + bool + llarp_config_read_file(struct llarp_config *conf, const char *fname) + { + if(conf == nullptr) + return false; + return conf->impl.Load(fname); + } + bool llarp_config_load_file(const char *fname, struct llarp_config **conf) { @@ -565,6 +597,31 @@ extern "C" { return "default"; } + + void + llarp_main_stop(struct llarp_main *ptr) + { + if(ptr == nullptr) + return; + ptr->ctx->Close(); + ptr->ctx->Wait(); + } + + bool + llarp_main_configure(struct llarp_main *ptr, struct llarp_config *conf) + { + if(ptr == nullptr || conf == nullptr) + return false; + // give new config + ptr->ctx->config.reset(new llarp::Config(conf->impl)); + return ptr->ctx->Configure(); + } + + bool + llarp_main_is_running(struct llarp_main *ptr) + { + return ptr && ptr->ctx->router && ptr->ctx->router->IsRunning(); + } } llarp_main::llarp_main(llarp_config *conf) diff --git a/llarp/router/abstractrouter.hpp b/llarp/router/abstractrouter.hpp index 08fbf449e..556ad9469 100644 --- a/llarp/router/abstractrouter.hpp +++ b/llarp/router/abstractrouter.hpp @@ -132,6 +132,9 @@ namespace llarp virtual bool Run() = 0; + virtual bool + IsRunning() const = 0; + /// stop running the router logic gracefully virtual void Stop() = 0; diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 5e9f7fd58..21c48ebc3 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -1036,6 +1036,12 @@ namespace llarp return _running; } + bool + Router::IsRunning() const + { + return _running; + } + llarp_time_t Router::Uptime() const { diff --git a/llarp/router/router.hpp b/llarp/router/router.hpp index e2d94536e..4e85e5fe0 100644 --- a/llarp/router/router.hpp +++ b/llarp/router/router.hpp @@ -310,6 +310,9 @@ namespace llarp bool InitServiceNode(); + bool + IsRunning() const override; + /// return true if we are running in service node mode bool IsServiceNode() const;