Update libaria2ex.cc to use latest API

pull/92/head
Tatsuhiro Tsujikawa 2013-05-18 16:13:56 +09:00
parent a4b29ac2f7
commit f3876ad9ab
1 changed files with 53 additions and 30 deletions

View File

@ -43,33 +43,45 @@
int downloadEventCallback(aria2::Session* session, aria2::DownloadEvent event, int downloadEventCallback(aria2::Session* session, aria2::DownloadEvent event,
const aria2::A2Gid& gid, void* userData) const aria2::A2Gid& gid, void* userData)
{ {
std::cout << "Download Event: ";
switch(event) { switch(event) {
case aria2::EVENT_ON_DOWNLOAD_START:
std::cout << "START";
break;
case aria2::EVENT_ON_DOWNLOAD_PAUSE:
std::cout << "PAUSE";
break;
case aria2::EVENT_ON_DOWNLOAD_STOP:
std::cout << "STOP";
break;
case aria2::EVENT_ON_DOWNLOAD_COMPLETE: case aria2::EVENT_ON_DOWNLOAD_COMPLETE:
std::cout << "COMPLETE"; std::cerr << "COMPLETE";
break; break;
case aria2::EVENT_ON_DOWNLOAD_ERROR: case aria2::EVENT_ON_DOWNLOAD_ERROR:
std::cout << "ERROR"; std::cerr << "ERROR";
break;
case aria2::EVENT_ON_BT_DOWNLOAD_COMPLETE:
std::cout << "COMPLETE";
break; break;
default:
return 0;
} }
std::cout << " for [" << aria2::gidToHex(gid) << "]" << std::endl; std::cerr << " [" << aria2::gidToHex(gid) << "] ";
aria2::DownloadHandle* dh = aria2::getDownloadHandle(session, gid);
if(!dh) return 0;
if(dh->getNumFiles() > 0) {
aria2::FileData f = dh->getFile(1);
// Path may be empty if the file name has not been determined yet.
if(f.path.empty()) {
if(!f.uris.empty()) {
std::cerr << f.uris[0].uri;
}
} else {
std::cerr << f.path;
}
}
aria2::deleteDownloadHandle(dh);
std::cerr << std::endl;
return 0; return 0;
} }
int main() int main(int argc, char** argv)
{ {
int rv;
if(argc < 2) {
std::cerr << "Usage: libaria2ex URI [URI...]\n"
<< "\n"
<< " Download given URIs in parallel in the current directory."
<< std::endl;
exit(EXIT_SUCCESS);
}
aria2::libraryInit(); aria2::libraryInit();
// session is actually singleton: 1 session per process // session is actually singleton: 1 session per process
aria2::Session* session; aria2::Session* session;
@ -78,16 +90,18 @@ int main()
// Add event callback // Add event callback
config.downloadEventCallback = downloadEventCallback; config.downloadEventCallback = downloadEventCallback;
session = aria2::sessionNew(aria2::KeyVals(), config); session = aria2::sessionNew(aria2::KeyVals(), config);
std::vector<std::string> uris = { // Add download item to session
"http://localhost/" for(int i = 1; i < argc; ++i) {
}; std::vector<std::string> uris = {argv[i]};
// Download files are stored in the current directory aria2::KeyVals options;
aria2::KeyVals options = { std::make_pair("dir", ".") }; rv = aria2::addUri(session, 0, uris, options);
// Add URI if(rv < 0) {
aria2::addUri(session, 0, uris, options); std::cerr << "Failed to add download " << uris[0] << std::endl;
}
}
auto start = std::chrono::steady_clock::now(); auto start = std::chrono::steady_clock::now();
for(;;) { for(;;) {
int rv = aria2::run(session, aria2::RUN_ONCE); rv = aria2::run(session, aria2::RUN_ONCE);
if(rv != 1) { if(rv != 1) {
break; break;
} }
@ -96,16 +110,25 @@ int main()
auto now = std::chrono::steady_clock::now(); auto now = std::chrono::steady_clock::now();
auto count = std::chrono::duration_cast<std::chrono::milliseconds> auto count = std::chrono::duration_cast<std::chrono::milliseconds>
(now - start).count(); (now - start).count();
// Print progress information once per 500ms
if(count >= 500) { if(count >= 500) {
start = now; start = now;
aria2::GlobalStat gstat = aria2::getGlobalStat(session);
std::cerr << "Overall #Active:" << gstat.numActive
<< " #waiting:" << gstat.numWaiting
<< " D:" << gstat.downloadSpeed/1024 << "KiB/s"
<< " U:"<< gstat.uploadSpeed/1024 << "KiB/s " << std::endl;
std::vector<aria2::A2Gid> gids = aria2::getActiveDownload(session); std::vector<aria2::A2Gid> gids = aria2::getActiveDownload(session);
std::cout << "== Active Download(s) ==" << std::endl; for(const auto& gid : gids) {
for(auto gid : gids) {
aria2::DownloadHandle* dh = aria2::getDownloadHandle(session, gid); aria2::DownloadHandle* dh = aria2::getDownloadHandle(session, gid);
if(dh) { if(dh) {
std::cout << "[" << aria2::gidToHex(gid) << "] " std::cerr << " [" << aria2::gidToHex(gid) << "] "
<< dh->getCompletedLength() << "/" << dh->getCompletedLength() << "/"
<< dh->getTotalLength() << " D:" << dh->getTotalLength() << "("
<< (dh->getTotalLength() > 0 ?
(100*dh->getCompletedLength()/dh->getTotalLength())
: 0) << "%)"
<< " D:"
<< dh->getDownloadSpeed()/1024 << "KiB/s, U:" << dh->getDownloadSpeed()/1024 << "KiB/s, U:"
<< dh->getUploadSpeed()/1024 << "KiB/s" << dh->getUploadSpeed()/1024 << "KiB/s"
<< std::endl; << std::endl;
@ -114,7 +137,7 @@ int main()
} }
} }
} }
int rv = aria2::sessionFinal(session); rv = aria2::sessionFinal(session);
aria2::libraryDeinit(); aria2::libraryDeinit();
return rv; return rv;
} }