From 78193fc8f8b68efa7169ef43ec067be798c0c3d5 Mon Sep 17 00:00:00 2001 From: R4SAS Date: Wed, 8 Jun 2022 19:35:23 +0300 Subject: [PATCH] [daemon] WIP: use callbacks to work with daemon Signed-off-by: R4SAS --- daemon/Daemon.cpp | 4 ++++ libi2pd_webconsole/HTTPServer.cpp | 18 +++++++++++++----- libi2pd_webconsole/HTTPServer.h | 10 ++++++++++ libi2pd_webconsole/HTTPServerResources.h | 6 +++--- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/daemon/Daemon.cpp b/daemon/Daemon.cpp index c6e6465a..1dbeba3b 100644 --- a/daemon/Daemon.cpp +++ b/daemon/Daemon.cpp @@ -423,6 +423,10 @@ namespace util try { d.httpServer = std::unique_ptr(new i2p::http::HTTPServer(httpAddr, httpPort)); + d.httpServer->SetDaemonStop (std::bind (Daemon_Singleton::stop, this)); +#if ((!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID)) || defined(ANDROID_BINARY)) + d.httpServer->SetDaemonGracefulTimer (std::bind (Daemon_Singleton::stop, this)); +#endif d.httpServer->Start(); } catch (std::exception& ex) diff --git a/libi2pd_webconsole/HTTPServer.cpp b/libi2pd_webconsole/HTTPServer.cpp index 1ec0db67..cc5844be 100644 --- a/libi2pd_webconsole/HTTPServer.cpp +++ b/libi2pd_webconsole/HTTPServer.cpp @@ -27,15 +27,18 @@ #include "RouterContext.h" #include "ClientContext.h" #include "HTTPServer.h" -#include "Daemon.h" #include "util.h" #include "ECIESX25519AEADRatchetSession.h" #include "I18N.h" #ifdef WIN32_APP +#include "Daemon.h" #include "Win32App.h" #endif +// Inja template engine +#include "inja/inja.hpp" + // For image, style and info #include "version.h" #include "HTTPServerResources.h" @@ -270,8 +273,10 @@ namespace http { ShowNetworkStatus (s, i2p::context.GetStatusV6 ()); s << "
\r\n"; } + + // TODO: rewrite timer access #if ((!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID)) || defined(ANDROID_BINARY)) - if (auto remains = Daemon.gracefulShutdownInterval) { + if (auto remains = m_DaemonGracefulTimer) { s << "" << tr("Stopping in") << ": "; ShowUptime(s, remains); s << "
\r\n"; @@ -1240,8 +1245,9 @@ namespace http { else if (cmd == HTTP_COMMAND_SHUTDOWN_START) { i2p::context.SetAcceptsTunnels (false); + // TODO: rewrite timer access #if ((!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID)) || defined(ANDROID_BINARY)) - Daemon.gracefulShutdownInterval = 10*60; + if (m_DaemonGracefulTimer) m_DaemonGracefulTimer = 10 * 60; #elif defined(WIN32_APP) i2p::win32::GracefulShutdown (); #endif @@ -1249,16 +1255,18 @@ namespace http { else if (cmd == HTTP_COMMAND_SHUTDOWN_CANCEL) { i2p::context.SetAcceptsTunnels (true); + // TODO: rewrite timer access #if ((!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID)) || defined(ANDROID_BINARY)) - Daemon.gracefulShutdownInterval = 0; + if (m_DaemonGracefulTimer) m_DaemonGracefulTimer = 0; #elif defined(WIN32_APP) i2p::win32::StopGracefulShutdown (); #endif } else if (cmd == HTTP_COMMAND_SHUTDOWN_NOW) { + // TODO: rewrite stop command access #ifndef WIN32_APP - Daemon.running = false; + if (m_DaemonStop) m_DaemonStop(); #else i2p::win32::StopWin32App (); #endif diff --git a/libi2pd_webconsole/HTTPServer.h b/libi2pd_webconsole/HTTPServer.h index 8646253f..8f85421d 100644 --- a/libi2pd_webconsole/HTTPServer.h +++ b/libi2pd_webconsole/HTTPServer.h @@ -69,6 +69,11 @@ namespace http void Start (); void Stop (); + typedef std::function DaemonStop; + typedef int DaemonGracefulTimer; + void SetDaemonStop (const DaemonStop& f) { m_DaemonStop = f; }; + void SetDaemonGracefulTimer (const DaemonGracefulTimer& f) { m_DaemonGracefulTimer = f; }; + private: void Run (); @@ -85,6 +90,11 @@ namespace http boost::asio::io_service::work m_Work; boost::asio::ip::tcp::acceptor m_Acceptor; std::string m_Hostname; + + private: + + DaemonStop m_DaemonStop; + DaemonGracefulTimer m_DaemonGracefulTimer; }; //all the below functions are also used by Qt GUI, see mainwindow.cpp -> getStatusPageHtml diff --git a/libi2pd_webconsole/HTTPServerResources.h b/libi2pd_webconsole/HTTPServerResources.h index d2df1d32..d248c1c4 100644 --- a/libi2pd_webconsole/HTTPServerResources.h +++ b/libi2pd_webconsole/HTTPServerResources.h @@ -84,7 +84,7 @@ namespace http " border-radius: 5px; font-size: 12px; }\r\n" " button[type=submit] { padding: 5px 15px; background: transparent; border: 2px solid var(--main-link-color); cursor: pointer;\r\n" " border-radius: 5px; position: relative; height: 36px; display: -webkit-inline-box; margin-top: 10px; }\r\n" - "}\r\n" + "}\r\n"; // for external style sheet std::string externalCSS; @@ -124,7 +124,7 @@ namespace http
{% block content %}{% endblock %}
\ \ \ -" +"; const std::string pageMain = "{% extends \"base.html\" %} \ @@ -203,7 +203,7 @@ namespace http \ \ \ -{% endblock %}" +{% endblock %}"; } // http } // i2p