ConsoleStatCalc.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  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., 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. #include "ConsoleStatCalc.h"
  36. #include "RequestGroupMan.h"
  37. #include "RequestGroup.h"
  38. #include "FileAllocationMan.h"
  39. #include "FileAllocationEntry.h"
  40. #include "CheckIntegrityMan.h"
  41. #include "CheckIntegrityEntry.h"
  42. #include "Util.h"
  43. #ifdef ENABLE_BITTORRENT
  44. # include "BtContext.h"
  45. #endif // ENABLE_BITTORRENT
  46. #include <termios.h>
  47. #include <sys/ioctl.h>
  48. #include <unistd.h>
  49. #include <iomanip>
  50. #include <iostream>
  51. namespace aria2 {
  52. void
  53. ConsoleStatCalc::calculateStat(const RequestGroupManHandle& requestGroupMan,
  54. const FileAllocationManHandle& fileAllocationMan,
  55. const CheckIntegrityManHandle& checkIntegrityMan)
  56. {
  57. if(!_cp.elapsed(1)) {
  58. return;
  59. }
  60. _cp.reset();
  61. bool isTTY = isatty(STDOUT_FILENO) == 1;
  62. if(isTTY) {
  63. struct winsize size;
  64. if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == -1) {
  65. size.ws_col = 80;
  66. }
  67. std::cout << '\r' << std::setw(size.ws_col) << ' ' << '\r';
  68. }
  69. if(requestGroupMan->countRequestGroup() > 0) {
  70. RequestGroupHandle firstRequestGroup = requestGroupMan->getRequestGroup(0);
  71. TransferStat stat = firstRequestGroup->calculateStat();
  72. unsigned int eta = 0;
  73. if(firstRequestGroup->getTotalLength() > 0 && stat.getDownloadSpeed() > 0) {
  74. eta = (firstRequestGroup->getTotalLength()-firstRequestGroup->getCompletedLength())/stat.getDownloadSpeed();
  75. }
  76. std::cout << "["
  77. << "#" << firstRequestGroup->getGID() << " ";
  78. #ifdef ENABLE_BITTORRENT
  79. if(firstRequestGroup->downloadFinished() &&
  80. !BtContextHandle(firstRequestGroup->getDownloadContext()).isNull()) {
  81. std::cout << "SEEDING" << "(" << "ratio:"
  82. << std::fixed << std::setprecision(1)
  83. << ((stat.getAllTimeUploadLength()*10)/firstRequestGroup->getCompletedLength())/10.0
  84. << ")";
  85. } else
  86. #endif // ENABLE_BITTORRENT
  87. {
  88. std::cout << "SIZE:"
  89. << Util::abbrevSize(firstRequestGroup->getCompletedLength())
  90. << "B"
  91. << "/"
  92. << Util::abbrevSize(firstRequestGroup->getTotalLength())
  93. << "B";
  94. if(firstRequestGroup->getTotalLength() > 0) {
  95. std::cout << "("
  96. << 100*firstRequestGroup->getCompletedLength()/firstRequestGroup->getTotalLength()
  97. << "%)";
  98. }
  99. }
  100. std::cout << " "
  101. << "CN:"
  102. << firstRequestGroup->getNumConnection();
  103. if(!firstRequestGroup->downloadFinished()) {
  104. std::cout << " "
  105. << "SPD:"
  106. << std::fixed << std::setprecision(2) << stat.getDownloadSpeed()/1024.0 << "KiB/s";
  107. }
  108. if(stat.getSessionUploadLength() > 0) {
  109. std::cout << " "
  110. << "UP:"
  111. << std::fixed << std::setprecision(2) << stat.getUploadSpeed()/1024.0 << "KiB/s"
  112. << "(" << Util::abbrevSize(stat.getAllTimeUploadLength()) << "B)";
  113. }
  114. if(eta > 0) {
  115. std::cout << " "
  116. << "ETA:"
  117. << Util::secfmt(eta);
  118. }
  119. std::cout << "]";
  120. if(requestGroupMan->countRequestGroup() > 1) {
  121. std::cout << "("
  122. << requestGroupMan->countRequestGroup()-1
  123. << "more...)";
  124. }
  125. }
  126. if(requestGroupMan->countRequestGroup() > 1 &&
  127. !requestGroupMan->downloadFinished()) {
  128. TransferStat stat = requestGroupMan->calculateStat();
  129. std::cout << " "
  130. << "[TOTAL SPD:"
  131. << std::fixed << std::setprecision(2) << stat.getDownloadSpeed()/1024.0 << "KiB/s" << "]";
  132. }
  133. {
  134. FileAllocationEntryHandle entry = fileAllocationMan->getCurrentFileAllocationEntry();
  135. if(!entry.isNull()) {
  136. std::cout << " "
  137. << "[FileAlloc:"
  138. << "#" << entry->getRequestGroup()->getGID() << " "
  139. << Util::abbrevSize(entry->getCurrentLength())
  140. << "B"
  141. << "/"
  142. << Util::abbrevSize(entry->getTotalLength())
  143. << "B"
  144. << "(";
  145. if(entry->getTotalLength() > 0) {
  146. std::cout << 100*entry->getCurrentLength()/entry->getTotalLength();
  147. } else {
  148. std::cout << "--";
  149. }
  150. std::cout << "%)"
  151. << "]";
  152. if(fileAllocationMan->countFileAllocationEntryInQueue() > 0) {
  153. std::cout << "("
  154. << fileAllocationMan->countFileAllocationEntryInQueue()
  155. << "waiting...)";
  156. }
  157. }
  158. }
  159. #ifdef ENABLE_MESSAGE_DIGEST
  160. {
  161. CheckIntegrityEntryHandle entry = checkIntegrityMan->getFirstCheckIntegrityEntry();
  162. if(!entry.isNull()) {
  163. std::cout << " "
  164. << "[Checksum:"
  165. << "#" << entry->getRequestGroup()->getGID() << " "
  166. << Util::abbrevSize(entry->getCurrentLength())
  167. << "B"
  168. << "/"
  169. << Util::abbrevSize(entry->getTotalLength())
  170. << "B"
  171. << "("
  172. << 100*entry->getCurrentLength()/entry->getTotalLength()
  173. << "%)";
  174. std::cout << "]";
  175. if(checkIntegrityMan->countCheckIntegrityEntry() > 1) {
  176. std::cout << "("
  177. << checkIntegrityMan->countCheckIntegrityEntry()-1
  178. << "more...)";
  179. }
  180. }
  181. }
  182. #endif // ENABLE_MESSAGE_DIGEST
  183. if(isTTY) {
  184. std::cout << std::flush;
  185. } else {
  186. std::cout << std::endl;
  187. }
  188. }
  189. } // namespace aria2