ConsoleStatCalc.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #ifdef HAVE_TERMIOS_H
  37. #include <termios.h>
  38. #endif // HAVE_TERMIOS_H
  39. #ifdef HAVE_SYS_IOCTL_H
  40. #include <sys/ioctl.h>
  41. #endif // HAVE_SYS_IOCTL_H
  42. #include <unistd.h>
  43. #include <iomanip>
  44. #include <iostream>
  45. #include <algorithm>
  46. #include <cstring>
  47. #include <sstream>
  48. #include <iterator>
  49. #include "RequestGroupMan.h"
  50. #include "RequestGroup.h"
  51. #include "FileAllocationMan.h"
  52. #include "FileAllocationEntry.h"
  53. #include "CheckIntegrityMan.h"
  54. #include "CheckIntegrityEntry.h"
  55. #include "Util.h"
  56. #ifdef ENABLE_BITTORRENT
  57. # include "BtContext.h"
  58. #endif // ENABLE_BITTORRENT
  59. namespace aria2 {
  60. static void printProgress(std::ostream& o, const SharedHandle<RequestGroup>& rg)
  61. {
  62. TransferStat stat = rg->calculateStat();
  63. unsigned int eta = 0;
  64. if(rg->getTotalLength() > 0 && stat.getDownloadSpeed() > 0) {
  65. eta = (rg->getTotalLength()-rg->getCompletedLength())/stat.getDownloadSpeed();
  66. }
  67. o << "["
  68. << "#" << rg->getGID() << " ";
  69. #ifdef ENABLE_BITTORRENT
  70. if(rg->downloadFinished() &&
  71. !dynamic_pointer_cast<BtContext>(rg->getDownloadContext()).isNull()) {
  72. o << "SEEDING" << "(" << "ratio:"
  73. << std::fixed << std::setprecision(1)
  74. << ((stat.getAllTimeUploadLength()*10)/rg->getCompletedLength())/10.0
  75. << ")";
  76. } else
  77. #endif // ENABLE_BITTORRENT
  78. {
  79. o << "SIZE:"
  80. << Util::abbrevSize(rg->getCompletedLength())
  81. << "B"
  82. << "/"
  83. << Util::abbrevSize(rg->getTotalLength())
  84. << "B";
  85. if(rg->getTotalLength() > 0) {
  86. o << "("
  87. << 100*rg->getCompletedLength()/rg->getTotalLength()
  88. << "%)";
  89. }
  90. }
  91. o << " "
  92. << "CN:"
  93. << rg->getNumConnection();
  94. if(!rg->downloadFinished()) {
  95. o << " "
  96. << "SPD:"
  97. << std::fixed << std::setprecision(2) << stat.getDownloadSpeed()/1024.0 << "KiB/s";
  98. }
  99. if(stat.getSessionUploadLength() > 0) {
  100. o << " "
  101. << "UP:"
  102. << std::fixed << std::setprecision(2) << stat.getUploadSpeed()/1024.0 << "KiB/s"
  103. << "(" << Util::abbrevSize(stat.getAllTimeUploadLength()) << "B)";
  104. }
  105. if(eta > 0) {
  106. o << " "
  107. << "ETA:"
  108. << Util::secfmt(eta);
  109. }
  110. o << "]";
  111. }
  112. class PrintSummary
  113. {
  114. private:
  115. size_t _cols;
  116. public:
  117. PrintSummary(size_t cols):_cols(cols) {}
  118. void operator()(const SharedHandle<RequestGroup>& rg)
  119. {
  120. const char SEP_CHAR = '-';
  121. printProgress(std::cout, rg);
  122. std::cout << "\n"
  123. << "FILE: " << rg->getFilePath() << "\n"
  124. << std::setfill(SEP_CHAR) << std::setw(_cols) << SEP_CHAR << "\n";
  125. }
  126. };
  127. static void printProgressSummary(const std::deque<SharedHandle<RequestGroup> >& groups, size_t cols)
  128. {
  129. const char SEP_CHAR = '=';
  130. time_t now;
  131. time(&now);
  132. std::cout << " *** Download Progress Summary";
  133. {
  134. time_t now;
  135. struct tm* staticNowtmPtr;
  136. char buf[26];
  137. if(time(&now) != (time_t)-1 && (staticNowtmPtr = localtime(&now)) != 0 &&
  138. asctime_r(staticNowtmPtr, buf) != 0) {
  139. char* lfptr = strchr(buf, '\n');
  140. if(lfptr) {
  141. *lfptr = '\0';
  142. }
  143. std::cout << " as of " << buf;
  144. }
  145. }
  146. std::cout << " *** " << "\n"
  147. << std::setfill(SEP_CHAR) << std::setw(cols) << SEP_CHAR << "\n";
  148. std::for_each(groups.begin(), groups.end(), PrintSummary(cols));
  149. }
  150. ConsoleStatCalc::ConsoleStatCalc(time_t summaryInterval):
  151. _summaryInterval(summaryInterval)
  152. {}
  153. void
  154. ConsoleStatCalc::calculateStat(const RequestGroupManHandle& requestGroupMan,
  155. const FileAllocationManHandle& fileAllocationMan,
  156. const CheckIntegrityManHandle& checkIntegrityMan)
  157. {
  158. if(!_cp.elapsed(1)) {
  159. return;
  160. }
  161. _cp.reset();
  162. bool isTTY = isatty(STDOUT_FILENO) == 1;
  163. unsigned short int cols = 80;
  164. if(isTTY) {
  165. #ifdef HAVE_TERMIOS_H
  166. struct winsize size;
  167. if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == 0) {
  168. cols = size.ws_col;
  169. }
  170. #endif // HAVE_TERMIOS_H
  171. std::cout << '\r' << std::setfill(' ') << std::setw(cols) << ' ' << '\r';
  172. }
  173. std::ostringstream o;
  174. if(requestGroupMan->countRequestGroup() > 0) {
  175. if(_lastSummaryNotified.elapsed(_summaryInterval)) {
  176. _lastSummaryNotified.reset();
  177. printProgressSummary(requestGroupMan->getRequestGroups(), cols);
  178. std::cout << "\n";
  179. }
  180. RequestGroupHandle firstRequestGroup = requestGroupMan->getRequestGroup(0);
  181. printProgress(o, firstRequestGroup);
  182. if(requestGroupMan->countRequestGroup() > 1) {
  183. o << "("
  184. << requestGroupMan->countRequestGroup()-1
  185. << "more...)";
  186. }
  187. }
  188. if(requestGroupMan->countRequestGroup() > 1 &&
  189. !requestGroupMan->downloadFinished()) {
  190. TransferStat stat = requestGroupMan->calculateStat();
  191. o << " "
  192. << "[TOTAL SPD:"
  193. << std::fixed << std::setprecision(2) << stat.getDownloadSpeed()/1024.0 << "KiB/s" << "]";
  194. }
  195. {
  196. FileAllocationEntryHandle entry = fileAllocationMan->getCurrentFileAllocationEntry();
  197. if(!entry.isNull()) {
  198. o << " "
  199. << "[FileAlloc:"
  200. << "#" << entry->getRequestGroup()->getGID() << " "
  201. << Util::abbrevSize(entry->getCurrentLength())
  202. << "B"
  203. << "/"
  204. << Util::abbrevSize(entry->getTotalLength())
  205. << "B"
  206. << "(";
  207. if(entry->getTotalLength() > 0) {
  208. o << 100*entry->getCurrentLength()/entry->getTotalLength();
  209. } else {
  210. o << "--";
  211. }
  212. o << "%)"
  213. << "]";
  214. if(fileAllocationMan->countFileAllocationEntryInQueue() > 0) {
  215. o << "("
  216. << fileAllocationMan->countFileAllocationEntryInQueue()
  217. << "waiting...)";
  218. }
  219. }
  220. }
  221. #ifdef ENABLE_MESSAGE_DIGEST
  222. {
  223. CheckIntegrityEntryHandle entry = checkIntegrityMan->getFirstCheckIntegrityEntry();
  224. if(!entry.isNull()) {
  225. o << " "
  226. << "[Checksum:"
  227. << "#" << entry->getRequestGroup()->getGID() << " "
  228. << Util::abbrevSize(entry->getCurrentLength())
  229. << "B"
  230. << "/"
  231. << Util::abbrevSize(entry->getTotalLength())
  232. << "B"
  233. << "("
  234. << 100*entry->getCurrentLength()/entry->getTotalLength()
  235. << "%)"
  236. << "]";
  237. if(checkIntegrityMan->countCheckIntegrityEntry() > 1) {
  238. o << "("
  239. << checkIntegrityMan->countCheckIntegrityEntry()-1
  240. << "more...)";
  241. }
  242. }
  243. }
  244. #endif // ENABLE_MESSAGE_DIGEST
  245. std::string readout = o.str();
  246. if(isTTY) {
  247. std::string::iterator last = readout.begin();
  248. if(readout.size() > cols) {
  249. std::advance(last, cols);
  250. } else {
  251. last = readout.end();
  252. }
  253. std::copy(readout.begin(), last, std::ostream_iterator<char>(std::cout));
  254. std::cout << std::flush;
  255. } else {
  256. std::copy(readout.begin(), readout.end(), std::ostream_iterator<char>(std::cout));
  257. std::cout << std::endl;
  258. }
  259. }
  260. } // namespace aria2