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.
libaria2
Tatsuhiro Tsujikawa 2013-05-01 21:28:04 +09:00
parent a456d83de0
commit 24a6896bf4
4 changed files with 50 additions and 7 deletions

View File

@ -107,7 +107,7 @@ RequestGroupMan::RequestGroupMan
(option->getAsInt(PREF_MAX_OVERALL_DOWNLOAD_LIMIT)), (option->getAsInt(PREF_MAX_OVERALL_DOWNLOAD_LIMIT)),
maxOverallUploadSpeedLimit_(option->getAsInt maxOverallUploadSpeedLimit_(option->getAsInt
(PREF_MAX_OVERALL_UPLOAD_LIMIT)), (PREF_MAX_OVERALL_UPLOAD_LIMIT)),
rpc_(option->getAsBool(PREF_ENABLE_RPC)), keepRunning_(option->getAsBool(PREF_ENABLE_RPC)),
queueCheck_(true), queueCheck_(true),
removedErrorResult_(0), removedErrorResult_(0),
removedLastErrorResult_(error_code::FINISHED), removedLastErrorResult_(error_code::FINISHED),
@ -125,7 +125,7 @@ RequestGroupMan::~RequestGroupMan()
bool RequestGroupMan::downloadFinished() bool RequestGroupMan::downloadFinished()
{ {
if(rpc_) { if(keepRunning_) {
return false; return false;
} }
return requestGroups_.empty() && reservedGroups_.empty(); return requestGroups_.empty() && reservedGroups_.empty();
@ -474,7 +474,7 @@ void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e)
} }
SharedHandle<RequestGroup> groupToAdd = *reservedGroups_.begin(); SharedHandle<RequestGroup> groupToAdd = *reservedGroups_.begin();
reservedGroups_.pop_front(); reservedGroups_.pop_front();
if((rpc_ && groupToAdd->isPauseRequested()) || if((keepRunning_ && groupToAdd->isPauseRequested()) ||
!groupToAdd->isDependencyResolved()) { !groupToAdd->isDependencyResolved()) {
pending.push_back(groupToAdd); pending.push_back(groupToAdd);
continue; continue;

View File

@ -84,8 +84,9 @@ private:
NetStat netStat_; NetStat netStat_;
// true if JSON-RPC/XML-RPC is enabled. // true if download engine should keep running even if there is no
bool rpc_; // download to perform.
bool keepRunning_;
bool queueCheck_; bool queueCheck_;
@ -335,6 +336,11 @@ public:
// Initializes WrDiskCache according to PREF_DISK_CACHE option. If // Initializes WrDiskCache according to PREF_DISK_CACHE option. If
// its value is 0, cache storage will not be initialized. // its value is 0, cache storage will not be initialized.
void initWrDiskCache(); void initWrDiskCache();
void setKeepRunning(bool flag)
{
keepRunning_ = flag;
}
}; };
} // namespace aria2 } // namespace aria2

View File

@ -106,6 +106,13 @@ int sessionFinal(Session* session)
return rv; return rv;
} }
int sessionConfigSetKeepRunning(Session* session, bool flag)
{
session->context->reqinfo->getDownloadEngine()->getRequestGroupMan()
->setKeepRunning(flag);
return 0;
}
int run(Session* session, RUN_MODE mode) int run(Session* session, RUN_MODE mode)
{ {
const SharedHandle<DownloadEngine>& e = const SharedHandle<DownloadEngine>& e =
@ -113,6 +120,18 @@ int run(Session* session, RUN_MODE mode)
return e->run(mode == RUN_ONCE); return e->run(mode == RUN_ONCE);
} }
int shutdown(Session* session, bool force)
{
const SharedHandle<DownloadEngine>& e =
session->context->reqinfo->getDownloadEngine();
if(force) {
e->requestForceHalt();
} else {
e->requestHalt();
}
return 0;
}
std::string gidToHex(const A2Gid& gid) std::string gidToHex(const A2Gid& gid)
{ {
return GroupId::toHex(gid); return GroupId::toHex(gid);

View File

@ -94,6 +94,13 @@ enum RUN_MODE {
RUN_ONCE 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 // Performs event polling and actions for them. If the |mode| is
// RUN_DEFAULT, this function returns when no downloads are left to be // RUN_DEFAULT, this function returns when no downloads are left to be
// processed. In this case, this function returns 0. // 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 // download is placed on the first position of waiting queue. As long
// as the status is DOWNLOAD_PAUSED, the download will not start. To // as the status is DOWNLOAD_PAUSED, the download will not start. To
// change status to DOWNLOAD_WAITING, use unpauseDownload() function. // 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 // 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); int pauseDownload(Session* session, const A2Gid& gid, bool force = false);
// Changes the status of the download denoted by the |gid| from // 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. // eligible to restart. This function returns 0 if it succeeds, or -1.
int unpauseDownload(Session* session, const A2Gid& gid); 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 { enum UriStatus {
URI_USED, URI_USED,
URI_WAITING URI_WAITING