Browse Source

[daemon] WIP: use callbacks to work with daemon

Signed-off-by: R4SAS <r4sas@i2pmail.org>
webconsole-inja
R4SAS 2 years ago
parent
commit
78193fc8f8
Signed by: r4sas
GPG Key ID: 66F6C87B98EBCFE2
  1. 4
      daemon/Daemon.cpp
  2. 18
      libi2pd_webconsole/HTTPServer.cpp
  3. 10
      libi2pd_webconsole/HTTPServer.h
  4. 6
      libi2pd_webconsole/HTTPServerResources.h

4
daemon/Daemon.cpp

@ -423,6 +423,10 @@ namespace util
try try
{ {
d.httpServer = std::unique_ptr<i2p::http::HTTPServer>(new i2p::http::HTTPServer(httpAddr, httpPort)); d.httpServer = std::unique_ptr<i2p::http::HTTPServer>(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(); d.httpServer->Start();
} }
catch (std::exception& ex) catch (std::exception& ex)

18
libi2pd_webconsole/HTTPServer.cpp

@ -27,15 +27,18 @@
#include "RouterContext.h" #include "RouterContext.h"
#include "ClientContext.h" #include "ClientContext.h"
#include "HTTPServer.h" #include "HTTPServer.h"
#include "Daemon.h"
#include "util.h" #include "util.h"
#include "ECIESX25519AEADRatchetSession.h" #include "ECIESX25519AEADRatchetSession.h"
#include "I18N.h" #include "I18N.h"
#ifdef WIN32_APP #ifdef WIN32_APP
#include "Daemon.h"
#include "Win32App.h" #include "Win32App.h"
#endif #endif
// Inja template engine
#include "inja/inja.hpp"
// For image, style and info // For image, style and info
#include "version.h" #include "version.h"
#include "HTTPServerResources.h" #include "HTTPServerResources.h"
@ -270,8 +273,10 @@ namespace http {
ShowNetworkStatus (s, i2p::context.GetStatusV6 ()); ShowNetworkStatus (s, i2p::context.GetStatusV6 ());
s << "<br>\r\n"; s << "<br>\r\n";
} }
// TODO: rewrite timer access
#if ((!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID)) || defined(ANDROID_BINARY)) #if ((!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID)) || defined(ANDROID_BINARY))
if (auto remains = Daemon.gracefulShutdownInterval) { if (auto remains = m_DaemonGracefulTimer) {
s << "<b>" << tr("Stopping in") << ":</b> "; s << "<b>" << tr("Stopping in") << ":</b> ";
ShowUptime(s, remains); ShowUptime(s, remains);
s << "<br>\r\n"; s << "<br>\r\n";
@ -1240,8 +1245,9 @@ namespace http {
else if (cmd == HTTP_COMMAND_SHUTDOWN_START) else if (cmd == HTTP_COMMAND_SHUTDOWN_START)
{ {
i2p::context.SetAcceptsTunnels (false); i2p::context.SetAcceptsTunnels (false);
// TODO: rewrite timer access
#if ((!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID)) || defined(ANDROID_BINARY)) #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) #elif defined(WIN32_APP)
i2p::win32::GracefulShutdown (); i2p::win32::GracefulShutdown ();
#endif #endif
@ -1249,16 +1255,18 @@ namespace http {
else if (cmd == HTTP_COMMAND_SHUTDOWN_CANCEL) else if (cmd == HTTP_COMMAND_SHUTDOWN_CANCEL)
{ {
i2p::context.SetAcceptsTunnels (true); i2p::context.SetAcceptsTunnels (true);
// TODO: rewrite timer access
#if ((!defined(WIN32) && !defined(QT_GUI_LIB) && !defined(ANDROID)) || defined(ANDROID_BINARY)) #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) #elif defined(WIN32_APP)
i2p::win32::StopGracefulShutdown (); i2p::win32::StopGracefulShutdown ();
#endif #endif
} }
else if (cmd == HTTP_COMMAND_SHUTDOWN_NOW) else if (cmd == HTTP_COMMAND_SHUTDOWN_NOW)
{ {
// TODO: rewrite stop command access
#ifndef WIN32_APP #ifndef WIN32_APP
Daemon.running = false; if (m_DaemonStop) m_DaemonStop();
#else #else
i2p::win32::StopWin32App (); i2p::win32::StopWin32App ();
#endif #endif

10
libi2pd_webconsole/HTTPServer.h

@ -69,6 +69,11 @@ namespace http
void Start (); void Start ();
void Stop (); void Stop ();
typedef std::function<void ()> DaemonStop;
typedef int DaemonGracefulTimer;
void SetDaemonStop (const DaemonStop& f) { m_DaemonStop = f; };
void SetDaemonGracefulTimer (const DaemonGracefulTimer& f) { m_DaemonGracefulTimer = f; };
private: private:
void Run (); void Run ();
@ -85,6 +90,11 @@ namespace http
boost::asio::io_service::work m_Work; boost::asio::io_service::work m_Work;
boost::asio::ip::tcp::acceptor m_Acceptor; boost::asio::ip::tcp::acceptor m_Acceptor;
std::string m_Hostname; 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 //all the below functions are also used by Qt GUI, see mainwindow.cpp -> getStatusPageHtml

6
libi2pd_webconsole/HTTPServerResources.h

@ -84,7 +84,7 @@ namespace http
" border-radius: 5px; font-size: 12px; }\r\n" " 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" " 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" " border-radius: 5px; position: relative; height: 36px; display: -webkit-inline-box; margin-top: 10px; }\r\n"
"}\r\n" "}\r\n";
// for external style sheet // for external style sheet
std::string externalCSS; std::string externalCSS;
@ -124,7 +124,7 @@ namespace http
<div class=\"content\">{% block content %}{% endblock %}</div> \ <div class=\"content\">{% block content %}{% endblock %}</div> \
</div> \ </div> \
</body> \ </body> \
</html>" </html>";
const std::string pageMain = const std::string pageMain =
"{% extends \"base.html\" %} \ "{% extends \"base.html\" %} \
@ -203,7 +203,7 @@ namespace http
</tr> \ </tr> \
</tbody> \ </tbody> \
</table> \ </table> \
{% endblock %}" {% endblock %}";
} // http } // http
} // i2p } // i2p

Loading…
Cancel
Save