2013-05-06 23:46:29 +08:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
//
|
|
|
|
// Compile and link like this:
|
|
|
|
// $ g++ -Wall -O2 -g -std=c++11 -o libaria2ex libaria2ex.cc -laria2
|
|
|
|
#include <iostream>
|
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
#include <aria2/aria2.h>
|
|
|
|
|
2013-05-11 18:40:24 +08:00
|
|
|
int downloadEventCallback(aria2::Session* session, aria2::DownloadEvent event,
|
2013-06-26 23:20:48 +08:00
|
|
|
aria2::A2Gid gid, void* userData)
|
2013-05-11 18:40:24 +08:00
|
|
|
{
|
2015-12-27 17:39:47 +08:00
|
|
|
switch (event) {
|
2013-05-11 18:40:24 +08:00
|
|
|
case aria2::EVENT_ON_DOWNLOAD_COMPLETE:
|
2013-05-18 15:13:56 +08:00
|
|
|
std::cerr << "COMPLETE";
|
2013-05-11 18:40:24 +08:00
|
|
|
break;
|
|
|
|
case aria2::EVENT_ON_DOWNLOAD_ERROR:
|
2013-05-18 15:13:56 +08:00
|
|
|
std::cerr << "ERROR";
|
2013-05-11 18:40:24 +08:00
|
|
|
break;
|
2013-05-18 15:13:56 +08:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
std::cerr << " [" << aria2::gidToHex(gid) << "] ";
|
|
|
|
aria2::DownloadHandle* dh = aria2::getDownloadHandle(session, gid);
|
2015-12-27 17:39:47 +08:00
|
|
|
if (!dh)
|
|
|
|
return 0;
|
|
|
|
if (dh->getNumFiles() > 0) {
|
2013-05-18 15:13:56 +08:00
|
|
|
aria2::FileData f = dh->getFile(1);
|
|
|
|
// Path may be empty if the file name has not been determined yet.
|
2015-12-27 17:39:47 +08:00
|
|
|
if (f.path.empty()) {
|
|
|
|
if (!f.uris.empty()) {
|
2013-05-18 15:13:56 +08:00
|
|
|
std::cerr << f.uris[0].uri;
|
|
|
|
}
|
2015-12-27 17:39:47 +08:00
|
|
|
}
|
|
|
|
else {
|
2013-05-18 15:13:56 +08:00
|
|
|
std::cerr << f.path;
|
|
|
|
}
|
2013-05-11 18:40:24 +08:00
|
|
|
}
|
2013-05-18 15:13:56 +08:00
|
|
|
aria2::deleteDownloadHandle(dh);
|
|
|
|
std::cerr << std::endl;
|
2013-05-11 18:40:24 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-18 15:13:56 +08:00
|
|
|
int main(int argc, char** argv)
|
2013-05-06 23:46:29 +08:00
|
|
|
{
|
2013-05-18 15:13:56 +08:00
|
|
|
int rv;
|
2015-12-27 17:39:47 +08:00
|
|
|
if (argc < 2) {
|
2013-05-18 15:13:56 +08:00
|
|
|
std::cerr << "Usage: libaria2ex URI [URI...]\n"
|
|
|
|
<< "\n"
|
|
|
|
<< " Download given URIs in parallel in the current directory."
|
|
|
|
<< std::endl;
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
2013-05-06 23:46:29 +08:00
|
|
|
aria2::libraryInit();
|
|
|
|
// session is actually singleton: 1 session per process
|
|
|
|
aria2::Session* session;
|
2013-05-18 15:26:24 +08:00
|
|
|
// Create default configuration. The libaria2 takes care of signal
|
|
|
|
// handling.
|
2013-05-06 23:46:29 +08:00
|
|
|
aria2::SessionConfig config;
|
2013-05-11 18:40:24 +08:00
|
|
|
// Add event callback
|
|
|
|
config.downloadEventCallback = downloadEventCallback;
|
2013-05-06 23:46:29 +08:00
|
|
|
session = aria2::sessionNew(aria2::KeyVals(), config);
|
2013-05-18 15:13:56 +08:00
|
|
|
// Add download item to session
|
2015-12-27 17:39:47 +08:00
|
|
|
for (int i = 1; i < argc; ++i) {
|
2013-05-18 15:13:56 +08:00
|
|
|
std::vector<std::string> uris = {argv[i]};
|
|
|
|
aria2::KeyVals options;
|
2013-05-21 22:53:21 +08:00
|
|
|
rv = aria2::addUri(session, nullptr, uris, options);
|
2015-12-27 17:39:47 +08:00
|
|
|
if (rv < 0) {
|
2013-05-18 15:13:56 +08:00
|
|
|
std::cerr << "Failed to add download " << uris[0] << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2013-05-06 23:46:29 +08:00
|
|
|
auto start = std::chrono::steady_clock::now();
|
2015-12-27 17:39:47 +08:00
|
|
|
for (;;) {
|
2013-05-18 15:13:56 +08:00
|
|
|
rv = aria2::run(session, aria2::RUN_ONCE);
|
2015-12-27 17:39:47 +08:00
|
|
|
if (rv != 1) {
|
2013-05-06 23:46:29 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// the application can call aria2 API to add URI or query progress
|
|
|
|
// here
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
2015-12-27 17:39:47 +08:00
|
|
|
auto count = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
now - start).count();
|
2013-05-18 15:13:56 +08:00
|
|
|
// Print progress information once per 500ms
|
2015-12-27 17:39:47 +08:00
|
|
|
if (count >= 500) {
|
2013-05-06 23:46:29 +08:00
|
|
|
start = now;
|
2013-05-18 15:13:56 +08:00
|
|
|
aria2::GlobalStat gstat = aria2::getGlobalStat(session);
|
|
|
|
std::cerr << "Overall #Active:" << gstat.numActive
|
|
|
|
<< " #waiting:" << gstat.numWaiting
|
2015-12-27 17:39:47 +08:00
|
|
|
<< " D:" << gstat.downloadSpeed / 1024 << "KiB/s"
|
|
|
|
<< " U:" << gstat.uploadSpeed / 1024 << "KiB/s " << std::endl;
|
2013-05-06 23:46:29 +08:00
|
|
|
std::vector<aria2::A2Gid> gids = aria2::getActiveDownload(session);
|
2015-12-27 17:39:47 +08:00
|
|
|
for (const auto& gid : gids) {
|
2013-05-06 23:46:29 +08:00
|
|
|
aria2::DownloadHandle* dh = aria2::getDownloadHandle(session, gid);
|
2015-12-27 17:39:47 +08:00
|
|
|
if (dh) {
|
2013-05-18 15:13:56 +08:00
|
|
|
std::cerr << " [" << aria2::gidToHex(gid) << "] "
|
2015-12-27 17:39:47 +08:00
|
|
|
<< dh->getCompletedLength() << "/" << dh->getTotalLength()
|
|
|
|
<< "(" << (dh->getTotalLength() > 0
|
|
|
|
? (100 * dh->getCompletedLength() /
|
|
|
|
dh->getTotalLength())
|
|
|
|
: 0) << "%)"
|
|
|
|
<< " D:" << dh->getDownloadSpeed() / 1024
|
|
|
|
<< "KiB/s, U:" << dh->getUploadSpeed() / 1024 << "KiB/s"
|
2013-05-06 23:46:29 +08:00
|
|
|
<< std::endl;
|
|
|
|
aria2::deleteDownloadHandle(dh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-18 15:13:56 +08:00
|
|
|
rv = aria2::sessionFinal(session);
|
2013-05-06 23:46:29 +08:00
|
|
|
aria2::libraryDeinit();
|
|
|
|
return rv;
|
|
|
|
}
|