From 81359a7065207c489e4b4528da8d94f840b1b076 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 7 May 2013 00:46:29 +0900 Subject: [PATCH] Add libaria2 examples --- examples/libaria2ex.cc | 91 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 examples/libaria2ex.cc diff --git a/examples/libaria2ex.cc b/examples/libaria2ex.cc new file mode 100644 index 000000000..7a55657cb --- /dev/null +++ b/examples/libaria2ex.cc @@ -0,0 +1,91 @@ +/* */ +// +// Compile and link like this: +// $ g++ -Wall -O2 -g -std=c++11 -o libaria2ex libaria2ex.cc -laria2 +#include +#include + +#include + +int main() +{ + aria2::libraryInit(); + // session is actually singleton: 1 session per process + aria2::Session* session; + // Use default configuration + aria2::SessionConfig config; + session = aria2::sessionNew(aria2::KeyVals(), config); + std::vector uris = { + "http://localhost/" + }; + // Download files are stored in the current directory + aria2::KeyVals options = { std::make_pair("dir", ".") }; + // 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 + (now - start).count(); + if(count >= 500) { + start = now; + std::vector 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::gidToHex(gid) << "] " + << dh->getCompletedLength() << "/" + << dh->getTotalLength() << " D:" + << dh->getDownloadSpeed()/1024 << "KiB/s, U:" + << dh->getUploadSpeed()/1024 << "KiB/s" + << std::endl; + aria2::deleteDownloadHandle(dh); + } + } + } + } + int rv = aria2::sessionFinal(session); + aria2::libraryDeinit(); + return rv; +}