Skip to content

Instantly share code, notes, and snippets.

@tatsuhiro-t
Created April 26, 2013 16:43
Show Gist options
  • Save tatsuhiro-t/5468651 to your computer and use it in GitHub Desktop.
Save tatsuhiro-t/5468651 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <chrono>
#include <aria2/aria2.h>
int main()
{
aria2::libraryInit();
// session is actually singleton: 1 session per process
aria2::Session* session;
session = aria2::sessionNew(aria2::KeyVals());
std::vector<std::string> uris = {
"http://releases.ubuntu.com/13.04/ubuntu-13.04-desktop-amd64.iso.torrent"
};
aria2::KeyVals options = { std::make_pair("dir", "/store") };
// Add URI
aria2::A2Gid gid;
aria2::addUri(session, gid, uris, options);
auto start = std::chrono::steady_clock::now();
for(;;) {
int rv = aria2::run(session, aria2::RUN_ONCE);
if(rv != 1) {
break;
}
// the application can call aria2 API to add URI or query progress
// here
auto now = std::chrono::steady_clock::now();
auto count = std::chrono::duration_cast<std::chrono::milliseconds>
(now - start).count();
if(count >= 500) {
start = now;
std::vector<aria2::A2Gid> gids = aria2::getActiveDownload(session);
std::cout << "== Active Download(s) ==" << std::endl;
for(auto gid : gids) {
aria2::DownloadHandle* dh = aria2::getDownloadHandle(session, gid);
if(dh) {
std::cout << "[" << aria2::gidToString(gid) << "] "
<< aria2::downloadGetCompletedLength(dh) << "/"
<< aria2::downloadGetTotalLength(dh) << " D:"
<< aria2::downloadGetDownloadSpeed(dh)/1024 << "KiB/s, U:"
<< aria2::downloadGetUploadSpeed(dh)/1024 << "KiB/s"
<< std::endl;
aria2::deleteDownloadHandle(dh);
}
}
}
}
int rv = aria2::sessionFinal(session);
session = 0;
aria2::libraryDeinit();
return rv;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment