RequestGroupMan.cc 9.0 KB

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