libaria2ex.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2013 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. //
  36. // Compile and link like this:
  37. // $ g++ -Wall -O2 -g -std=c++11 -o libaria2ex libaria2ex.cc -laria2
  38. #include <iostream>
  39. #include <chrono>
  40. #include <aria2/aria2.h>
  41. int downloadEventCallback(aria2::Session* session, aria2::DownloadEvent event,
  42. aria2::A2Gid gid, void* userData)
  43. {
  44. switch(event) {
  45. case aria2::EVENT_ON_DOWNLOAD_COMPLETE:
  46. std::cerr << "COMPLETE";
  47. break;
  48. case aria2::EVENT_ON_DOWNLOAD_ERROR:
  49. std::cerr << "ERROR";
  50. break;
  51. default:
  52. return 0;
  53. }
  54. std::cerr << " [" << aria2::gidToHex(gid) << "] ";
  55. aria2::DownloadHandle* dh = aria2::getDownloadHandle(session, gid);
  56. if(!dh) return 0;
  57. if(dh->getNumFiles() > 0) {
  58. aria2::FileData f = dh->getFile(1);
  59. // Path may be empty if the file name has not been determined yet.
  60. if(f.path.empty()) {
  61. if(!f.uris.empty()) {
  62. std::cerr << f.uris[0].uri;
  63. }
  64. } else {
  65. std::cerr << f.path;
  66. }
  67. }
  68. aria2::deleteDownloadHandle(dh);
  69. std::cerr << std::endl;
  70. return 0;
  71. }
  72. int main(int argc, char** argv)
  73. {
  74. int rv;
  75. if(argc < 2) {
  76. std::cerr << "Usage: libaria2ex URI [URI...]\n"
  77. << "\n"
  78. << " Download given URIs in parallel in the current directory."
  79. << std::endl;
  80. exit(EXIT_SUCCESS);
  81. }
  82. aria2::libraryInit();
  83. // session is actually singleton: 1 session per process
  84. aria2::Session* session;
  85. // Create default configuration. The libaria2 takes care of signal
  86. // handling.
  87. aria2::SessionConfig config;
  88. // Add event callback
  89. config.downloadEventCallback = downloadEventCallback;
  90. session = aria2::sessionNew(aria2::KeyVals(), config);
  91. // Add download item to session
  92. for(int i = 1; i < argc; ++i) {
  93. std::vector<std::string> uris = {argv[i]};
  94. aria2::KeyVals options;
  95. rv = aria2::addUri(session, nullptr, uris, options);
  96. if(rv < 0) {
  97. std::cerr << "Failed to add download " << uris[0] << std::endl;
  98. }
  99. }
  100. auto start = std::chrono::steady_clock::now();
  101. for(;;) {
  102. rv = aria2::run(session, aria2::RUN_ONCE);
  103. if(rv != 1) {
  104. break;
  105. }
  106. // the application can call aria2 API to add URI or query progress
  107. // here
  108. auto now = std::chrono::steady_clock::now();
  109. auto count = std::chrono::duration_cast<std::chrono::milliseconds>
  110. (now - start).count();
  111. // Print progress information once per 500ms
  112. if(count >= 500) {
  113. start = now;
  114. aria2::GlobalStat gstat = aria2::getGlobalStat(session);
  115. std::cerr << "Overall #Active:" << gstat.numActive
  116. << " #waiting:" << gstat.numWaiting
  117. << " D:" << gstat.downloadSpeed/1024 << "KiB/s"
  118. << " U:"<< gstat.uploadSpeed/1024 << "KiB/s " << std::endl;
  119. std::vector<aria2::A2Gid> gids = aria2::getActiveDownload(session);
  120. for(const auto& gid : gids) {
  121. aria2::DownloadHandle* dh = aria2::getDownloadHandle(session, gid);
  122. if(dh) {
  123. std::cerr << " [" << aria2::gidToHex(gid) << "] "
  124. << dh->getCompletedLength() << "/"
  125. << dh->getTotalLength() << "("
  126. << (dh->getTotalLength() > 0 ?
  127. (100*dh->getCompletedLength()/dh->getTotalLength())
  128. : 0) << "%)"
  129. << " D:"
  130. << dh->getDownloadSpeed()/1024 << "KiB/s, U:"
  131. << dh->getUploadSpeed()/1024 << "KiB/s"
  132. << std::endl;
  133. aria2::deleteDownloadHandle(dh);
  134. }
  135. }
  136. }
  137. }
  138. rv = aria2::sessionFinal(session);
  139. aria2::libraryDeinit();
  140. return rv;
  141. }