RequestGroupMan.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "DownloadEngine.h"
  37. #include "message.h"
  38. #include <iomanip>
  39. void RequestGroupMan::removeStoppedGroup()
  40. {
  41. int32_t count = 0;
  42. RequestGroups temp;
  43. for(RequestGroups::iterator itr = _requestGroups.begin();
  44. itr != _requestGroups.end(); ++itr) {
  45. if((*itr)->numConnection > 0) {
  46. temp.push_back(*itr);
  47. } else {
  48. (*itr)->closeFile();
  49. if((*itr)->downloadFinished()) {
  50. _logger->notice(MSG_FILE_DOWNLOAD_COMPLETED,
  51. (*itr)->getFilePath().c_str());
  52. (*itr)->remove();
  53. } else {
  54. (*itr)->save();
  55. }
  56. ++count;
  57. }
  58. }
  59. _requestGroups = temp;
  60. if(count > 0) {
  61. _logger->debug("%d RequestGroup(s) deleted.", count);
  62. }
  63. }
  64. void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e)
  65. {
  66. removeStoppedGroup();
  67. int32_t count = 0;
  68. for(int32_t num = _maxSimultaneousDownloads-_requestGroups.size();
  69. num > 0 && _reservedGroups.size() > 0; --num) {
  70. RequestGroupHandle groupToAdd = _reservedGroups.front();
  71. _reservedGroups.pop_front();
  72. _requestGroups.push_back(groupToAdd);
  73. groupToAdd->initSegmentMan();
  74. groupToAdd->setGID(++_gidCounter);
  75. Commands commands = groupToAdd->createNextCommand(e, 1);
  76. count += commands.size();
  77. e->addCommand(commands);
  78. }
  79. if(count > 0) {
  80. _logger->debug("%d RequestGroup(s) added.", count);
  81. }
  82. }
  83. Commands RequestGroupMan::getInitialCommands(DownloadEngine* e)
  84. {
  85. Commands commands;
  86. for(RequestGroups::const_iterator itr = _requestGroups.begin();
  87. itr != _requestGroups.end(); ++itr) {
  88. (*itr)->initSegmentMan();
  89. (*itr)->setGID(++_gidCounter);
  90. Commands nextCommands = (*itr)->createNextCommand(e, 1);
  91. if(!nextCommands.empty()) {
  92. commands.push_back(nextCommands.front());
  93. }
  94. }
  95. return commands;
  96. }
  97. void RequestGroupMan::showDownloadResults(ostream& o) const
  98. {
  99. // Download Results:
  100. // idx|stat|path/length
  101. // ===+====+=======================================================================
  102. o << "\n"
  103. <<_("Download Results:") << "\n"
  104. << "idx|stat|path/URI" << "\n"
  105. << "===+====+======================================================================" << "\n";
  106. for(RequestGroups::const_iterator itr = _requestGroups.begin();
  107. itr != _requestGroups.end(); ++itr) {
  108. o << setw(3) << (*itr)->getGID() << "|";
  109. if((*itr)->downloadFinished()) {
  110. o << "OK ";
  111. } else {
  112. o << "ERR ";
  113. }
  114. o << "|";
  115. if((*itr)->downloadFinished()) {
  116. o << (*itr)->getFilePath();
  117. } else {
  118. Strings uris = (*itr)->getUris();
  119. if(uris.empty()) {
  120. o << "n/a";
  121. } else {
  122. o << uris.front();
  123. if(uris.size() > 1) {
  124. o << " (" << uris.size()-1 << "more)";
  125. }
  126. }
  127. }
  128. o << "\n";
  129. }
  130. }
  131. bool RequestGroupMan::isSameFileBeingDownloaded(RequestGroup* requestGroup) const
  132. {
  133. for(RequestGroups::const_iterator itr = _requestGroups.begin();
  134. itr != _requestGroups.end(); ++itr) {
  135. if((*itr).get() != requestGroup &&
  136. (*itr)->getFilePath() == requestGroup->getFilePath()) {
  137. return true;
  138. }
  139. }
  140. return false;
  141. }