Some progress, not rly sure what

This commit is contained in:
Leon Etienne (ubuntu wsl) 2020-09-27 13:06:04 +02:00
parent 96d11c80f3
commit cdd8eded30
12 changed files with 281 additions and 35 deletions

98
Tubio/ConsoleManager.cpp Normal file
View File

@ -0,0 +1,98 @@
#include "ConsoleManager.h"
void ConsoleManager::PrePreInit()
{
#ifdef _WIN
FreeConsole();
AllocConsole();
FILE* dummy;
freopen_s(&dummy, "CONOUT$", "wt", stdout);
freopen_s(&dummy, "CONOUT$", "wt", stderr);
freopen_s(&dummy, "CONOUT$", "rt", stdin);
consoleHandle = GetConsoleWindow();
#endif
return;
}
void ConsoleManager::PreInit()
{
log = new Logging::Logger("ConsoleManager");
isConsoleActive = XGConfig::general.show_console;
return;
}
void ConsoleManager::PostInit()
{
#ifdef _WIN
if (isConsoleActive)
{
ShowWindow(consoleHandle, SW_SHOW);
BringWindowToTop(consoleHandle);
}
else
{
ShowWindow(consoleHandle, SW_HIDE);
}
#endif
return;
}
bool ConsoleManager::ShowConsole()
{
#ifdef _WIN
if (!IsConsoleShown())
{
ShowWindow(consoleHandle, SW_SHOW);
BringWindowToTop(consoleHandle);
XGConfig::general.show_console = true;
isConsoleActive = true;
return true;
}
#endif
return false;
}
bool ConsoleManager::HideConsole()
{
#ifdef _WIN
if (IsConsoleShown())
{
ShowWindow(consoleHandle, SW_HIDE);
XGConfig::general.show_console = false;
isConsoleActive = false;
return true;
}
#endif
return false;
}
bool ConsoleManager::IsConsoleShown()
{
return isConsoleActive;
}
bool ConsoleManager::IsSupported()
{
#ifdef _WIN
return true;
#endif
return false;
}
void ConsoleManager::PostExit()
{
delete log;
log = nullptr;
return;
}
bool ConsoleManager::isConsoleActive;
Logging::Logger* ConsoleManager::log;
#ifdef _WIN
HWND ConsoleManager::consoleHandle;
#endif

35
Tubio/ConsoleManager.h Normal file
View File

@ -0,0 +1,35 @@
#pragma once
#include "XGConfig.h"
#include "Logger.h"
#undef _WIN
#ifdef _WIN
#include <Windows.h>
#endif
class ConsoleManager
{
public:
static void PrePreInit();
static void PreInit();
static void PostInit();
static void PostExit();
static bool ShowConsole();
static bool HideConsole();
static bool IsConsoleShown();
// Returns whether or not the current platform supports hiding the console
static bool IsSupported();
private:
static bool isConsoleActive;
static Logging::Logger* log;
#ifdef _WIN
static HWND consoleHandle;
#endif
};

View File

