RequestGroupMan.cc 8.6 KB

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