ConsoleStatCalc.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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
  155. (const RequestGroupManHandle& requestGroupMan,
  156. const SharedHandle<FileAllocationMan>& fileAllocationMan,
  157. const CheckIntegrityManHandle& checkIntegrityMan)
  158. {
  159. if(!_cp.elapsed(1)) {
  160. return;
  161. }
  162. _cp.reset();
  163. bool isTTY = isatty(STDOUT_FILENO) == 1;
  164. unsigned short int cols = 80;
  165. if(isTTY) {
  166. #ifdef HAVE_TERMIOS_H
  167. struct winsize size;
  168. if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == 0) {
  169. cols = size.ws_col;
  170. }
  171. #endif // HAVE_TERMIOS_H
  172. std::cout << '\r' << std::setfill(' ') << std::setw(cols) << ' ' << '\r';
  173. }
  174. std::ostringstream o;
  175. if(requestGroupMan->countRequestGroup() > 0) {
  176. if((_summaryInterval > 0) &&
  177. _lastSummaryNotified.elapsed(_summaryInterval)) {
  178. _lastSummaryNotified.reset();
  179. printProgressSummary(requestGroupMan->getRequestGroups(), cols);
  180. std::cout << "\n";
  181. }
  182. RequestGroupHandle firstRequestGroup = requestGroupMan->getRequestGroup(0);
  183. printProgress(o, firstRequestGroup);
  184. if(requestGroupMan->countRequestGroup() > 1) {
  185. o << "("
  186. << requestGroupMan->countRequestGroup()-1
  187. << "more...)";
  188. }
  189. }
  190. if(requestGroupMan->countRequestGroup() > 1 &&
  191. !requestGroupMan->downloadFinished()) {
  192. TransferStat stat = requestGroupMan->calculateStat();
  193. o << " "
  194. << "[TOTAL SPD:"
  195. << std::fixed << std::setprecision(2) << stat.getDownloadSpeed()/1024.0 << "KiB/s" << "]";
  196. }
  197. {
  198. SharedHandle<FileAllocationEntry> entry=fileAllocationMan->getPickedEntry();
  199. if(!entry.isNull()) {
  200. o << " "
  201. << "[FileAlloc:"
  202. << "#" << entry->getRequestGroup()->getGID() << " "
  203. << Util::abbrevSize(entry->getCurrentLength())
  204. << "B"
  205. << "/"
  206. << Util::abbrevSize(entry->getTotalLength())
  207. << "B"
  208. << "(";
  209. if(entry->getTotalLength() > 0) {
  210. o << 100*entry->getCurrentLength()/entry->getTotalLength();
  211. } else {
  212. o << "--";
  213. }
  214. o << "%)"
  215. << "]";
  216. if(fileAllocationMan->hasNext()) {
  217. o << "("
  218. << fileAllocationMan->countEntryInQueue()
  219. << "waiting...)";
  220. }
  221. }
  222. }
  223. #ifdef ENABLE_MESSAGE_DIGEST
  224. {
  225. CheckIntegrityEntryHandle entry = checkIntegrityMan->getFirstCheckIntegrityEntry();
  226. if(!entry.isNull()) {
  227. o << " "
  228. << "[Checksum:"
  229. << "#" << entry->getRequestGroup()->getGID() << " "
  230. << Util::abbrevSize(entry->getCurrentLength())
  231. << "B"
  232. << "/"
  233. << Util::abbrevSize(entry->getTotalLength())
  234. << "B"
  235. << "("
  236. << 100*entry->getCurrentLength()/entry->getTotalLength()
  237. << "%)"
  238. << "]";
  239. if(checkIntegrityMan->countCheckIntegrityEntry() > 1) {
  240. o << "("
  241. << checkIntegrityMan->countCheckIntegrityEntry()-1
  242. << "more...)";
  243. }
  244. }
  245. }
  246. #endif // ENABLE_MESSAGE_DIGEST
  247. std::string readout = o.str();
  248. if(isTTY) {
  249. std::string::iterator last = readout.begin();
  250. if(readout.size() > cols) {
  251. std::advance(last, cols);
  252. } else {
  253. last = readout.end();
  254. }
  255. std::copy(readout.begin(), last, std::ostream_iterator<char>(std::cout));
  256. std::cout << std::flush;
  257. } else {
  258. std::copy(readout.begin(), readout.end(), std::ostream_iterator<char>(std::cout));
  259. std::cout << std::endl;
  260. }
  261. }
  262. } // namespace aria2