Added standby mode to reduce idling cpu load to near 0%
This commit is contained in:
parent
611e08340d
commit
486064cef3
@ -80,6 +80,7 @@ void Framework::Update()
|
||||
httpServer->Update();
|
||||
DownloadManager::Update();
|
||||
LogHistory::Update();
|
||||
Idler::Update();
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include "HttpServer.h"
|
||||
#include "DownloadManager.h"
|
||||
#include "ConsoleManager.h"
|
||||
#include "Idler.h"
|
||||
#include "XGControl.h"
|
||||
#include "XGConfig.h"
|
||||
|
||||
|
@ -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!
|
||||
|
21
Tubio/Idler.cpp
Normal file
21
Tubio/Idler.cpp
Normal file
@ -0,0 +1,21 @@
|
||||
#include "Idler.h"
|
||||
#include <iostream>
|
||||
#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
|
||||
}
|
||||
}
|
15
Tubio/Idler.h
Normal file
15
Tubio/Idler.h
Normal file
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
#include "XGControl.h"
|
||||
|
||||
#ifdef _WIN
|
||||
#include <Windows.h>
|
||||
#else
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
class Idler
|
||||
{
|
||||
public:
|
||||
static void Update();
|
||||
};
|
@ -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<long int>(request["max_age"].AsInt, max_age);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
11
Tubio/TimeUnits.h
Normal file
11
Tubio/TimeUnits.h
Normal file
@ -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)
|
@ -153,6 +153,7 @@
|
||||
<ClCompile Include="external_dependencies\leonetienne\JasonPP\JasonPP.cpp" />
|
||||
<ClCompile Include="FileSystem.cpp" />
|
||||
<ClCompile Include="Framework.cpp" />
|
||||
<ClCompile Include="Idler.cpp" />
|
||||
<ClCompile Include="Logger.cpp" />
|
||||
<ClCompile Include="LogHistory.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
@ -170,12 +171,14 @@
|
||||
<ClInclude Include="external_dependencies\leonetienne\JasonPP\JasonPP.hpp" />
|
||||
<ClInclude Include="FileSystem.h" />
|
||||
<ClInclude Include="Framework.h" />
|
||||
<ClInclude Include="Idler.h" />
|
||||
<ClInclude Include="Logger.h" />
|
||||
<ClInclude Include="LogHistory.h" />
|
||||
<ClInclude Include="LogTypes.h" />
|
||||
<ClInclude Include="RestQueryHandler.h" />
|
||||
<ClInclude Include="RestResponseTemplates.h" />
|
||||
<ClInclude Include="HttpServer.h" />
|
||||
<ClInclude Include="TimeUnits.h" />
|
||||
<ClInclude Include="Updater.h" />
|
||||
<ClInclude Include="XGConfig.h" />
|
||||
<ClInclude Include="XGControl.h" />
|
||||
|
@ -63,6 +63,9 @@
|
||||
<ClCompile Include="Updater.cpp">
|
||||
<Filter>Quelldateien</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Idler.cpp">
|
||||
<Filter>Quelldateien</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Logger.h">
|
||||
@ -110,5 +113,11 @@
|
||||
<ClInclude Include="Updater.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Idler.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="TimeUnits.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -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
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
#pragma once
|
||||
#ifdef _WIN
|
||||
#include <string>
|
||||
#include "external_dependencies/leonetienne/JasonPP/JasonPP.hpp" // We need Internal::Helpers
|
||||
|
||||
#ifdef _WIN
|
||||
#include <urlmon.h>
|
||||
#include <Windows.h>
|
||||
#include "external_dependencies/leonetienne/JasonPP/JasonPP.hpp" // We need Internal::Helpers
|
||||
#endif
|
||||
|
||||
class Updater
|
||||
|
@ -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
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <ctime>
|
||||
#include "TimeUnits.h"
|
||||
|
||||
/// <summary>
|
||||
/// 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;
|
||||
};
|
||||
|
||||
|
@ -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 \
|
||||
|
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user