@ -83,14 +83,12 @@ std::string DownloadManager::QueueDownload(std::string url, DOWNLOAD_MODE mode)
queue.push_back(newDownload);
Save();
return tubioId;
}
void DownloadManager::Update()
{
//if (shouldSave) Save();
if (shouldSave) Save();
std::size_t numActiveDownloads = GetNumActiveDownloads();
@ -131,7 +129,7 @@ void DownloadManager::DownloadNext()
if (entry->mode == DOWNLOAD_MODE::VIDEO)
{
ss << "youtube-dl --newline --no-call-home --no-playlist --limit-rate " << XGConfig::downloader.max_dlrate_per_thread
<< " --no-mtime --no-cache-dir --format \"bestvideo[ext=mp4]+bestaudio\" --merge-output-format mp4"
<< " --no-mtime --no-cache-dir --format \"bestvideo[ext=mp4]+bestaudio/best[ext=mp4]/best\" --merge-output-format mp4"
<< " -o \"" << XGConfig::downloader.cachedir << "/download/" << entry->tubio_id
<< ".mp4\" " << entry->webpage_url << " > \"" << XGConfig::downloader.cachedir
<< "/dlprogbuf/" << entry->tubio_id << ".buf" << "\"";
@ -145,7 +143,8 @@ void DownloadManager::DownloadNext()
<< "/dlprogbuf/" << entry->tubio_id << ".buf" << "\"";
}
system(ss.str().c_str());
int returnCode = system(ss.str().c_str());
std::cout << returnCode << std::endl;
entry->status = DOWNLOAD_STATUS::FINISHED;
entry->download_progress = 100;
@ -187,6 +186,8 @@ void DownloadManager::UpdateDownloadProgressPercentages()
{
int newPercentage = std::stoi(ss.str());
queue[i].download_progress = newPercentage;
//if (newPercentage == 100) queue[i].status = DOWNLOAD_STATUS::FINISHED;
}
}
}
@ -238,6 +239,25 @@ void Downloader::DownloadManager::ClearDownloadCache()
void DownloadManager::Save()
{
log->cout << "Saving...";
log->Flush();
if (downloadThreads.size() > 0)
{
log->cout << "Waiting for active download threads to finish before saving...";
log->Flush();
for (std::size_t i = 0; i < downloadThreads.size(); i++)
{
downloadThreads[i]->join();
delete downloadThreads[i];
downloadThreads[i] = nullptr;
}
downloadThreads.clear();
log->cout << "All threads have finished. Now saving...";
log->Flush();
}
JsonArray arr;
for (std::size_t i = 0; i < queue.size(); i++)
{
@ -256,6 +276,9 @@ void DownloadManager::Save()
shouldSave = false;
log->cout << "Saved!";
log->Flush();
return;
}
@ -285,7 +308,7 @@ void DownloadManager::Load()
{
JsonBlock iter = cachedArr[i].AsJson;
DownloadEntry newEntry;
newEntry.download_progress = -1;
newEntry.download_progress = 100;
newEntry.status = DOWNLOAD_STATUS::FINISHED; // All saved entries are finished...
if ((iter.DoesExist("title")) && (iter["title"].GetDataType() == JDType::STRING))
@ -392,18 +415,7 @@ std::size_t Downloader::DownloadManager::GetNumActiveDownloads()
void DownloadManager::OnExit()
{
if (downloadThreads.size() > 0)
{
log->cout << "Waiting for active download threads to finish...";
log->Flush();
for (std::size_t i = 0; i < downloadThreads.size(); i++)
{
downloadThreads[i]->join();
delete downloadThreads[i];
downloadThreads[i] = nullptr;
}
}
Save();
// Clear dlprogbuf directory.
if (FileSystem::ExistsDirectory(XGConfig::downloader.cachedir + "/dlprogbuf"))
@ -411,8 +423,6 @@ void DownloadManager::OnExit()
FileSystem::DeleteDirectory(XGConfig::downloader.cachedir + "/dlprogbuf");
}
Save();
return;
}

View File

@ -6,6 +6,8 @@ using namespace Downloader;
Framework::Framework()
{
ConsoleManager::PrePreInit();
PreInit();
log = new Logger("Framework");
@ -18,6 +20,10 @@ Framework::Framework()
XGControl::keepServerRunning = true;
log = new Logger("Framework");
log->cout << "Started Tubio server successfully!";
log->Flush();
return;
}
@ -52,6 +58,7 @@ void Framework::PreInit()
{
LogHistory::PreInit();
XGConfig::PreInit();
ConsoleManager::PreInit();
RestQueryHandler::PreInit();
DownloadManager::PreInit();
@ -61,6 +68,7 @@ void Framework::PreInit()
void Framework::PostInit()
{
ConsoleManager::PostInit();
httpServer->PostInit();
return;
@ -80,6 +88,7 @@ void Framework::PostExit()
RestQueryHandler::PostExit();
LogHistory::PostExit();
DownloadManager::PostExit();
ConsoleManager::PostExit();
return;
}

View File

@ -3,6 +3,7 @@
#include "LogHistory.h"
#include "HttpServer.h"
#include "DownloadManager.h"
#include "ConsoleManager.h"
#include "XGControl.h"
#include "XGConfig.h"

View File

@ -60,7 +60,7 @@ bool HttpServer::InitWebServer()
void HttpServer::Update()
{
mg_mgr_poll(pMgr, XGConfig::httpServer.pollingRate);
mg_mgr_poll(pMgr, XGConfig::httpServer.polling_rate);
return;
}

View File

@ -31,6 +31,9 @@ bool RestQueryHandler::ProcessQuery(const std::string clientAdress, const Json&
else if (requestName == "fetch_queue") return FetchQueue(requestBody, responseBody, responseCode);
else if (requestName == "clear_download_cache") return ClearDownloadCache(requestBody, responseBody, responseCode);
else if (requestName == "foo") return Example_Foo(requestBody, responseBody, responseCode);
else if (requestName == "show_console") return ShowConsole(requestBody, responseBody, responseCode);
else if (requestName == "hide_console") return HideConsole(requestBody, responseBody, responseCode);
else if (requestName == "get_os_name") return GetOSName(requestBody, responseBody, responseCode);
@ -104,6 +107,10 @@ bool RestQueryHandler::ClearDownloadCache(const JsonBlock& request, JsonBlock& r
responseCode = OK;
responseBody.CloneFrom(RestResponseTemplates::GetByCode(OK));
DownloadManager::ClearDownloadCache();
log->cout << "Clearing download cache...";
log->Flush();
return true;
}
@ -111,7 +118,7 @@ bool RestQueryHandler::KillYourself(const JsonBlock& request, JsonBlock& respons
{
XGControl::keepServerRunning = false;
log->cout << "Shutting down server upon rest request...";
log->cout << "Shutting down server upon client request...";
log->Flush();
responseCode = OK;
@ -120,9 +127,63 @@ bool RestQueryHandler::KillYourself(const JsonBlock& request, JsonBlock& respons
return true;
}
bool RestQueryHandler::HideConsole(const JsonBlock& request, JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode)
{
if (ConsoleManager::IsSupported())
{
bool didAnythingChange = ConsoleManager::HideConsole();
responseCode = OK;
responseBody.CloneFrom(RestResponseTemplates::GetByCode(OK));
responseBody.Set("message") = (didAnythingChange) ? "Console is now hidden!" : "Console was already hidden!";
return true;
}
else
{
responseCode = NOT_IMPLEMENTED;
responseBody.CloneFrom(RestResponseTemplates::GetByCode(NOT_IMPLEMENTED));
responseBody.Set("message") = "This feature is currently only supported on Windows! Make sure to compile with preprocessor directive _WIN!";
return false;
}
}
bool RestQueryHandler::ShowConsole(const JsonBlock& request, JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode)
{
if (ConsoleManager::IsSupported())
{
bool didAnythingChange = ConsoleManager::ShowConsole();
responseCode = OK;
responseBody.CloneFrom(RestResponseTemplates::GetByCode(OK));
responseBody.Set("message") = (didAnythingChange) ? "Console is now shown!" : "Console was already shown!";
return true;
}
else
{
responseCode = NOT_IMPLEMENTED;
responseBody.CloneFrom(RestResponseTemplates::GetByCode(NOT_IMPLEMENTED));
responseBody.Set("message") = "This feature is currently only supported on Windows! Make sure to compile with preprocessor directive _WIN!";
return false;
}
}
bool RestQueryHandler::GetOSName(const JsonBlock& request, JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode)
{
std::string osName = "other";
#ifdef _WIN
osName = "Windows";
#elif __APPLE__ || __MACH__
osName = "Mac OSX";
#elif __linux__
osName = "Linux";
#elif __FreeBSD__
osName = "FreeBSD";
#elif __unix || __unix__
osName = "Unix";
#endif
responseCode = OK;
responseBody.CloneFrom(RestResponseTemplates::GetByCode(OK));
responseBody.Set("os_name") = osName;
return true;
}

View File

@ -4,6 +4,7 @@
#include "XGControl.h"
#include "Logger.h"
#include "DownloadManager.h"
#include "ConsoleManager.h"
namespace Rest
{
@ -22,6 +23,9 @@ namespace Rest
static bool QueueDownload(const JasonPP::JsonBlock& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode);
static bool ClearDownloadCache(const JasonPP::JsonBlock& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode);
static bool KillYourself(const JasonPP::JsonBlock& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode);
static bool HideConsole(const JasonPP::JsonBlock& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode);
static bool ShowConsole(const JasonPP::JsonBlock& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode);
static bool GetOSName(const JasonPP::JsonBlock& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode);
static bool ValidateField(const std::string name, const JasonPP::JDType type, const JasonPP::Json& checkThat, JasonPP::JsonBlock& putErrorResponseHere);

View File

@ -86,8 +86,9 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>JASONPP_RENDER_SORTED;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WIN;_WIN32;JASONPP_RENDER_SORTED;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@ -100,8 +101,9 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WIN;_WIN32;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@ -114,7 +116,7 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WIN;_WIN64;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
@ -129,8 +131,9 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WIN;_WIN64;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@ -140,6 +143,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="ConsoleManager.cpp" />
<ClCompile Include="DownloadManager.cpp" />
<ClCompile Include="FileSystem.cpp" />
<ClCompile Include="Framework.cpp" />
@ -155,6 +159,7 @@
<ClCompile Include="XGControl.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="ConsoleManager.h" />
<ClInclude Include="DownloadManager.h" />
<ClInclude Include="FileSystem.h" />
<ClInclude Include="Framework.h" />

View File

@ -57,6 +57,9 @@
<ClCompile Include="DownloadManager.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="ConsoleManager.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="JasonPP.hpp">
@ -98,5 +101,8 @@
<ClInclude Include="DownloadManager.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="ConsoleManager.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -33,7 +33,7 @@ bool XGConfig::IsJsonFieldValid(const JsonBlock& json, const std::string key, co
void XGConfig::InitializeDefaultValues()
{
httpServer.port = "6969";
httpServer.pollingRate = 100;
httpServer.polling_rate = 100;
httpServer.rootdir = "frontend";
logging.logfile_text = "log.txt";
@ -48,6 +48,8 @@ void XGConfig::InitializeDefaultValues()
downloader.loginCredentials.password = "";
downloader.loginCredentials.twofactor = "";
general.show_console = true;
return;
}
@ -70,9 +72,9 @@ void XGConfig::LoadFromJson(const JasonPP::JsonBlock& json)
httpServer.port = json.ShorthandGet("httpServer.port").AsString;
}
if (IsJsonFieldValid(json, "httpServer.pollingRate", JDType::INT))
if (IsJsonFieldValid(json, "httpServer.polling_rate", JDType::INT))
{
httpServer.pollingRate = json.ShorthandGet("httpServer.pollingRate").AsInt;
httpServer.polling_rate = json.ShorthandGet("httpServer.polling_rate").AsInt;
}
if (IsJsonFieldValid(json, "httpServer.rootdir", JDType::STRING))
@ -112,6 +114,11 @@ void XGConfig::LoadFromJson(const JasonPP::JsonBlock& json)
downloader.loginCredentials.twofactor = json.ShorthandGet("downloader.loginCredentials.twofactor").AsString;
}
if (IsJsonFieldValid(json, "general.show_console", JDType::BOOL))
{
general.show_console = json.ShorthandGet("general.show_console").AsBool;
}
return;
}
@ -120,7 +127,7 @@ JsonBlock XGConfig::CreateJson()
return JsonBlock({
Ele("httpServer", JsonBlock({
Ele("port", httpServer.port),
Ele("pollingRate", httpServer.pollingRate),
Ele("pollingRate", httpServer.polling_rate),
Ele("rootdir", httpServer.rootdir)
})),
Ele("logging", JsonBlock({
@ -137,6 +144,9 @@ JsonBlock XGConfig::CreateJson()
Ele("password", downloader.loginCredentials.password),
Ele("twofactor", downloader.loginCredentials.twofactor)
}))
})),
Ele("general", JsonBlock({
Ele("show_console", general.show_console)
}))
});
}
@ -249,4 +259,6 @@ void XGConfig::Load()
XGConfig::HttpServer XGConfig::httpServer;
XGConfig::Logging XGConfig::logging;
XGConfig::Downloader XGConfig::downloader;
XGConfig::General XGConfig::general;
::Logging::Logger* XGConfig::log;

View File

@ -13,7 +13,7 @@ public:
{
std::string port;
std::string rootdir;
int pollingRate;
int polling_rate;
};
struct Logging
{
@ -36,14 +36,19 @@ public:
int num_threads;
};
struct General
{
bool show_console;
};
static void PreInit();
static void Save();
static void PostExit();
static HttpServer httpServer;
static XGConfig::HttpServer httpServer;
static XGConfig::Logging logging;
static XGConfig::Downloader downloader;
static XGConfig::General general;
static ::Logging::Logger* log;