TorrentConsoleDownloadEngine.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "TorrentConsoleDownloadEngine.h"
  23. #include "Util.h"
  24. TorrentConsoleDownloadEngine::TorrentConsoleDownloadEngine() {}
  25. TorrentConsoleDownloadEngine::~TorrentConsoleDownloadEngine() {}
  26. void TorrentConsoleDownloadEngine::printStatistics() {
  27. printf("\r ");
  28. printf("\r");
  29. printf("%s/%sB %d%% DW:%.2f UP:%.2f(%s) %dpeers",
  30. Util::llitos(torrentMan->getDownloadedSize(), true).c_str(),
  31. Util::llitos(torrentMan->totalSize, true).c_str(),
  32. (torrentMan->totalSize == 0 ?
  33. 0 : (int)((torrentMan->getDownloadedSize()*100)/torrentMan->totalSize)),
  34. downloadSpeed/1000.0,
  35. uploadSpeed/1000.0,
  36. Util::llitos(torrentMan->getUploadedSize(), true).c_str(),
  37. torrentMan->connections);
  38. fflush(stdout);
  39. }
  40. void TorrentConsoleDownloadEngine::initStatistics() {
  41. /*
  42. gettimeofday(&cp, NULL);
  43. */
  44. downloadSpeed = 0;
  45. uploadSpeed = 0;
  46. /*
  47. sessionDownloadSize = 0;
  48. sessionUploadSize = 0;
  49. */
  50. lastElapsed = 0;
  51. gettimeofday(&cp[0], NULL);
  52. gettimeofday(&cp[1], NULL);
  53. sessionDownloadSize[0] = 0;
  54. sessionDownloadSize[1] = 0;
  55. sessionUploadSize[0] = 0;
  56. sessionUploadSize[1] = 0;
  57. currentCp = 0;
  58. }
  59. /*
  60. int TorrentConsoleDownloadEngine::calculateSpeed(int deltaSize, long long int elapsed, int prevSpeed) {
  61. int nowSpeed = (int)(deltaSize/(elapsed/1000000.0));
  62. return (nowSpeed+prevSpeed)/2;
  63. }
  64. */
  65. int TorrentConsoleDownloadEngine::calculateSpeed(long long int sessionSize, long long int elapsed) {
  66. int nowSpeed = (int)(sessionSize/(elapsed/1000000.0));
  67. return nowSpeed;
  68. }
  69. void TorrentConsoleDownloadEngine::calculateStatistics() {
  70. /*
  71. struct timeval now;
  72. gettimeofday(&now, NULL);
  73. long long int elapsed = Util::difftv(now, cp);
  74. sessionDownloadSize += torrentMan->getDeltaDownload();
  75. sessionUploadSize += torrentMan->getDeltaUpload();
  76. downloadSpeed = calculateSpeed(sessionDownloadSize, elapsed);
  77. uploadSpeed = calculateSpeed(sessionUploadSize, elapsed);
  78. torrentMan->resetDeltaDownload();
  79. torrentMan->resetDeltaUpload();
  80. if(elapsed-lastElapsed >= 1000000) {
  81. printStatistics();
  82. lastElapsed = elapsed;
  83. }
  84. */
  85. struct timeval now;
  86. gettimeofday(&now, NULL);
  87. long long int elapsed = Util::difftv(now, cp[currentCp]);
  88. sessionDownloadSize[0] += torrentMan->getDeltaDownload();
  89. sessionUploadSize[0] += torrentMan->getDeltaUpload();
  90. sessionDownloadSize[1] += torrentMan->getDeltaDownload();
  91. sessionUploadSize[1] += torrentMan->getDeltaUpload();
  92. downloadSpeed = calculateSpeed(sessionDownloadSize[currentCp], elapsed);
  93. uploadSpeed = calculateSpeed(sessionUploadSize[currentCp], elapsed);
  94. torrentMan->resetDeltaDownload();
  95. torrentMan->resetDeltaUpload();
  96. if(elapsed-lastElapsed >= 1000000) {
  97. printStatistics();
  98. lastElapsed = elapsed;
  99. }
  100. if(elapsed > 15*1000000) {
  101. sessionDownloadSize[currentCp] = 0;
  102. sessionUploadSize[currentCp] = 0;
  103. cp[currentCp] = now;
  104. lastElapsed = 0;
  105. currentCp = currentCp ? 0 : 1;
  106. }
  107. /*
  108. if(elapsed >= 1000000) {
  109. downloadSpeed = calculateSpeed(torrentMan->getDeltaDownload(),
  110. elapsed, downloadSpeed);
  111. uploadSpeed = calculateSpeed(torrentMan->getDeltaUpload(),
  112. elapsed, uploadSpeed);
  113. torrentMan->resetDeltaDownload();
  114. torrentMan->resetDeltaUpload();
  115. cp = now;
  116. printStatistics();
  117. }
  118. */
  119. }