Merge pull request #218 from kiwix/downloadPauseResume

Add pause and unpause functions for aria2
This commit is contained in:
Matthieu Gautier 2019-05-13 10:41:29 +02:00 committed by GitHub
commit 568b2b0e0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 90 additions and 1 deletions

View File

@ -54,6 +54,9 @@ class Download {
m_status(K_UNKNOWN), m_status(K_UNKNOWN),
m_did(did) {}; m_did(did) {};
void updateStatus(bool follow=false); void updateStatus(bool follow=false);
void pauseDownload();
void resumeDownload();
void cancelDownload();
StatusResult getStatus() { return m_status; } StatusResult getStatus() { return m_status; }
std::string getDid() { return m_did; } std::string getDid() { return m_did; }
std::string getFollowedBy() { return m_followedBy; } std::string getFollowedBy() { return m_followedBy; }

View File

@ -171,7 +171,6 @@ std::vector<std::string> Aria2::tellActive()
MethodCall methodCall("aria2.tellActive", m_secret); MethodCall methodCall("aria2.tellActive", m_secret);
auto statusArray = methodCall.newParamValue().getArray(); auto statusArray = methodCall.newParamValue().getArray();
statusArray.addValue().set(std::string("gid")); statusArray.addValue().set(std::string("gid"));
statusArray.addValue().set(std::string("following"));
auto responseContent = doRequest(methodCall); auto responseContent = doRequest(methodCall);
MethodResponse response(responseContent); MethodResponse response(responseContent);
std::vector<std::string> activeGID; std::vector<std::string> activeGID;
@ -186,6 +185,27 @@ std::vector<std::string> Aria2::tellActive()
return activeGID; return activeGID;
} }
std::vector<std::string> Aria2::tellWaiting()
{
MethodCall methodCall("aria2.tellWaiting", m_secret);
methodCall.newParamValue().set(0);
methodCall.newParamValue().set(99); // max number of downloads to be returned, don't know how to set this properly assumed that there will not be more than 99 paused downloads.
auto statusArray = methodCall.newParamValue().getArray();
statusArray.addValue().set(std::string("gid"));
auto responseContent = doRequest(methodCall);
MethodResponse response(responseContent);
std::vector<std::string> waitingGID;
int index = 0;
while(true) {
try {
auto structNode = response.getParamValue(0).getArray().getValue(index++).getStruct();
auto gidNode = structNode.getMember("gid");
waitingGID.push_back(gidNode.getValue().getAsS());
} catch (InvalidRPCNode& e) { break; }
}
return waitingGID;
}
void Aria2::saveSession() void Aria2::saveSession()
{ {
MethodCall methodCall("aria2.saveSession", m_secret); MethodCall methodCall("aria2.saveSession", m_secret);
@ -199,5 +219,25 @@ void Aria2::shutdown()
doRequest(methodCall); doRequest(methodCall);
} }
void Aria2::pause(const std::string& gid)
{
MethodCall methodCall("aria2.pause", m_secret);
methodCall.newParamValue().set(gid);
doRequest(methodCall);
}
void Aria2::unpause(const std::string& gid)
{
MethodCall methodCall("aria2.unpause", m_secret);
methodCall.newParamValue().set(gid);
doRequest(methodCall);
}
void Aria2::remove(const std::string& gid)
{
MethodCall methodCall("aria2.remove", m_secret);
methodCall.newParamValue().set(gid);
doRequest(methodCall);
}
} // end namespace kiwix } // end namespace kiwix

View File

@ -37,8 +37,12 @@ class Aria2
std::string addUri(const std::vector<std::string>& uri); std::string addUri(const std::vector<std::string>& uri);
std::string tellStatus(const std::string& gid, const std::vector<std::string>& statusKey); std::string tellStatus(const std::string& gid, const std::vector<std::string>& statusKey);
std::vector<std::string> tellActive(); std::vector<std::string> tellActive();
std::vector<std::string> tellWaiting();
void saveSession(); void saveSession();
void shutdown(); void shutdown();
void pause(const std::string& gid);
void unpause(const std::string& gid);
void remove(const std::string& gid);
}; };
}; //end namespace kiwix }; //end namespace kiwix

View File

@ -36,6 +36,8 @@ namespace kiwix
void Download::updateStatus(bool follow) void Download::updateStatus(bool follow)
{ {
if (m_status == Download::K_REMOVED)
return;
static std::vector<std::string> statusKey = {"status", "files", "totalLength", static std::vector<std::string> statusKey = {"status", "files", "totalLength",
"completedLength", "followedBy", "completedLength", "followedBy",
"downloadSpeed", "verifiedLength"}; "downloadSpeed", "verifiedLength"};
@ -93,6 +95,33 @@ void Download::updateStatus(bool follow)
} }
} }
void Download::resumeDownload()
{
if (!m_followedBy.empty())
mp_aria->unpause(m_followedBy);
else
mp_aria->unpause(m_did);
updateStatus(true);
}
void Download::pauseDownload()
{
if (!m_followedBy.empty())
mp_aria->pause(m_followedBy);
else
mp_aria->pause(m_did);
updateStatus(true);
}
void Download::cancelDownload()
{
if (!m_followedBy.empty())
mp_aria->remove(m_followedBy);
else
mp_aria->remove(m_did);
m_status = Download::K_REMOVED;
}
/* Constructor */ /* Constructor */
Downloader::Downloader() : Downloader::Downloader() :
mp_aria(new Aria2()) mp_aria(new Aria2())
@ -101,6 +130,10 @@ Downloader::Downloader() :
m_knownDownloads[gid] = std::unique_ptr<Download>(new Download(mp_aria, gid)); m_knownDownloads[gid] = std::unique_ptr<Download>(new Download(mp_aria, gid));
m_knownDownloads[gid]->updateStatus(); m_knownDownloads[gid]->updateStatus();
} }
for (auto gid : mp_aria->tellWaiting()) {
m_knownDownloads[gid] = std::unique_ptr<Download>(new Download(mp_aria, gid));
m_knownDownloads[gid]->updateStatus();
}
} }
@ -139,11 +172,20 @@ Download* Downloader::startDownload(const std::string& uri)
Download* Downloader::getDownload(const std::string& did) Download* Downloader::getDownload(const std::string& did)
{ {
try { try {
m_knownDownloads.at(did).get()->updateStatus(true);
return m_knownDownloads.at(did).get(); return m_knownDownloads.at(did).get();
} catch(exception& e) { } catch(exception& e) {
for (auto gid : mp_aria->tellActive()) { for (auto gid : mp_aria->tellActive()) {
if (gid == did) { if (gid == did) {
m_knownDownloads[gid] = std::unique_ptr<Download>(new Download(mp_aria, gid)); m_knownDownloads[gid] = std::unique_ptr<Download>(new Download(mp_aria, gid));
m_knownDownloads.at(gid).get()->updateStatus(true);
return m_knownDownloads[gid].get();
}
}
for (auto gid : mp_aria->tellWaiting()) {
if (gid == did) {
m_knownDownloads[gid] = std::unique_ptr<Download>(new Download(mp_aria, gid));
m_knownDownloads.at(gid).get()->updateStatus(true);
return m_knownDownloads[gid].get(); return m_knownDownloads[gid].get();
} }
} }