Fixed a bit of bullshittery and added max_age and max_num controls to fetch_session_cache

This commit is contained in:
Leon Etienne (ubuntu wsl) committed 2020-09-28 13:02:10 +02:00
1 parent 01d52d1dce
commit c44d56f27b
9 files changed
+161 -61

No files matched your search

-1
View File
@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 721.01 378"><defs><style>.cls-1{fill:#010101;stroke:#1b344e;stroke-linecap:round;stroke-width:1.5px;}.cls-2{fill:#1e1e1c;}</style></defs><title>Element 2</title><g id="Ebene_2" data-name="Ebene 2"><g id="Ebene_1-2" data-name="Ebene 1"><g id="Curve"><path id="curva_3D.Curve" data-name="curva 3D.Curve" class="cls-1" d="M.75,97.49H20.6V62.88H40.76V97.49H74.23l-2.16,17.68H40.76L38.6,206.63l0,1.63,0,1.63.06,1.63.11,1.62.15,1.62.18,1.62.23,1.61.27,1.61.3,1.6.35,1.59.39,1.58.42,1.58.13.61.16.6.18.59.22.59.24.57.27.56.29.54.32.53.35.52.37.49.4.49.42.46.44.44.46.42.48.39.5.38.51.34.53.32.55.3.55.27.58.24.58.21.59.19.61.16.91.23.92.22.92.2.92.19.93.17.93.16.93.14.93.13.94.12.93.1.94.08.94.07,1.08.1,1,.08,1,.08.91.07.86.06.8,0,.74,0,.67,0,.61,0,.55,0h1.66l.58,0,.76,0,.91,0,1.07,0,1.23,0,1.37.05,1.53.05,1.67.06,1.82.06,2.09.13,2.29.16,2.47.19,2.67.23,2.86.27,3.06.31,3.25.35,3.44.4,3.63.43,3.83.49,4,.54,4.22.58.79.15.8.14.8.15.81.15.82.15.82.16.83.15.84.16.84.16.85.16.85.16.86.17.87.17.88.17.88.17.88.17.9.18.9.17.9.18.92.18.91.19.93.18.93.19.94.18.94.19.95.2,1,.19,1,.2,1,.2,1,.2,1,.21,1,.21,1,.21,1,.22,1,.22,1,.23,1.05.26,1.06.27,1.07.27,1.08.29,1.09.29,1.1.29,1.11.31,1.11.31,1.13.31,1.14.33,1.14.33,1.16.34,1.18.37,1.2.39,1.21.38,1.21.4,1.23.4,1.23.41,1.25.42,1.26.42,1.27.43,1.27.44,1.29.45,1.3.45,1.33.48,1.34.5,1.36.49,1.36.51,1.38.51,1.39.52,1.39.52,1.41.54,1.42.53,1.43.55,1.45.55,1.45.56,1.59.61,1.61.62,1.61.62,1.63.63,1.64.63,1.66.63,1.66.64,1.68.65,1.69.65,1.7.65,1.72.66,1.72.66,1.78.68,1.79.69,1.8.69,1.81.69,1.82.7,1.84.7,1.85.7,1.87.7,1.88.71,1.89.71,1.9.71,1.92.72,2,.72,2,.73,2,.72,2,.73,2,.73,2,.73,2,.73,2.06.74,2.08.73,2.08.73,2.1.74,2.12.73,2.16.73,2.18.74,2.2.73,2.21.74,2.22.74,2.24.75,2.26.75,2.26.76,2.29.77,2.3.79,2.31.79,2.34.81,2.42.95,2.44,1,2.47,1,2.48,1,2.51,1,2.53,1.05,2.54,1.08,2.57,1.09,2.59,1.12,2.61,1.13,2.63,1.15,2.65,1.17,2.76,1.28,2.79,1.29,2.81,1.31,2.84,1.31,2.86,1.33,2.88,1.35,2.9,1.35,2.93,1.37,3,1.38,3,1.39,3,1.4,3,1.42,3.18,1.38,3.19,1.38,3.21,1.39,3.24,1.38,3.25,1.39,3.28,1.38,3.3,1.38,3.32,1.38L386,339.6l3.37,1.38,3.38,1.37,3.41,1.36,3.54,1.32,3.56,1.3,3.58,1.28,3.6,1.27,3.62,1.26,3.65,1.24,3.66,1.22,3.68,1.21,3.7,1.18,3.72,1.17,3.74,1.16,3.76,1.13,3.88,1,3.89.95,3.91.91,3.92.88,3.94.85,3.95.82,4,.78,4,.76,4,.72,4,.69,4,.65,4,.62,4.1.35,4.11.31,4.12.27,4.11.22,4.13.17,4.12.13,4.13.09,4.14,0h4.13l4.14-.06,4.14-.1,4.14-.15,4.13-.49,4.12-.55,4.11-.59,4.1-.66,4.09-.7,4.08-.76,4.07-.81,4.05-.87,4-.92,4-1,4-1,4-1.08,3.88-1.46,3.84-1.52,3.82-1.58,3.79-1.62,3.77-1.69,3.74-1.73,3.7-1.79,3.68-1.85,3.65-1.9,3.62-2,3.59-2,3.56-2.06,3-2.28,3-2.32,2.95-2.36,2.92-2.39,2.87-2.42,2.83-2.46,2.8-2.49,2.75-2.53,2.7-2.56,2.67-2.59,2.63-2.62,2.58-2.65,2.33-2.89,2.27-2.9,2.24-2.92,2.18-2.95,2.14-3,2.1-3,2.05-3,2-3,1.95-3,1.92-3.07,1.87-3.08,1.82-3.1,1.54-3.24,1.49-3.25,1.45-3.25,1.41-3.26,1.35-3.26,1.32-3.27,1.27-3.28,1.22-3.28,1.18-3.29,1.14-3.29,1.09-3.29,1-3.3.78-3.35.73-3.34.7-3.33.65-3.33.61-3.32.58-3.32.53-3.31.5-3.3.45-3.29.42-3.28.38-3.28.33-3.26.11-3.24.07-3.23,0-3.21v-3.19l0-3.18-.06-3.16-.09-3.14-.13-3.13-.15-3.11-.18-3.09-.22-3.07-.24-3.06-.43-3-.46-3-.48-2.94-.5-2.91-.53-2.89-.55-2.87L706.8,136l-.59-2.83-.62-2.79-.64-2.78-.66-2.75-.68-2.73-.75-2.27-.76-2.25-.76-2.23-.78-2.21-.79-2.18-.8-2.17-.8-2.14-.82-2.12-.82-2.11-.83-2.08-.84-2.06-.85-2-.92-1.95-.92-1.93-.92-1.91-.93-1.89-.94-1.87-.93-1.85-1-1.83-.94-1.81-.95-1.79-1-1.77-1-1.75-1-1.73-1-1.64-1-1.63-1-1.61-1-1.59-1-1.58-1-1.55-1-1.54-1-1.52-1-1.5-1-1.48-1-1.46-1-1.45-1-1.37-1-1.35-1-1.34-1-1.32-1-1.3-1-1.29-1-1.27-1-1.25-1-1.24-1-1.22-1-1.2-1-1.19-1-1.14-1-1.13-1-1.12-1-1.12-1-1.11-1-1.1-1-1.1-1-1.08-1-1.08-1-1.06-1-1.06-1-1-1.13-1-1.14-1-1.15-.95-1.16-.92-1.16-.91-1.18-.89-1.18-.87-1.19-.86-1.2-.83-1.2-.81-1.22-.8L632,16.54l-1.1-.55-1.1-.55-1.11-.53-1.1-.51-1.11-.5-1.11-.49-1.11-.47-1.12-.46L622.06,12l-1.12-.43-1.12-.42-1.12-.4-1.15-.33-1.16-.31-1.15-.3-1.15-.29-1.15-.27L611.8,9l-1.14-.25-1.14-.24-1.14-.22-1.13-.22-1.13-.2L605,7.71l-1.13-.12-1.13-.12-1.12-.1-1.11-.1-1.11-.08-1.1-.08L597.21,7,596.13,7l-1.08-.05-1.07,0-1.07,0-1,0-1.05,0-1,0-1,0-1,0-1,0-1,.06-1,.07-1,.07-1,.07-1,.09-1,.08-.94.1L579,7.7l-.91.13-.9.14-.89.14-.88.14-.87.14-.85.14-.85.15-.84.15-.83.16-.82.16-.81.16-.78.18-.78.18-.76.18-.75.18-.74.18-.74.19-.72.18-.72.19-.7.18-.7.19-.68.19-.68.18-.54.17-.54.16-.53.16-.52.17-.52.16-.51.16-.51.16-.5.17-.5.16-.49.16-.49.16-.48.16-.46.16-.46.16-.45.16-.45.16-.44.16-.44.16-.43.16-.43.16-.42.15-.42.16-.41.15-.41.16-.39.15-.39.15-.38.15-.38.15-.38.15-.37.15-.37.15-.36.14-.36.15-.35.14-.35.14-.34.14-.34.14-.33.14-.32.14-.32.13-.32.14-.31.13-.31.13-.31.13-.3.13-.3.13-.29.13-.29.12-.28.13-.28.12-.27.12-.27.12-.27.12-.26.11-.26.12-.26.11-.25.12-.25.11-.25.11-.24.11-.24.1-.23.11-.23.1-.23.1-.22.11-.22.1-.22.1-.21.09-.21.1-.21.09-.21.1-.2.09-.79.46-1.73,1-2.48,1.52-3,1.88-3.42,2.12-3.62,2.23-3.62,2.24-3.44,2.12-3.06,1.88-2.51,1.53L506,39.38l-.83.47-.25.41.12.76.42,1,.66,1.27.83,1.43.92,1.49,1Line truncated

