From 24a6896bf44dcda62c9bbf0b41eb61b178ba6954 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 1 May 2013 21:28:04 +0900 Subject: [PATCH] Add sessionConfigSetKeepRunning and shutdown API function Setting sessionConfigSetKeepRunning to true makes aria2 core keep running even if there is no download to perform, just like --enable-rpc option. --- src/RequestGroupMan.cc | 6 +++--- src/RequestGroupMan.h | 10 ++++++++-- src/aria2api.cc | 19 +++++++++++++++++++ src/includes/aria2/aria2.h | 22 ++++++++++++++++++++-- 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/RequestGroupMan.cc b/src/RequestGroupMan.cc index fb8b4994..54cfe5be 100644 --- a/src/RequestGroupMan.cc +++ b/src/RequestGroupMan.cc @@ -107,7 +107,7 @@ RequestGroupMan::RequestGroupMan (option->getAsInt(PREF_MAX_OVERALL_DOWNLOAD_LIMIT)), maxOverallUploadSpeedLimit_(option->getAsInt (PREF_MAX_OVERALL_UPLOAD_LIMIT)), - rpc_(option->getAsBool(PREF_ENABLE_RPC)), + keepRunning_(option->getAsBool(PREF_ENABLE_RPC)), queueCheck_(true), removedErrorResult_(0), removedLastErrorResult_(error_code::FINISHED), @@ -125,7 +125,7 @@ RequestGroupMan::~RequestGroupMan() bool RequestGroupMan::downloadFinished() { - if(rpc_) { + if(keepRunning_) { return false; } return requestGroups_.empty() && reservedGroups_.empty(); @@ -474,7 +474,7 @@ void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e) } SharedHandle groupToAdd = *reservedGroups_.begin(); reservedGroups_.pop_front(); - if((rpc_ && groupToAdd->isPauseRequested()) || + if((keepRunning_ && groupToAdd->isPauseRequested()) || !groupToAdd->isDependencyResolved()) { pending.push_back(groupToAdd); continue; diff --git a/src/RequestGroupMan.h b/src/RequestGroupMan.h index 473941a3..debb2573 100644 --- a/src/RequestGroupMan.h +++ b/src/RequestGroupMan.h @@ -84,8 +84,9 @@ private: NetStat netStat_; - // true if JSON-RPC/XML-RPC is enabled. - bool rpc_; + // true if download engine should keep running even if there is no + // download to perform. + bool keepRunning_; bool queueCheck_; @@ -335,6 +336,11 @@ public: // Initializes WrDiskCache according to PREF_DISK_CACHE option. If // its value is 0, cache storage will not be initialized. void initWrDiskCache(); + + void setKeepRunning(bool flag) + { + keepRunning_ = flag; + } }; } // namespace aria2 diff --git a/src/aria2api.cc b/src/aria2api.cc index 9f46e957..72352266 100644 --- a/src/aria2api.cc +++ b/src/aria2api.cc @@ -106,6 +106,13 @@ int sessionFinal(Session* session) return rv; } +int sessionConfigSetKeepRunning(Session* session, bool flag) +{ + session->context->reqinfo->getDownloadEngine()->getRequestGroupMan() + ->setKeepRunning(flag); + return 0; +} + int run(Session* session, RUN_MODE mode) { const SharedHandle& e = @@ -113,6 +120,18 @@ int run(Session* session, RUN_MODE mode) return e->run(mode == RUN_ONCE); } +int shutdown(Session* session, bool force) +{ + const SharedHandle& e = + session->context->reqinfo->getDownloadEngine(); + if(force) { + e->requestForceHalt(); + } else { + e->requestHalt(); + } + return 0; +} + std::string gidToHex(const A2Gid& gid) { return GroupId::toHex(gid); diff --git a/src/includes/aria2/aria2.h b/src/includes/aria2/aria2.h index 37ae62f5..46415a4f 100644 --- a/src/includes/aria2/aria2.h +++ b/src/includes/aria2/aria2.h @@ -94,6 +94,13 @@ enum RUN_MODE { RUN_ONCE }; +// If the |flag| is true, run(session, RUN_ONCE) will return 1 even if +// there are no download to perform. The behavior is very similar to +// RPC server, except that this option does not enable RPC +// functionality. To stop aria2, use shutdown() function. This +// function returns 0 if it succeeds, or -1. +int sessionConfigSetKeepRunning(Session* session, bool flag); + // Performs event polling and actions for them. If the |mode| is // RUN_DEFAULT, this function returns when no downloads are left to be // processed. In this case, this function returns 0. @@ -147,9 +154,13 @@ int removeDownload(Session* session, const A2Gid& gid, bool force = false); // download is placed on the first position of waiting queue. As long // as the status is DOWNLOAD_PAUSED, the download will not start. To // change status to DOWNLOAD_WAITING, use unpauseDownload() function. -// If the |force| is true, removal will take place without any action +// If the |force| is true, pause will take place without any action // which takes time such as contacting BitTorrent tracker. This -// function returns 0 if it succeeds, or -1. +// function returns 0 if it succeeds, or -1. Please note that, to +// make pause work, the application must call +// sessionConfigSetKeepRunning() function with the |flag| argument to +// true. Without this call, download may be paused at first, but it +// will be restarted automatically. int pauseDownload(Session* session, const A2Gid& gid, bool force = false); // Changes the status of the download denoted by the |gid| from @@ -157,6 +168,13 @@ int pauseDownload(Session* session, const A2Gid& gid, bool force = false); // eligible to restart. This function returns 0 if it succeeds, or -1. int unpauseDownload(Session* session, const A2Gid& gid); +// Schedules shutdown. If the |force| is true, shutdown will take +// place without any action which takes time such as contacting +// BitTorrent tracker. After this call, the application must keep +// calling run() method until it returns 0. This function returns 0 +// if it succeeds, or -1. +int shutdown(Session* session, bool force = false); + enum UriStatus { URI_USED, URI_WAITING