TorrentDownloadEngine.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "TorrentDownloadEngine.h"
  23. #include "Util.h"
  24. TorrentDownloadEngine::TorrentDownloadEngine():filenameFixed(false),
  25. torrentMan(NULL) {}
  26. TorrentDownloadEngine::~TorrentDownloadEngine() {
  27. if(torrentMan != NULL) {
  28. delete torrentMan;
  29. }
  30. }
  31. void TorrentDownloadEngine::onEndOfRun() {
  32. torrentMan->diskAdaptor->closeFile();
  33. if(filenameFixed && torrentMan->downloadComplete()) {
  34. torrentMan->remove();
  35. } else {
  36. torrentMan->save();
  37. }
  38. }
  39. void TorrentDownloadEngine::afterEachIteration() {
  40. if(!filenameFixed && torrentMan->downloadComplete()) {
  41. if(torrentMan->isSelectiveDownloadingMode()) {
  42. onSelectiveDownloadingCompletes();
  43. }
  44. logger->info("The download was complete.");
  45. torrentMan->onDownloadComplete();
  46. if(torrentMan->downloadComplete()) {
  47. filenameFixed = true;
  48. }
  49. }
  50. }
  51. void TorrentDownloadEngine::initStatistics() {
  52. downloadSpeed = 0;
  53. uploadSpeed = 0;
  54. lastElapsed = 0;
  55. cp[0].reset();
  56. cp[1].reset();
  57. startup.reset();
  58. sessionDownloadLengthArray[0] = 0;
  59. sessionDownloadLengthArray[1] = 0;
  60. sessionUploadLengthArray[0] = 0;
  61. sessionUploadLengthArray[1] = 0;
  62. currentCp = 0;
  63. eta = 0;
  64. avgSpeed = 0;
  65. sessionDownloadLength = 0;
  66. downloadLength = 0;
  67. totalLength = 0;
  68. if(torrentMan->isSelectiveDownloadingMode()) {
  69. selectedDownloadLengthDiff = torrentMan->getDownloadLength()-torrentMan->getCompletedLength();
  70. selectedTotalLength = torrentMan->getSelectedTotalLength();
  71. }
  72. }
  73. int TorrentDownloadEngine::calculateSpeed(long long int sessionLength, int elapsed) {
  74. int nowSpeed = (int)(sessionLength/elapsed);
  75. return nowSpeed;
  76. }
  77. void TorrentDownloadEngine::calculateStatistics() {
  78. int elapsed = cp[currentCp].difference();
  79. sessionDownloadLengthArray[0] += torrentMan->getDeltaDownloadLength();
  80. sessionUploadLengthArray[0] += torrentMan->getDeltaUploadLength();
  81. sessionDownloadLengthArray[1] += torrentMan->getDeltaDownloadLength();
  82. sessionUploadLengthArray[1] += torrentMan->getDeltaUploadLength();
  83. sessionDownloadLength += torrentMan->getDeltaDownloadLength();
  84. torrentMan->resetDeltaDownloadLength();
  85. torrentMan->resetDeltaUploadLength();
  86. if(torrentMan->isSelectiveDownloadingMode()) {
  87. downloadLength = torrentMan->getDownloadLength()-selectedDownloadLengthDiff;
  88. totalLength = selectedTotalLength;
  89. } else {
  90. downloadLength = torrentMan->getDownloadLength();
  91. totalLength = torrentMan->getTotalLength();
  92. }
  93. if(elapsed > 0) {
  94. downloadSpeed = calculateSpeed(sessionDownloadLengthArray[currentCp], elapsed);
  95. uploadSpeed = calculateSpeed(sessionUploadLengthArray[currentCp], elapsed);
  96. }
  97. if(elapsed-lastElapsed >= 1) {
  98. int elapsedFromStartup = startup.difference();
  99. if(elapsedFromStartup > 0) {
  100. avgSpeed = calculateSpeed(sessionDownloadLength,
  101. elapsedFromStartup);
  102. }
  103. if(avgSpeed < 0) {
  104. avgSpeed = 0;
  105. } else if(avgSpeed != 0) {
  106. eta = (totalLength-downloadLength)/avgSpeed;
  107. }
  108. sendStatistics();
  109. lastElapsed = elapsed;
  110. }
  111. if(elapsed > 15) {
  112. sessionDownloadLengthArray[currentCp] = 0;
  113. sessionUploadLengthArray[currentCp] = 0;
  114. cp[currentCp].reset();
  115. lastElapsed = 0;
  116. currentCp = currentCp ? 0 : 1;
  117. }
  118. }