ConsoleStatCalc.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "DownloadEngine.h"
  50. #include "RequestGroupMan.h"
  51. #include "RequestGroup.h"
  52. #include "FileAllocationMan.h"
  53. #include "FileAllocationEntry.h"
  54. #include "CheckIntegrityMan.h"
  55. #include "CheckIntegrityEntry.h"
  56. #include "util.h"
  57. #include "DownloadContext.h"
  58. #ifdef ENABLE_BITTORRENT
  59. # include "bittorrent_helper.h"
  60. # include "Peer.h"
  61. # include "PeerStorage.h"
  62. # include "BtRegistry.h"
  63. # include "BtProgressInfoFile.h"
  64. # include "BtRuntime.h"
  65. # include "BtAnnounce.h"
  66. # include "PieceStorage.h"
  67. #endif // ENABLE_BITTORRENT
  68. namespace aria2 {
  69. static void printProgress
  70. (std::ostream& o, const SharedHandle<RequestGroup>& rg, const DownloadEngine* e)
  71. {
  72. TransferStat stat = rg->calculateStat();
  73. unsigned int eta = 0;
  74. if(rg->getTotalLength() > 0 && stat.getDownloadSpeed() > 0) {
  75. eta = (rg->getTotalLength()-rg->getCompletedLength())/stat.getDownloadSpeed();
  76. }
  77. o << "["
  78. << "#" << rg->getGID() << " ";
  79. #ifdef ENABLE_BITTORRENT
  80. if(rg->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT) &&
  81. rg->getDownloadContext()->getAttribute(bittorrent::BITTORRENT)
  82. .containsKey(bittorrent::METADATA) &&
  83. rg->downloadFinished()) {
  84. o << "SEEDING" << "(" << "ratio:";
  85. if(rg->getCompletedLength() > 0) {
  86. o << std::fixed << std::setprecision(1)
  87. << ((stat.getAllTimeUploadLength()*10)/rg->getCompletedLength())/10.0;
  88. } else {
  89. o << "--";
  90. }
  91. o << ")";
  92. } else
  93. #endif // ENABLE_BITTORRENT
  94. {
  95. o << "SIZE:"
  96. << util::abbrevSize(rg->getCompletedLength())
  97. << "B"
  98. << "/"
  99. << util::abbrevSize(rg->getTotalLength())
  100. << "B";
  101. if(rg->getTotalLength() > 0) {
  102. o << "("
  103. << 100*rg->getCompletedLength()/rg->getTotalLength()
  104. << "%)";
  105. }
  106. }
  107. o << " "
  108. << "CN:"
  109. << rg->getNumConnection();
  110. #ifdef ENABLE_BITTORRENT
  111. SharedHandle<PeerStorage> ps =
  112. e->getBtRegistry()->get(rg->getGID())._peerStorage;
  113. if(!ps.isNull()) {
  114. std::deque<SharedHandle<Peer> > peers;
  115. ps->getActivePeers(peers);
  116. o << " " << "SEED:"
  117. << countSeeder(peers.begin(), peers.end());
  118. }
  119. #endif // ENABLE_BITTORRENT
  120. if(!rg->downloadFinished()) {
  121. o << " "
  122. << "SPD:"
  123. << util::abbrevSize(stat.getDownloadSpeed()) << "Bs";
  124. }
  125. if(stat.getSessionUploadLength() > 0) {
  126. o << " "
  127. << "UP:"
  128. << util::abbrevSize(stat.getUploadSpeed()) << "Bs"
  129. << "(" << util::abbrevSize(stat.getAllTimeUploadLength()) << "B)";
  130. }
  131. if(eta > 0) {
  132. o << " "
  133. << "ETA:"
  134. << util::secfmt(eta);
  135. }
  136. o << "]";
  137. }
  138. class PrintSummary
  139. {
  140. private:
  141. size_t _cols;
  142. const DownloadEngine* _e;
  143. public:
  144. PrintSummary(size_t cols, const DownloadEngine* e):_cols(cols), _e(e) {}
  145. void operator()(const SharedHandle<RequestGroup>& rg)
  146. {
  147. const char SEP_CHAR = '-';
  148. printProgress(std::cout, rg, _e);
  149. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  150. rg->getDownloadContext()->getFileEntries();
  151. std::cout << "\n"
  152. << "FILE: ";
  153. writeFilePath(fileEntries.begin(), fileEntries.end(),
  154. std::cout, rg->inMemoryDownload());
  155. std::cout << "\n"
  156. << std::setfill(SEP_CHAR) << std::setw(_cols) << SEP_CHAR << "\n";
  157. }
  158. };
  159. static void printProgressSummary
  160. (const std::deque<SharedHandle<RequestGroup> >& groups, size_t cols,
  161. const DownloadEngine* e)
  162. {
  163. const char SEP_CHAR = '=';
  164. time_t now;
  165. time(&now);
  166. std::cout << " *** Download Progress Summary";
  167. {
  168. time_t now;
  169. struct tm* staticNowtmPtr;
  170. char buf[26];
  171. if(time(&now) != (time_t)-1 && (staticNowtmPtr = localtime(&now)) != 0 &&
  172. asctime_r(staticNowtmPtr, buf) != 0) {
  173. char* lfptr = strchr(buf, '\n');
  174. if(lfptr) {
  175. *lfptr = '\0';
  176. }
  177. std::cout << " as of " << buf;
  178. }
  179. }
  180. std::cout << " *** " << "\n"
  181. << std::setfill(SEP_CHAR) << std::setw(cols) << SEP_CHAR << "\n";
  182. std::for_each(groups.begin(), groups.end(), PrintSummary(cols, e));
  183. }
  184. ConsoleStatCalc::ConsoleStatCalc(time_t summaryInterval):
  185. _summaryInterval(summaryInterval)
  186. {}
  187. void
  188. ConsoleStatCalc::calculateStat(const DownloadEngine* e)
  189. {
  190. if(!_cp.elapsed(1)) {
  191. return;
  192. }
  193. _cp.reset();
  194. bool isTTY = isatty(STDOUT_FILENO) == 1;
  195. unsigned short int cols = 80;
  196. #ifdef __MINGW32__
  197. // Windows terminal cannot handle at the end of line properly.
  198. --cols;
  199. #endif // __MINGW32__
  200. if(isTTY) {
  201. #ifdef HAVE_TERMIOS_H
  202. struct winsize size;
  203. if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == 0) {
  204. cols = size.ws_col;
  205. #ifdef __MINGW32__
  206. if(cols > 0) {
  207. // Windows terminal cannot handle at the end of line properly.
  208. --cols;
  209. }
  210. #endif // __MINGW32__
  211. }
  212. #endif // HAVE_TERMIOS_H
  213. std::cout << '\r' << std::setfill(' ') << std::setw(cols) << ' ' << '\r';
  214. }
  215. std::ostringstream o;
  216. if(e->_requestGroupMan->countRequestGroup() > 0) {
  217. if((_summaryInterval > 0) &&
  218. _lastSummaryNotified.elapsed(_summaryInterval)) {
  219. _lastSummaryNotified.reset();
  220. printProgressSummary(e->_requestGroupMan->getRequestGroups(), cols, e);
  221. std::cout << "\n";
  222. }
  223. RequestGroupHandle firstRequestGroup = e->_requestGroupMan->getRequestGroup(0);
  224. printProgress(o, firstRequestGroup, e);
  225. if(e->_requestGroupMan->countRequestGroup() > 1) {
  226. o << "("
  227. << e->_requestGroupMan->countRequestGroup()-1
  228. << "more...)";
  229. }
  230. }
  231. if(e->_requestGroupMan->countRequestGroup() > 1 &&
  232. !e->_requestGroupMan->downloadFinished()) {
  233. TransferStat stat = e->_requestGroupMan->calculateStat();
  234. o << " "
  235. << "[TOTAL SPD:"
  236. << util::abbrevSize(stat.getDownloadSpeed()) << "Bs" << "]";
  237. }
  238. {
  239. SharedHandle<FileAllocationEntry> entry=e->_fileAllocationMan->getPickedEntry();
  240. if(!entry.isNull()) {
  241. o << " "
  242. << "[FileAlloc:"
  243. << "#" << entry->getRequestGroup()->getGID() << " "
  244. << util::abbrevSize(entry->getCurrentLength())
  245. << "B"
  246. << "/"
  247. << util::abbrevSize(entry->getTotalLength())
  248. << "B"
  249. << "(";
  250. if(entry->getTotalLength() > 0) {
  251. o << 100*entry->getCurrentLength()/entry->getTotalLength();
  252. } else {
  253. o << "--";
  254. }
  255. o << "%)"
  256. << "]";
  257. if(e->_fileAllocationMan->hasNext()) {
  258. o << "("
  259. << e->_fileAllocationMan->countEntryInQueue()
  260. << "waiting...)";
  261. }
  262. }
  263. }
  264. #ifdef ENABLE_MESSAGE_DIGEST
  265. {
  266. CheckIntegrityEntryHandle entry = e->_checkIntegrityMan->getPickedEntry();
  267. if(!entry.isNull()) {
  268. o << " "
  269. << "[Checksum:"
  270. << "#" << entry->getRequestGroup()->getGID() << " "
  271. << util::abbrevSize(entry->getCurrentLength())
  272. << "B"
  273. << "/"
  274. << util::abbrevSize(entry->getTotalLength())
  275. << "B"
  276. << "("
  277. << 100*entry->getCurrentLength()/entry->getTotalLength()
  278. << "%)"
  279. << "]";
  280. if(e->_checkIntegrityMan->hasNext()) {
  281. o << "("
  282. << e->_checkIntegrityMan->countEntryInQueue()
  283. << "waiting...)";
  284. }
  285. }
  286. }
  287. #endif // ENABLE_MESSAGE_DIGEST
  288. std::string readout = o.str();
  289. if(isTTY) {
  290. std::string::iterator last = readout.begin();
  291. if(readout.size() > cols) {
  292. std::advance(last, cols);
  293. } else {
  294. last = readout.end();
  295. }
  296. std::copy(readout.begin(), last, std::ostream_iterator<char>(std::cout));
  297. std::cout << std::flush;
  298. } else {
  299. std::copy(readout.begin(), readout.end(), std::ostream_iterator<char>(std::cout));
  300. std::cout << std::endl;
  301. }
  302. }
  303. } // namespace aria2