Before

Width:  |  Height:  |  Size: 11 KiB

+110 -48
View File
@@ -28,6 +28,7 @@ std::string DownloadManager::QueueDownload(std::string url, DOWNLOAD_MODE mode)
newDownload.tubio_id = tubioId; newDownload.tubio_id = tubioId;
newDownload.mode = mode; newDownload.mode = mode;
newDownload.download_progress = 0; newDownload.download_progress = 0;
newDownload.queued_timestamp = time(0);
newDownload.download_url = "/download/" + newDownload.tubio_id; newDownload.download_url = "/download/" + newDownload.tubio_id;
if (!IsJsonValid(jsString)) if (!IsJsonValid(jsString))
@@ -89,7 +90,7 @@ std::string DownloadManager::QueueDownload(std::string url, DOWNLOAD_MODE mode)
} }
} }
queue.push_back(newDownload); unfinishedCache.push_back(newDownload);
return tubioId; return tubioId;
} }
@@ -130,11 +131,11 @@ void DownloadManager::DownloadNext()
DownloadEntry* next = nullptr; DownloadEntry* next = nullptr;
for (std::size_t i = 0; i < queue.size(); i++) for (std::size_t i = 0; i < unfinishedCache.size(); i++)
{ {
if (queue[i].status == DOWNLOAD_STATUS::QUEUED) if (unfinishedCache[i].status == DOWNLOAD_STATUS::QUEUED)
{ {
next = &queue[i]; next = &unfinishedCache[i];
break; break;
} }
} }
@@ -200,11 +201,11 @@ void DownloadManager::DownloadNext()
void DownloadManager::UpdateDownloadProgressPercentages() void DownloadManager::UpdateDownloadProgressPercentages()
{ {
for (std::size_t i = 0; i < queue.size(); i++) for (std::size_t i = 0; i < unfinishedCache.size(); i++)
{ {
if (queue[i].status == DOWNLOAD_STATUS::DOWNLOADING) if (unfinishedCache[i].status == DOWNLOAD_STATUS::DOWNLOADING)
{ {
std::string filePath = XGConfig::downloader.cachedir + "/dlprogbuf/" + queue[i].tubio_id + ".buf"; std::string filePath = XGConfig::downloader.cachedir + "/dlprogbuf/" + unfinishedCache[i].tubio_id + ".buf";
if (FileSystem::Exists(filePath)) if (FileSystem::Exists(filePath))
{ {
std::ifstream ifs; std::ifstream ifs;
@@ -227,9 +228,9 @@ void DownloadManager::UpdateDownloadProgressPercentages()
if (ss.str().length() > 0) if (ss.str().length() > 0)
{ {
int newPercentage = std::stoi(ss.str()); int newPercentage = std::stoi(ss.str());
queue[i].download_progress = newPercentage; unfinishedCache[i].download_progress = newPercentage;
//if (newPercentage == 100) queue[i].status = DOWNLOAD_STATUS::FINISHED; if (newPercentage == 100) unfinishedCache[i].status = DOWNLOAD_STATUS::FINISHED;
} }
} }
} }
@@ -247,39 +248,79 @@ void DownloadManager::UpdateDownloadProgressPercentages()
std::size_t DownloadManager::GetQueueLength() std::size_t DownloadManager::GetQueueLength()
{ {
std::size_t counter = 0; std::size_t counter = 0;
for (std::size_t i = 0; i < queue.size(); i++) for (std::size_t i = 0; i < unfinishedCache.size(); i++)
{ {
if (queue[i].status == DOWNLOAD_STATUS::QUEUED) counter++; if (unfinishedCache[i].status == DOWNLOAD_STATUS::QUEUED) counter++;
} }
return counter; return counter;
} }
JsonArray DownloadManager::GetQueueAsJson() JsonArray DownloadManager::GetAlltimeCacheAsJson(time_t max_age, std::size_t max_num)
{ {
JsonArray arr; JsonArray arr;
for (std::size_t i = 0; i < queue.size(); i++) for (std::size_t i = 0; i < unfinishedCache.size(); i++)
{ {
arr += queue[i].GetAsJson(); arr += unfinishedCache[i].GetAsJson();
} }
return arr; arr.Merge(saveFileCache);
arr.Sort("queued_timestamp", JSON_ARRAY_SORT_MODE::NUM_DESC);
// Both limits are inifnite. Just return arr as is
if ((max_age == -1) && (max_num == (std::size_t)-1)) return arr;
JsonArray cutArr;
// If max_num is -1 (would mean inifnite) it would underflow to size_t::max
for (std::size_t i = 0; ((i < arr.Size()) && (i < max_num)); i++)
{
// If max_age is > 0, we have to check against the max age
if (max_age > 0)
{
if (arr[i].AsJson.DoesExist("queued_timestamp"))
{
if (arr[i]["queued_timestamp"].GetDataType() == JDType::INT)
{
if ((time(0) - arr[i]["queued_timestamp"].AsInt) < max_age)
{
cutArr += arr[i];
}
}
}
}
else // If not, just insert it
{
cutArr += arr[i];
}
}
return cutArr;
} }
bool DownloadManager::DoesTubioIDExist(std::string tubioId) bool DownloadManager::DoesTubioIDExist(std::string tubioId)
{ {
for (std::size_t i = 0; i < queue.size(); i++) for (std::size_t i = 0; i < unfinishedCache.size(); i++)
{ {
if (queue[i].tubio_id == tubioId) return true; if (unfinishedCache[i].tubio_id == tubioId) return true;
} }
for (std::size_t i = 0; i < saveFileCache_Atomic.size(); i++)
{
if (saveFileCache_Atomic[i].tubio_id == tubioId) return true;
}
return false; return false;
} }
DownloadEntry& DownloadManager::GetDownloadEntryByTubioID(std::string tubioId) DownloadEntry& DownloadManager::GetDownloadEntryByTubioID(std::string tubioId)
{ {
for (std::size_t i = 0; i < queue.size(); i++) for (std::size_t i = 0; i < unfinishedCache.size(); i++)
{ {
if (queue[i].tubio_id == tubioId) return queue[i]; if (unfinishedCache[i].tubio_id == tubioId) return unfinishedCache[i];
} }
for (std::size_t i = 0; i < saveFileCache_Atomic.size(); i++)
{
if (saveFileCache_Atomic[i].tubio_id == tubioId) return saveFileCache_Atomic[i];
}
throw std::exception("TubioID not found!"); throw std::exception("TubioID not found!");
std::terminate(); std::terminate();
} }
@@ -298,7 +339,9 @@ bool DownloadManager::ClearDownloadCache()
FileSystem::CreateDirectoryIfNotExists(XGConfig::downloader.cachedir + "/metadata"); FileSystem::CreateDirectoryIfNotExists(XGConfig::downloader.cachedir + "/metadata");
FileSystem::CreateDirectoryIfNotExists(XGConfig::downloader.cachedir + "/download"); FileSystem::CreateDirectoryIfNotExists(XGConfig::downloader.cachedir + "/download");
FileSystem::CreateDirectoryIfNotExists(XGConfig::downloader.cachedir + "/dlprogbuf"); FileSystem::CreateDirectoryIfNotExists(XGConfig::downloader.cachedir + "/dlprogbuf");
queue.clear(); unfinishedCache.clear();
saveFileCache.Clear();
saveFileCache_Atomic.clear();
} }
return true; return true;
@@ -331,21 +374,23 @@ void DownloadManager::Save()
log->Flush(); log->Flush();
} }
JsonArray arr; // Filecache has contain all and only nonsaved entries.
for (std::size_t i = 0; i < queue.size(); i++) for (long long int i = unfinishedCache.size() - 1; i >= 0; i--)
{ {
if (queue[i].status == DOWNLOAD_STATUS::FINISHED) if (unfinishedCache[i].status == DOWNLOAD_STATUS::FINISHED)
{ {
arr += queue[i].GetAsJson(); saveFileCache += unfinishedCache[i].GetAsJson();
unfinishedCache.erase(unfinishedCache.begin() + i);
} }
} }
Json j(arr); Json j(saveFileCache);
if (!FileSystem::WriteFile(XGConfig::downloader.cachedir + "/index.json", j.Render())) if (!FileSystem::WriteFile(XGConfig::downloader.cachedir + "/index.json", j.Render()))
{ {
log->cout << log->Err() << "Unable to save download cache index file!"; log->cout << log->Err() << "Unable to save download cache index file!";
log->Flush(); log->Flush();
} }
saveFileCache_Atomic = ParseJsonArrayToEntries(saveFileCache);
shouldSave = false; shouldSave = false;
@@ -375,11 +420,31 @@ void DownloadManager::Load()
if (j.GetDataType() == JDType::ARRAY) if (j.GetDataType() == JDType::ARRAY)
{ {
const JsonArray& cachedArr = j.AsArray; saveFileCache.CloneFrom(j.AsArray);
saveFileCache_Atomic = ParseJsonArrayToEntries(saveFileCache);
for (std::size_t i = 0; i < cachedArr.Size(); i++) }
else
{ {
JsonBlock iter = cachedArr[i].AsJson; log->cout << log->Err() << "Unable to parse download cache index file! Not json-type array!";
log->Flush();
}
}
else
{
log->cout << log->Err() << "Unable to parse download cache index file! Invalid json!";
log->Flush();
}
return;
}
std::vector<DownloadEntry> DownloadManager::ParseJsonArrayToEntries(const JasonPP::JsonArray& arr)
{
std::vector<DownloadEntry> entries;
for (std::size_t i = 0; i < arr.Size(); i++)
{
JsonBlock iter = arr[i].AsJson;
DownloadEntry newEntry; DownloadEntry newEntry;
newEntry.download_progress = 100; newEntry.download_progress = 100;
newEntry.status = DOWNLOAD_STATUS::FINISHED; // All saved entries are finished... newEntry.status = DOWNLOAD_STATUS::FINISHED; // All saved entries are finished...
@@ -429,6 +494,11 @@ void DownloadManager::Load()
newEntry.downloaded_filename = iter["downloaded_filename"]; newEntry.downloaded_filename = iter["downloaded_filename"];
} }
if ((iter.DoesExist("queued_timestamp")) && (iter["queued_timestamp"].GetDataType() == JDType::INT))
{
newEntry.queued_timestamp = iter["queued_timestamp"];
}
if ((iter.DoesExist("mode")) && (iter["mode"].GetDataType() == JDType::STRING)) if ((iter.DoesExist("mode")) && (iter["mode"].GetDataType() == JDType::STRING))
{ {
std::string cachedStrMode = iter["mode"]; std::string cachedStrMode = iter["mode"];
@@ -437,22 +507,10 @@ void DownloadManager::Load()
else newEntry.mode = DOWNLOAD_MODE::VIDEO; else newEntry.mode = DOWNLOAD_MODE::VIDEO;
} }
queue.push_back(newEntry); entries.push_back(newEntry);
}
}
else
{
log->cout << log->Err() << "Unable to parse download cache index file! Not json-type array!";
log->Flush();
}
}
else
{
log->cout << log->Err() << "Unable to parse download cache index file! Invalid json!";
log->Flush();
} }
return; return entries;
} }
void DownloadManager::FetchInformation(std::string url, std::string tubId) void DownloadManager::FetchInformation(std::string url, std::string tubId)
@@ -476,10 +534,11 @@ std::string DownloadManager::CreateNewTubioID()
newId = Internal::Helpers::Base10_2_X(time(0), "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); newId = Internal::Helpers::Base10_2_X(time(0), "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
isIdUnique = true; isIdUnique = true;
for (std::size_t i = 0; i < queue.size(); i++) if (unfinishedCache.size() > 0)
{ {
if (queue[i].tubio_id == newId) isIdUnique = false; isIdUnique = unfinishedCache[unfinishedCache.size() - 1].tubio_id != newId;
} }
counter++; counter++;
} }
@@ -489,9 +548,9 @@ std::string DownloadManager::CreateNewTubioID()
std::size_t DownloadManager::GetNumActiveDownloads() std::size_t DownloadManager::GetNumActiveDownloads()
{ {
std::size_t counter = 0; std::size_t counter = 0;
for (std::size_t i = 0; i < queue.size(); i++) for (std::size_t i = 0; i < unfinishedCache.size(); i++)
{ {
if (queue[i].status == DOWNLOAD_STATUS::DOWNLOADING) counter++; if (unfinishedCache[i].status == DOWNLOAD_STATUS::DOWNLOADING) counter++;
} }
return counter; return counter;
} }
@@ -519,8 +578,10 @@ void DownloadManager::PostExit()
} }
std::vector<DownloadEntry> DownloadManager::queue; std::vector<DownloadEntry> DownloadManager::unfinishedCache;
std::vector<std::thread*> DownloadManager::downloadThreads; std::vector<std::thread*> DownloadManager::downloadThreads;
JasonPP::JsonArray DownloadManager::saveFileCache;
std::vector<DownloadEntry> DownloadManager::saveFileCache_Atomic;
::Logging::Logger* DownloadManager::log; ::Logging::Logger* DownloadManager::log;
time_t DownloadManager::lastProgressCheck = 0; time_t DownloadManager::lastProgressCheck = 0;
bool DownloadManager::shouldSave = false; bool DownloadManager::shouldSave = false;
@@ -541,6 +602,7 @@ JsonBlock DownloadEntry::GetAsJson()
jb.Set(Ele("download_progress", download_progress)); jb.Set(Ele("download_progress", download_progress));
jb.Set(Ele("downloaded_filename", downloaded_filename)); jb.Set(Ele("downloaded_filename", downloaded_filename));
jb.Set(Ele("download_url", download_url)); jb.Set(Ele("download_url", download_url));
jb.Set(Ele("queued_timestamp", (long long int)queued_timestamp));
switch (mode) switch (mode)
{ {
+9 -3
View File
@@ -39,6 +39,7 @@ namespace Downloader
DOWNLOAD_STATUS status; DOWNLOAD_STATUS status;
DOWNLOAD_MODE mode; DOWNLOAD_MODE mode;
int download_progress; int download_progress;
time_t queued_timestamp;
JasonPP::JsonBlock GetAsJson(); JasonPP::JsonBlock GetAsJson();
@@ -67,10 +68,12 @@ namespace Downloader
static std::size_t GetQueueLength(); static std::size_t GetQueueLength();
/// <summary> /// <summary>
/// Will return the whole queue in json format /// Will return the whole cache in json format
/// </summary> /// </summary>
/// /// <param name="max_age">Maximum age of the entry in seconds. -1 = infinite</param>
/// /// <param name="max_num">Maximum of entries to fetch. -1 = infinite</param>
/// <returns></returns> /// <returns></returns>
static JasonPP::JsonArray GetQueueAsJson(); static JasonPP::JsonArray GetAlltimeCacheAsJson(time_t max_age, std::size_t max_num);
/// <summary> /// <summary>
/// Returns whether or not a tubio id exists /// Returns whether or not a tubio id exists
@@ -94,6 +97,7 @@ namespace Downloader
private: private:
static void Save(); static void Save();
static void Load(); static void Load();
static std::vector<DownloadEntry> ParseJsonArrayToEntries(const JasonPP::JsonArray& arr);
static void FetchInformation(std::string url, std::string tubId); static void FetchInformation(std::string url, std::string tubId);
static std::string CreateNewTubioID(); static std::string CreateNewTubioID();
@@ -106,8 +110,10 @@ namespace Downloader
static void DownloadNext(); static void DownloadNext();
static void UpdateDownloadProgressPercentages(); static void UpdateDownloadProgressPercentages();
static std::vector<DownloadEntry> queue; static std::vector<DownloadEntry> unfinishedCache;
static std::vector<std::thread*> downloadThreads; static std::vector<std::thread*> downloadThreads;
static JasonPP::JsonArray saveFileCache; // Content of the save-file
static std::vector<DownloadEntry> saveFileCache_Atomic; // Content of the save-file
static Logging::Logger* log; static Logging::Logger* log;
// This gets set by other threads // This gets set by other threads
static time_t lastProgressCheck; static time_t lastProgressCheck;
+1 -1
View File
@@ -177,7 +177,7 @@ void HttpServer::ServeDownloadeableResource(mg_connection* pNc, int ev, void* p,
else else
{ {
Json j; Json j;
j.CloneFrom(RestResponseTemplates::GetByCode(BAD_REQUEST, "Invalid file id!")); j.CloneFrom(RestResponseTemplates::GetByCode(BAD_REQUEST, "Invalid tubio id!"));
ServeStringToConnection(pNc, j.Render(), BAD_REQUEST); ServeStringToConnection(pNc, j.Render(), BAD_REQUEST);
} }
+36 -5
View File
@@ -28,7 +28,8 @@ bool RestQueryHandler::ProcessQuery(const std::string clientAdress, const Json&
if (requestName == "kill_yourself") return KillYourself(requestBody, responseBody, responseCode); if (requestName == "kill_yourself") return KillYourself(requestBody, responseBody, responseCode);
else if (requestName == "queue_download") return QueueDownload(requestBody, responseBody, responseCode); else if (requestName == "queue_download") return QueueDownload(requestBody, responseBody, responseCode);
else if (requestName == "fetch_queue") return FetchQueue(requestBody, responseBody, responseCode); else if (requestName == "fetch_session_cache") return FetchSessionCache(requestBody, responseBody, responseCode);
else if (requestName == "fetch_alltime_cache") return FetchAlltimeCache(requestBody, responseBody, responseCode);
else if (requestName == "clear_download_cache") return ClearDownloadCache(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 == "foo") return Example_Foo(requestBody, responseBody, responseCode);
else if (requestName == "show_console") return ShowConsole(requestBody, responseBody, responseCode); else if (requestName == "show_console") return ShowConsole(requestBody, responseBody, responseCode);
@@ -91,18 +92,48 @@ bool RestQueryHandler::QueueDownload(const JsonBlock& request, JsonBlock& respon
responseBody.Set("message") = "Download queued!"; responseBody.Set("message") = "Download queued!";
responseBody.Set("queue_position") = (long long int)DownloadManager::GetQueueLength(); responseBody.Set("queue_position") = (long long int)DownloadManager::GetQueueLength();
responseBody.Set("tubio_id") = tubId; responseBody.Set("tubio_id") = tubId;
responseBody.Set("queue") = DownloadManager::GetQueueAsJson(); JsonArray cache = DownloadManager::GetAlltimeCacheAsJson(time(0) - XGControl::boot_time, -1);
responseBody.Set("cache_size") = (long long int)cache.Size();
responseBody.Set("cache") = cache;
return true; return true;
} }
bool RestQueryHandler::FetchQueue(const JsonBlock& request, JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode) bool RestQueryHandler::FetchSessionCache(const JsonBlock& request, JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode)
{ {
log->cout << "Asking for queue..."; log->cout << "Asking for session cache...";
log->Flush();
time_t max_age = time(0) - XGControl::boot_time; // Default max_age is session length
std::size_t max_num = (std::size_t )-1; // Default max_num is infinite
JsonBlock dummy;
if (ValidateField("max_age", JDType::INT, request, dummy))
{
max_age = request["max_age"].AsInt;
}
if (ValidateField("max_num", JDType::INT, request, dummy))
{
max_num = request["max_num"].AsInt;
}
responseCode = OK;
responseBody.CloneFrom(RestResponseTemplates::GetByCode(OK));
JsonArray cache = DownloadManager::GetAlltimeCacheAsJson(max_age, max_num);
responseBody.Set("cache_size") = (long long int)cache.Size();
responseBody.Set("cache") = cache;
return true;
}
bool RestQueryHandler::FetchAlltimeCache(const JsonBlock& request, JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode)
{
log->cout << "Asking for whole cache...";
log->Flush(); log->Flush();
responseCode = OK; responseCode = OK;
responseBody.CloneFrom(RestResponseTemplates::GetByCode(OK)); responseBody.CloneFrom(RestResponseTemplates::GetByCode(OK));
responseBody.Set("queue") = DownloadManager::GetQueueAsJson(); JsonArray cache = DownloadManager::GetAlltimeCacheAsJson(-1, -1); // Get ALL the data
responseBody.Set("cache_size") = (long long int)cache.Size();
responseBody.Set("cache") = cache;
return true; return true;
} }
+2 -1
View File
@@ -19,7 +19,8 @@ namespace Rest
private: private:
static bool Example_Foo(const JasonPP::JsonBlock& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode); static bool Example_Foo(const JasonPP::JsonBlock& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode);
static bool FetchQueue(const JasonPP::JsonBlock& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode); static bool FetchSessionCache(const JasonPP::JsonBlock& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode);
static bool FetchAlltimeCache(const JasonPP::JsonBlock& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode);
static bool QueueDownload(const JasonPP::JsonBlock& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode); 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 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 KillYourself(const JasonPP::JsonBlock& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode);
+1
View File
@@ -1,3 +1,4 @@
#include "XGControl.h" #include "XGControl.h"
bool XGControl::keepServerRunning = false; bool XGControl::keepServerRunning = false;
time_t XGControl::boot_time = time(0);
+2
View File
@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <string> #include <string>
#include <ctime>
/// <summary> /// <summary>
/// Class to house control variables /// Class to house control variables
@@ -8,5 +9,6 @@ class XGControl
{ {
public: public:
static bool keepServerRunning; static bool keepServerRunning;
static time_t boot_time;
}; };
@@ -35,7 +35,6 @@ export default {
width: 50px; width: 50px;
stroke: theme("colors.purple-3"); stroke: theme("colors.purple-3");
fill: theme("colors.purple-3"); fill: theme("colors.purple-3");
-webkit-tap-highlight-color: #0000;
transition: transition:
stroke 0.2s, stroke 0.2s,
fill 0.2s, fill 0.2s,
@@ -55,7 +54,6 @@ export default {
width: 60px; width: 60px;
fill: theme("colors.purple-3"); fill: theme("colors.purple-3");
stroke: theme("colors.purple-3"); stroke: theme("colors.purple-3");
-webkit-tap-highlight-color: #0000;
transition: transition:
stroke 0.2s, stroke 0.2s,
fill 0.2s, fill 0.2s,