TorrentConsoleDownloadEngine.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. if(torrentMan->downloadComplete()) {
  30. printf("Download Completed ");
  31. } else {
  32. printf("%s/%sB %d%% DW:%.2f",
  33. Util::llitos(torrentMan->getDownloadedSize(), true).c_str(),
  34. Util::llitos(torrentMan->totalSize, true).c_str(),
  35. (torrentMan->totalSize == 0 ?
  36. 0 : (int)((torrentMan->getDownloadedSize()*100)/torrentMan->totalSize)),
  37. downloadSpeed/1000.0);
  38. }
  39. printf(" UP:%.2f(%s) %dpeers",
  40. uploadSpeed/1000.0,
  41. Util::llitos(torrentMan->getUploadedSize(), true).c_str(),
  42. torrentMan->connections);
  43. fflush(stdout);
  44. }
  45. void TorrentConsoleDownloadEngine::initStatistics() {
  46. downloadSpeed = 0;
  47. uploadSpeed = 0;
  48. lastElapsed = 0;
  49. gettimeofday(&cp[0], NULL);
  50. gettimeofday(&cp[1], NULL);
  51. sessionDownloadSize[0] = 0;
  52. sessionDownloadSize[1] = 0;
  53. sessionUploadSize[0] = 0;
  54. sessionUploadSize[1] = 0;
  55. currentCp = 0;
  56. }
  57. int TorrentConsoleDownloadEngine::calculateSpeed(long long int sessionSize, long long int elapsed) {
  58. int nowSpeed = (int)(sessionSize/(elapsed/1000000.0));
  59. return nowSpeed;
  60. }
  61. void TorrentConsoleDownloadEngine::calculateStatistics() {
  62. struct timeval now;
  63. gettimeofday(&now, NULL);
  64. long long int elapsed = Util::difftv(now, cp[currentCp]);
  65. sessionDownloadSize[0] += torrentMan->getDeltaDownload();
  66. sessionUploadSize[0] += torrentMan->getDeltaUpload();
  67. sessionDownloadSize[1] += torrentMan->getDeltaDownload();
  68. sessionUploadSize[1] += torrentMan->getDeltaUpload();
  69. downloadSpeed = calculateSpeed(sessionDownloadSize[currentCp], elapsed);
  70. uploadSpeed = calculateSpeed(sessionUploadSize[currentCp], elapsed);
  71. torrentMan->resetDeltaDownload();
  72. torrentMan->resetDeltaUpload();
  73. if(elapsed-lastElapsed >= 1000000) {
  74. printStatistics();
  75. lastElapsed = elapsed;
  76. }
  77. if(elapsed > 15*1000000) {
  78. sessionDownloadSize[currentCp] = 0;
  79. sessionUploadSize[currentCp] = 0;
  80. cp[currentCp] = now;
  81. lastElapsed = 0;
  82. currentCp = currentCp ? 0 : 1;
  83. }
  84. }