diff --git a/Tubio/Framework.cpp b/Tubio/Framework.cpp index 256b4f0..2c4dde4 100644 --- a/Tubio/Framework.cpp +++ b/Tubio/Framework.cpp @@ -80,6 +80,7 @@ void Framework::Update() httpServer->Update(); DownloadManager::Update(); LogHistory::Update(); + Idler::Update(); return; } diff --git a/Tubio/Framework.h b/Tubio/Framework.h index 20e26a1..953350d 100644 --- a/Tubio/Framework.h +++ b/Tubio/Framework.h @@ -4,6 +4,7 @@ #include "HttpServer.h" #include "DownloadManager.h" #include "ConsoleManager.h" +#include "Idler.h" #include "XGControl.h" #include "XGConfig.h" diff --git a/Tubio/HttpServer.cpp b/Tubio/HttpServer.cpp index 0771402..94f9b9e 100644 --- a/Tubio/HttpServer.cpp +++ b/Tubio/HttpServer.cpp @@ -78,6 +78,8 @@ void HttpServer::EventHandler(mg_connection* pNc, int ev, void* p) switch (ev) { case MG_EV_HTTP_REQUEST: + // Reset standby timer + XGControl::last_query_time = time(0); http_message* hpm = (http_message*)p; std::string requestedUri = FixUnterminatedString(hpm->uri.p, hpm->uri.len); @@ -224,7 +226,7 @@ bool HttpServer::IsConnectionAllowed(std::string peer_address, std::string& deni } // Whitelist is enabled, but peer is NOT whitelisted - denialReason = "Not whitelisted!"; + denialReason = std::string("Not whitelisted! Ask your tubio administrator to whitelist '") + peer_address + "' in order to gain access."; return false; } else // Whitelist is NOT enabled and only_allow_localhost is FALSE! diff --git a/Tubio/Idler.cpp b/Tubio/Idler.cpp new file mode 100644 index 0000000..762930c --- /dev/null +++ b/Tubio/Idler.cpp @@ -0,0 +1,21 @@ +#include "Idler.h" +#include +#define TIME_TO_FALL_ASLEEP MINUTES(5) +#define SLEEP_TIME SECONDS(3) + +void Idler::Update() +{ + // Has no request been made within 3 minutes? + if (time(0) - XGControl::last_query_time > TIME_TO_FALL_ASLEEP) + { + // Let the processor chill for a second. + // This should reduce the idling-cpu load to near 0% +#ifdef _WIN + std::cout << "Sleeping..." << std::endl; + Sleep(SLEEP_TIME * 1000); // Uses milliseconds + std::cout << "Waking..." << std::endl; +#else + usleep(SLEEP_TIME * 10000000); // Uses microseconds +#endif + } +} diff --git a/Tubio/Idler.h b/Tubio/Idler.h new file mode 100644 index 0000000..36dbdb6 --- /dev/null +++ b/Tubio/Idler.h @@ -0,0 +1,15 @@ +#pragma once +#include "XGControl.h" + +#ifdef _WIN +#include +#else +#include +#include +#endif + +class Idler +{ +public: + static void Update(); +}; diff --git a/Tubio/RestQueryHandler.cpp b/Tubio/RestQueryHandler.cpp index 36d83e0..f8c617d 100644 --- a/Tubio/RestQueryHandler.cpp +++ b/Tubio/RestQueryHandler.cpp @@ -124,7 +124,7 @@ bool RestQueryHandler::FetchSessionCache(const JsonBlock& request, JsonBlock& re #ifdef _WIN max_age = min(request["max_age"].AsInt, max_age); #else - max_age = std::min(request["max_age"].AsInt, max_age); + max_age = std::min(request["max_age"].AsInt, max_age); #endif } } diff --git a/Tubio/TimeUnits.h b/Tubio/TimeUnits.h new file mode 100644 index 0000000..acba9e1 --- /dev/null +++ b/Tubio/TimeUnits.h @@ -0,0 +1,11 @@ +#pragma once + +/* These are APPROXIMATIONS */ +/* UNITS ARE SECONDS */ +#define SECONDS(x) (x) +#define MINUTES(x) (SECONDS(60) * x) +#define HOURS(x) (MINUTES(60) * x) +#define DAYS(x) (HOURS(24) * x) +#define WEEKS(x) (DAYS(7) * x) +#define MONTHS(x) (WEEKS(4) * x) +#define YEARS(x) (MONTHS(12) * x) diff --git a/Tubio/Tubio.vcxproj b/Tubio/Tubio.vcxproj index 8c8a11b..36db03f 100644 --- a/Tubio/Tubio.vcxproj +++ b/Tubio/Tubio.vcxproj @@ -153,6 +153,7 @@ + @@ -170,12 +171,14 @@ + + diff --git a/Tubio/Tubio.vcxproj.filters b/Tubio/Tubio.vcxproj.filters index 1420e2d..b279a81 100644 --- a/Tubio/Tubio.vcxproj.filters +++ b/Tubio/Tubio.vcxproj.filters @@ -63,6 +63,9 @@ Quelldateien + + Quelldateien + @@ -110,5 +113,11 @@ Headerdateien + + Headerdateien + + + Headerdateien + \ No newline at end of file diff --git a/Tubio/Updater.cpp b/Tubio/Updater.cpp index caac927..9b5689b 100644 --- a/Tubio/Updater.cpp +++ b/Tubio/Updater.cpp @@ -14,6 +14,6 @@ std::string Updater::UpdateYoutubeDL() return "error code: 0x" + (JasonPP::Internal::Helpers::Base10_2_X(res, "0123456789abcdef")); } #else -#endif return "not implemented"; +#endif } diff --git a/Tubio/Updater.h b/Tubio/Updater.h index 6924be5..8c2d39f 100644 --- a/Tubio/Updater.h +++ b/Tubio/Updater.h @@ -1,9 +1,10 @@ #pragma once -#ifdef _WIN #include +#include "external_dependencies/leonetienne/JasonPP/JasonPP.hpp" // We need Internal::Helpers + +#ifdef _WIN #include #include -#include "external_dependencies/leonetienne/JasonPP/JasonPP.hpp" // We need Internal::Helpers #endif class Updater diff --git a/Tubio/XGControl.cpp b/Tubio/XGControl.cpp index 97fcd77..b3ce0c5 100644 --- a/Tubio/XGControl.cpp +++ b/Tubio/XGControl.cpp @@ -2,3 +2,4 @@ bool XGControl::keepServerRunning = false; time_t XGControl::boot_time = time(0); +time_t XGControl::last_query_time = time(0) - DAYS(1); // A day before to prevent it from "waking up" at boot diff --git a/Tubio/XGControl.h b/Tubio/XGControl.h index 3e4ba58..65a76c1 100644 --- a/Tubio/XGControl.h +++ b/Tubio/XGControl.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include "TimeUnits.h" /// /// Class to house control variables @@ -10,5 +11,6 @@ class XGControl public: static bool keepServerRunning; static time_t boot_time; + static time_t last_query_time; }; diff --git a/linux_build/build.sh b/linux_build/build.sh index b0c9316..2bed5ba 100644 --- a/linux_build/build.sh +++ b/linux_build/build.sh @@ -18,6 +18,8 @@ g++ \ ../Tubio/RestResponseTemplates.cpp \ ../Tubio/XGConfig.cpp \ ../Tubio/XGControl.cpp \ +../Tubio/Updater.cpp \ +../Tubio/Idler.cpp \ \ \ ../Tubio/external_dependencies/casenta/mongoose/mongoose.c \ diff --git a/linux_build/tubio.out b/linux_build/tubio.out index 25c1ee9..cfbcd17 100644 Binary files a/linux_build/tubio.out and b/linux_build/tubio.out differ