From e183c47ec2db4e3bdd82a3fbbe81d2d61c64107a Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 17 Apr 2021 01:26:34 +0200 Subject: Add manga combined plugin timeout --- src/plugins/MangaCombined.cpp | 81 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 16 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/MangaCombined.cpp b/src/plugins/MangaCombined.cpp index 994bb47..5b80872 100644 --- a/src/plugins/MangaCombined.cpp +++ b/src/plugins/MangaCombined.cpp @@ -1,34 +1,83 @@ #include "../../plugins/MangaCombined.hpp" +#include "../../include/AsyncTask.hpp" namespace QuickMedia { + static const int SEARCH_TIMEOUT_MILLISECONDS = 6000; + MangaCombinedSearchPage::MangaCombinedSearchPage(Program *program, std::vector search_pages) : Page(program), search_pages(std::move(search_pages)) { } - static void result_items_add_thread_results(std::vector>> &search_threads, BodyItems &result_items) { - for(auto &search_thread : search_threads) { - BodyItems search_page_body_items = search_thread.second.get(); - if(search_page_body_items.empty()) - continue; + static void result_items_add_thread_results(std::vector>> &search_threads, BodyItems &result_items) { + std::vector> plugin_finished_state(search_threads.size()); + for(size_t i = 0; i < plugin_finished_state.size(); ++i) { + plugin_finished_state[i].first = search_threads[i].first; + } + + int accumulated_sleep_time = 0; + while(true) { + size_t finshed_plugin_index = 0; + for(auto &f : plugin_finished_state) { + f.second = false; + } + + for(auto &search_thread : search_threads) { + if(!search_thread.second.valid()) { + plugin_finished_state[finshed_plugin_index].second = true; + ++finshed_plugin_index; + continue; + } + + if(!search_thread.second.ready()) + continue; + + BodyItems search_page_body_items = search_thread.second.get(); + plugin_finished_state[finshed_plugin_index].second = true; + ++finshed_plugin_index; + if(search_page_body_items.empty()) + continue; + + auto title_item = BodyItem::create(""); + title_item->set_author("======================== " + search_thread.first->title + " ========================"); + result_items.push_back(std::move(title_item)); + + for(auto &new_body_item : search_page_body_items) { + new_body_item->userdata = search_thread.first->page.get(); + } + result_items.insert(result_items.end(), std::move_iterator(search_page_body_items.begin()), std::move_iterator(search_page_body_items.end())); + } + + size_t num_finished_plugins = 0; + for(auto &f : plugin_finished_state) { + if(f.second) + ++num_finished_plugins; + } - auto title_item = BodyItem::create(""); - title_item->set_author("======================== " + search_thread.first->title + " ========================"); - title_item->url = search_thread.first->service_name; + if(num_finished_plugins == search_threads.size()) + break; - result_items.push_back(std::move(title_item)); - for(auto &new_body_item : search_page_body_items) { - new_body_item->userdata = search_thread.first->page.get(); + int sleep_time_ms = 200; + std::this_thread::sleep_for(std::chrono::milliseconds(sleep_time_ms)); + accumulated_sleep_time += sleep_time_ms; + if(accumulated_sleep_time >= SEARCH_TIMEOUT_MILLISECONDS) { + for(auto &f : plugin_finished_state) { + if(!f.second) { + auto title_item = BodyItem::create(""); + title_item->set_author("======================== " + f.first->title + " timed out ========================"); + result_items.push_back(std::move(title_item)); + } + } + break; } - result_items.insert(result_items.end(), std::move_iterator(search_page_body_items.begin()), std::move_iterator(search_page_body_items.end())); } } SearchResult MangaCombinedSearchPage::search(const std::string &str, BodyItems &result_items) { - std::vector>> search_threads; + std::vector>> search_threads; for(auto &search_page : search_pages) { - search_threads.push_back(std::make_pair(&search_page, std::async(std::launch::async, [&str, &search_page]() { + search_threads.push_back(std::make_pair(&search_page, AsyncTask([&str, &search_page]() { BodyItems search_page_body_items; search_page.page->search(str, search_page_body_items); return search_page_body_items; @@ -40,9 +89,9 @@ namespace QuickMedia { } PluginResult MangaCombinedSearchPage::get_page(const std::string &str, int page, BodyItems &result_items) { - std::vector>> search_threads; + std::vector>> search_threads; for(auto &search_page : search_pages) { - search_threads.push_back(std::make_pair(&search_page, std::async(std::launch::async, [&str, page, &search_page]() { + search_threads.push_back(std::make_pair(&search_page, AsyncTask([&str, page, &search_page]() { BodyItems search_page_body_items; search_page.page->get_page(str, page, search_page_body_items); return search_page_body_items; -- cgit v1.2.3