From db9000f706c7f384b0ef0740a72f090b34475d8d Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 29 Aug 2018 12:10:34 +0200 Subject: [PATCH] Make the downloader use the aria2c wrapper instead of the aria2 library. --- include/downloader.h | 17 ++----- meson.build | 7 +-- src/config.h.in | 2 - src/downloader.cpp | 114 ++++++++++++++++--------------------------- src/manager.cpp | 2 +- 5 files changed, 48 insertions(+), 94 deletions(-) diff --git a/include/downloader.h b/include/downloader.h index a8b373cd6..066a0483c 100644 --- a/include/downloader.h +++ b/include/downloader.h @@ -21,15 +21,13 @@ #define KIWIX_DOWNLOADER_H #include -#ifdef ENABLE_LIBARIA2 -# include -#endif #include +#include namespace kiwix { - +class Aria2; struct DownloadedFile { DownloadedFile() : success(false) {} @@ -56,17 +54,8 @@ class Downloader DownloadedFile download(const std::string& url); private: - static pthread_mutex_t globalLock; - std::string tmpDir; -#ifdef ENABLE_LIBARIA2 - DownloadedFile* fileHandle; - aria2::Session* session; - static int downloadEventCallback(aria2::Session* session, - aria2::DownloadEvent event, - aria2::A2Gid gid, - void* userData); -#endif + std::unique_ptr mp_aria; }; } diff --git a/meson.build b/meson.build index 096b68943..a855be365 100644 --- a/meson.build +++ b/meson.build @@ -18,7 +18,6 @@ libicu_dep = dependency('icu-i18n', static:static_deps) libzim_dep = dependency('libzim', version : '>=4.0.0', static:static_deps) pugixml_dep = dependency('pugixml', static:static_deps) libcurl_dep = dependency('libcurl', static:static_deps) -libaria2_dep = dependency('libaria2', static:static_deps, required:false) ctpp2_include_path = '' has_ctpp2_dep = false @@ -79,7 +78,7 @@ endif xapian_dep = dependency('xapian-core', required:false, static:static_deps) -all_deps = [thread_dep, libicu_dep, libzim_dep, xapian_dep, pugixml_dep, libcurl_dep, libaria2_dep] +all_deps = [thread_dep, libicu_dep, libzim_dep, xapian_dep, pugixml_dep, libcurl_dep] if has_ctpp2_dep all_deps += [ctpp2_dep] endif @@ -89,7 +88,6 @@ inc = include_directories('include') conf = configuration_data() conf.set('VERSION', '"@0@"'.format(meson.project_version())) conf.set('ENABLE_CTPP2', has_ctpp2_dep) -conf.set('ENABLE_LIBARIA2', libaria2_dep.found()) if build_machine.system() == 'windows' extra_link_args = ['-lshlwapi', '-lwinmm'] @@ -104,9 +102,6 @@ subdir('src') subdir('test') pkg_requires = ['libzim', 'icu-i18n', 'pugixml', 'libcurl'] -if libaria2_dep.found() - pkg_requires += ['libaria2'] -endif if xapian_dep.found() pkg_requires += ['xapian-core'] endif diff --git a/src/config.h.in b/src/config.h.in index c6162d720..713be9c12 100644 --- a/src/config.h.in +++ b/src/config.h.in @@ -2,5 +2,3 @@ #mesondefine VERSION #mesondefine ENABLE_CTPP2 - -#mesondefine ENABLE_LIBARIA2 diff --git a/src/downloader.cpp b/src/downloader.cpp index e5a565b71..9edb29c58 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -20,101 +20,73 @@ #include "downloader.h" #include "common/pathTools.h" -#ifndef _WIN32 -# include -#endif +#include +#include + #include +#include "aria2.h" +#include "xmlrpc.h" +#include "common/otherTools.h" +#include + namespace kiwix { -pthread_mutex_t Downloader::globalLock = PTHREAD_MUTEX_INITIALIZER; - - /* Constructor */ -Downloader::Downloader() +Downloader::Downloader() : + mp_aria(new Aria2()) { -#ifdef ENABLE_LIBARIA2 - aria2::SessionConfig config; - config.downloadEventCallback = Downloader::downloadEventCallback; - config.userData = this; - tmpDir = makeTmpDirectory(); - aria2::KeyVals options; - options.push_back(std::pair("dir", tmpDir)); - session = aria2::sessionNew(options, config); -#endif } /* Destructor */ Downloader::~Downloader() { -#ifdef ENABLE_LIBARIA2 - aria2::sessionFinal(session); -#endif - rmdir(tmpDir.c_str()); } -#ifdef ENABLE_LIBARIA2 -int Downloader::downloadEventCallback(aria2::Session* session, - aria2::DownloadEvent event, - aria2::A2Gid gid, - void* userData) -{ - Downloader* downloader = static_cast(userData); - - auto fileHandle = downloader->fileHandle; - auto dh = aria2::getDownloadHandle(session, gid); - - if (!dh) { - return 0; +pugi::xml_node find_member_in_struct(pugi::xml_node struct_node, std::string member_name) { + for(auto member=struct_node.first_child(); member; member=member.next_sibling()) { + std::string _member_name = member.child("name").text().get(); + if (_member_name == member_name) { + return member.child("value"); + } } - - switch (event) { - case aria2::EVENT_ON_DOWNLOAD_COMPLETE: - { - if (dh->getNumFiles() > 0) { - auto f = dh->getFile(1); - fileHandle->path = f.path; - fileHandle->success = true; - } - } - break; - case aria2::EVENT_ON_DOWNLOAD_ERROR: - { - fileHandle->success = false; - } - break; - default: - break; - } - aria2::deleteDownloadHandle(dh); - return 0; + return pugi::xml_node(); } -#endif DownloadedFile Downloader::download(const std::string& url) { - pthread_mutex_lock(&globalLock); DownloadedFile fileHandle; -#ifdef ENABLE_LIBARIA2 try { std::vector uris = {url}; - aria2::KeyVals options; - aria2::A2Gid gid; - int ret; - DownloadedFile fileHandle; + std::vector status_key = {"status", "files"}; + std::string gid; - ret = aria2::addUri(session, &gid, uris, options); - if (ret < 0) { - std::cerr << "Failed to download" << std::endl; - } else { - this->fileHandle = &fileHandle; - aria2::run(session, aria2::RUN_DEFAULT); + gid = mp_aria->addUri(uris); + std::cerr << "gid is : " << gid << std::endl; + pugi::xml_document ret; + while(true) { + auto strStatus = mp_aria->tellStatus(gid, status_key); + MethodResponse response(strStatus); + auto structNode = response.getParams().getParam(0).getValue().getStruct(); + auto status = structNode.getMember("status").getValue().getAsS(); + std::cerr << "Status is " << status << std::endl; + if (status == "complete") { + fileHandle.success = true; + auto filesMember = structNode.getMember("files"); + auto fileStruct = filesMember.getValue().getArray().getValue(0).getStruct(); + fileHandle.path = fileStruct.getMember("path").getValue().getAsS(); + std::cerr << "FilePath is " << fileHandle.path << std::endl; + } else if (status == "error") { + fileHandle.success = false; + } else { + // [TODO] Be wise here. + std::this_thread::sleep_for(std::chrono::microseconds(100000)); + continue; + } + break; } - } catch (...) {}; - this->fileHandle = nullptr; - pthread_mutex_unlock(&globalLock); -#endif + } catch (...) { std::cerr << "waautena " << std::endl; }; return fileHandle; } diff --git a/src/manager.cpp b/src/manager.cpp index 446616995..313b0fc88 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -127,7 +127,7 @@ bool Manager::parseOpdsDom(const pugi::xml_document& doc, const std::string& url if (rel == "http://opds-spec.org/image/thumbnail") { auto faviconUrl = urlHost + linkNode.attribute("href").value(); - auto downloader = Downloader(); + Downloader downloader; auto fileHandle = downloader.download(faviconUrl); if (fileHandle.success) { auto content = getFileContent(fileHandle.path);