RequestGroupMan.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 "RequestGroupMan.h"
  36. #include "BtProgressInfoFile.h"
  37. #include "RecoverableException.h"
  38. #include "RequestGroup.h"
  39. #include "LogFactory.h"
  40. #include "Logger.h"
  41. #include "DownloadEngine.h"
  42. #include "message.h"
  43. #include "a2functional.h"
  44. #include "DownloadResult.h"
  45. #include <iomanip>
  46. #include <sstream>
  47. #include <numeric>
  48. #include <algorithm>
  49. namespace aria2 {
  50. RequestGroupMan::RequestGroupMan(const RequestGroups& requestGroups,
  51. unsigned int maxSimultaneousDownloads):
  52. _requestGroups(requestGroups),
  53. _logger(LogFactory::getInstance()),
  54. _maxSimultaneousDownloads(maxSimultaneousDownloads),
  55. _gidCounter(0) {}
  56. bool RequestGroupMan::downloadFinished()
  57. {
  58. if(!_reservedGroups.empty()) {
  59. return false;
  60. }
  61. for(RequestGroups::iterator itr = _requestGroups.begin();
  62. itr != _requestGroups.end(); ++itr) {
  63. if((*itr)->getNumCommand() > 0 || !(*itr)->downloadFinished()) {
  64. return false;
  65. }
  66. }
  67. return true;
  68. }
  69. void RequestGroupMan::addRequestGroup(const RequestGroupHandle& group)
  70. {
  71. _requestGroups.push_back(group);
  72. }
  73. void RequestGroupMan::addReservedGroup(const RequestGroups& groups)
  74. {
  75. _reservedGroups.insert(_reservedGroups.end(), groups.begin(), groups.end());
  76. }
  77. void RequestGroupMan::addReservedGroup(const RequestGroupHandle& group)
  78. {
  79. _reservedGroups.push_back(group);
  80. }
  81. size_t RequestGroupMan::countRequestGroup() const
  82. {
  83. return _requestGroups.size();
  84. }
  85. RequestGroupHandle RequestGroupMan::getRequestGroup(size_t index) const
  86. {
  87. if(index < _requestGroups.size()) {
  88. return _requestGroups[index];
  89. } else {
  90. return 0;
  91. }
  92. }
  93. void RequestGroupMan::removeStoppedGroup()
  94. {
  95. unsigned int count = 0;
  96. RequestGroups temp;
  97. for(RequestGroups::iterator itr = _requestGroups.begin();
  98. itr != _requestGroups.end(); ++itr) {
  99. if((*itr)->getNumCommand() > 0) {
  100. temp.push_back(*itr);
  101. } else {
  102. try {
  103. (*itr)->closeFile();
  104. if((*itr)->downloadFinished()) {
  105. (*itr)->reportDownloadFinished();
  106. if((*itr)->allDownloadFinished()) {
  107. (*itr)->getProgressInfoFile()->removeFile();
  108. } else {
  109. (*itr)->getProgressInfoFile()->save();
  110. }
  111. RequestGroups nextGroups = (*itr)->postDownloadProcessing();
  112. if(nextGroups.size() > 0) {
  113. _logger->debug("Adding %u RequestGroups as a result of PostDownloadHandler.", nextGroups.size());
  114. std::copy(nextGroups.rbegin(), nextGroups.rend(), std::front_inserter(_reservedGroups));
  115. }
  116. } else {
  117. (*itr)->getProgressInfoFile()->save();
  118. }
  119. } catch(RecoverableException* ex) {
  120. _logger->error(EX_EXCEPTION_CAUGHT, ex);
  121. delete ex;
  122. }
  123. (*itr)->releaseRuntimeResource();
  124. ++count;
  125. _downloadResults.push_back((*itr)->createDownloadResult());
  126. }
  127. }
  128. _requestGroups = temp;
  129. if(count > 0) {
  130. _logger->debug("%u RequestGroup(s) deleted.", count);
  131. }
  132. }
  133. void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e)
  134. {
  135. RequestGroups temp;
  136. removeStoppedGroup();
  137. unsigned int count = 0;
  138. for(int num = _maxSimultaneousDownloads-_requestGroups.size();
  139. num > 0 && _reservedGroups.size() > 0; --num) {
  140. RequestGroupHandle groupToAdd = _reservedGroups.front();
  141. _reservedGroups.pop_front();
  142. try {
  143. if(!groupToAdd->isDependencyResolved()) {
  144. temp.push_front(groupToAdd);
  145. continue;
  146. }
  147. Commands commands = groupToAdd->createInitialCommand(e);
  148. _requestGroups.push_back(groupToAdd);
  149. ++count;
  150. e->addCommand(commands);
  151. } catch(RecoverableException* ex) {
  152. _logger->error(EX_EXCEPTION_CAUGHT, ex);
  153. delete ex;
  154. _downloadResults.push_back(groupToAdd->createDownloadResult());
  155. }
  156. }
  157. std::copy(temp.begin(), temp.end(), std::front_inserter(_reservedGroups));
  158. if(count > 0) {
  159. _logger->debug("%d RequestGroup(s) added.", count);
  160. }
  161. }
  162. Commands RequestGroupMan::getInitialCommands(DownloadEngine* e)
  163. {
  164. Commands commands;
  165. for(RequestGroups::iterator itr = _requestGroups.begin();
  166. itr != _requestGroups.end();) {
  167. try {
  168. if((*itr)->isDependencyResolved()) {
  169. Commands nextCommands = (*itr)->createInitialCommand(e);
  170. std::copy(nextCommands.begin(), nextCommands.end(), std::back_inserter(commands));
  171. ++itr;
  172. } else {
  173. _reservedGroups.push_front((*itr));
  174. itr = _requestGroups.erase(itr);
  175. }
  176. } catch(RecoverableException* e) {
  177. _logger->error(EX_EXCEPTION_CAUGHT, e);
  178. delete e;
  179. _downloadResults.push_back((*itr)->createDownloadResult());
  180. itr = _requestGroups.erase(itr);
  181. }
  182. }
  183. return commands;
  184. }
  185. void RequestGroupMan::save()
  186. {
  187. for(RequestGroups::iterator itr = _requestGroups.begin();
  188. itr != _requestGroups.end(); ++itr) {
  189. if((*itr)->allDownloadFinished()) {
  190. (*itr)->getProgressInfoFile()->removeFile();
  191. } else {
  192. try {
  193. (*itr)->getProgressInfoFile()->save();
  194. } catch(RecoverableException* e) {
  195. _logger->error(EX_EXCEPTION_CAUGHT, e);
  196. delete e;
  197. }
  198. }
  199. }
  200. }
  201. void RequestGroupMan::closeFile()
  202. {
  203. for(RequestGroups::iterator itr = _requestGroups.begin();
  204. itr != _requestGroups.end(); ++itr) {
  205. (*itr)->closeFile();
  206. }
  207. }
  208. RequestGroupMan::DownloadStat RequestGroupMan::getDownloadStat() const
  209. {
  210. DownloadStat stat;
  211. size_t finished = 0;
  212. size_t error = 0;
  213. size_t inprogress = 0;
  214. for(std::deque<SharedHandle<DownloadResult> >::const_iterator itr = _downloadResults.begin();
  215. itr != _downloadResults.end(); ++itr) {
  216. if((*itr)->result == DownloadResult::FINISHED) {
  217. ++finished;
  218. } else {
  219. ++error;
  220. }
  221. }
  222. for(RequestGroups::const_iterator itr = _requestGroups.begin();
  223. itr != _requestGroups.end(); ++itr) {
  224. DownloadResultHandle result = (*itr)->createDownloadResult();
  225. if(result->result == DownloadResult::FINISHED) {
  226. ++finished;
  227. } else {
  228. ++inprogress;
  229. }
  230. }
  231. stat.setCompleted(finished);
  232. stat.setError(error);
  233. stat.setInProgress(inprogress);
  234. stat.setWaiting(_reservedGroups.size());
  235. return stat;
  236. }
  237. void RequestGroupMan::showDownloadResults(std::ostream& o) const
  238. {
  239. // Download Results:
  240. // idx|stat|path/length
  241. // ===+====+=======================================================================
  242. o << "\n"
  243. <<_("Download Results:") << "\n"
  244. << "gid|stat|path/URI" << "\n"
  245. << "===+====+======================================================================" << "\n";
  246. for(std::deque<SharedHandle<DownloadResult> >::const_iterator itr = _downloadResults.begin();
  247. itr != _downloadResults.end(); ++itr) {
  248. std::string status = (*itr)->result == DownloadResult::FINISHED ? "OK" : "ERR";
  249. o << formatDownloadResult(status, *itr) << "\n";
  250. }
  251. for(RequestGroups::const_iterator itr = _requestGroups.begin();
  252. itr != _requestGroups.end(); ++itr) {
  253. DownloadResultHandle result = (*itr)->createDownloadResult();
  254. std::string status = result->result == DownloadResult::FINISHED ? "OK" : "INPR";
  255. o << formatDownloadResult(status, result) << "\n";
  256. }
  257. o << "\n"
  258. << _("Status Legend:") << "\n"
  259. << " (OK):download completed.(ERR):error occurred.(INPR):download in-progress." << "\n";
  260. }
  261. std::string RequestGroupMan::formatDownloadResult(const std::string& status, const DownloadResultHandle& downloadResult) const
  262. {
  263. std::stringstream o;
  264. o << std::setw(3) << downloadResult->gid << "|"
  265. << std::setw(4) << status << "|";
  266. if(downloadResult->result == DownloadResult::FINISHED) {
  267. o << downloadResult->filePath;
  268. } else {
  269. if(downloadResult->numUri == 0) {
  270. if(downloadResult->filePath.empty()) {
  271. o << "n/a";
  272. } else {
  273. o << downloadResult->filePath;
  274. }
  275. } else {
  276. o << downloadResult->uri;
  277. if(downloadResult->numUri > 1) {
  278. o << " (" << downloadResult->numUri-1 << "more)";
  279. }
  280. }
  281. }
  282. return o.str();
  283. }
  284. bool RequestGroupMan::isSameFileBeingDownloaded(RequestGroup* requestGroup) const
  285. {
  286. // TODO it may be good to use dedicated method rather than use isPreLocalFileCheckEnabled
  287. if(!requestGroup->isPreLocalFileCheckEnabled()) {
  288. return false;
  289. }
  290. for(RequestGroups::const_iterator itr = _requestGroups.begin();
  291. itr != _requestGroups.end(); ++itr) {
  292. if((*itr).get() != requestGroup &&
  293. (*itr)->getFilePath() == requestGroup->getFilePath()) {
  294. return true;
  295. }
  296. }
  297. return false;
  298. }
  299. void RequestGroupMan::halt()
  300. {
  301. for(RequestGroups::const_iterator itr = _requestGroups.begin();
  302. itr != _requestGroups.end(); ++itr) {
  303. (*itr)->setHaltRequested(true);
  304. }
  305. }
  306. void RequestGroupMan::forceHalt()
  307. {
  308. for(RequestGroups::const_iterator itr = _requestGroups.begin();
  309. itr != _requestGroups.end(); ++itr) {
  310. (*itr)->setForceHaltRequested(true);
  311. }
  312. }
  313. TransferStat RequestGroupMan::calculateStat()
  314. {
  315. return std::accumulate(_requestGroups.begin(), _requestGroups.end(), TransferStat(),
  316. adopt2nd(std::plus<TransferStat>(), mem_fun_sh(&RequestGroup::calculateStat)));
  317. }
  318. } // namespace aria2