ConsoleDownloadEngine.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  4. *
  5. * Copyright (C) 2006 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "ConsoleDownloadEngine.h"
  23. #include "Util.h"
  24. ConsoleDownloadEngine::ConsoleDownloadEngine() {}
  25. ConsoleDownloadEngine::~ConsoleDownloadEngine() {}
  26. void ConsoleDownloadEngine::sendStatistics(long long int currentSize, long long int totalSize) {
  27. printf("\r ");
  28. printf("\r");
  29. printf("%s/%s Bytes %d%% %s %.2f KB/s %d connections",
  30. Util::llitos(currentSize, true).c_str(),
  31. Util::llitos(totalSize, true).c_str(),
  32. (totalSize == 0 ? 0 : (int)((currentSize*100)/totalSize)),
  33. avgSpeed == 0 ? "-" : Util::secfmt(eta).c_str(),
  34. speed/1024.0,
  35. commands.size());
  36. fflush(stdout);
  37. }
  38. void ConsoleDownloadEngine::initStatistics() {
  39. cp.reset();
  40. startup.reset();
  41. speed = 0;
  42. psize = 0;
  43. avgSpeed = 0;
  44. eta = 0;
  45. startupLength = 0;
  46. isStartupLengthSet = false;
  47. }
  48. void ConsoleDownloadEngine::calculateStatistics() {
  49. long long int dlSize = segmentMan->getDownloadedSize();
  50. if(!isStartupLengthSet && dlSize > 0) {
  51. startupLength = dlSize;
  52. isStartupLengthSet = true;
  53. }
  54. int elapsed = cp.difference();
  55. if(elapsed >= 1) {
  56. int nspeed = (int)((dlSize-psize)/elapsed);
  57. speed = (nspeed+speed)/2;
  58. cp.reset();
  59. psize = dlSize;
  60. int elapsedFromStartup = startup.difference();
  61. if(elapsedFromStartup > 0) {
  62. avgSpeed = (int)((dlSize-startupLength)/elapsedFromStartup);
  63. }
  64. if(avgSpeed < 0) {
  65. avgSpeed = 0;
  66. } else if(avgSpeed != 0 && segmentMan->totalSize > 0) {
  67. eta = (segmentMan->totalSize-dlSize)/avgSpeed;
  68. }
  69. sendStatistics(dlSize, segmentMan->totalSize);
  70. }
  71. }
  72. void ConsoleDownloadEngine::onEndOfRun() {
  73. segmentMan->diskWriter->closeFile();
  74. if(segmentMan->finished()) {
  75. segmentMan->remove();
  76. } else {
  77. segmentMan->save();
  78. }
  79. }