RequestGroupMan.cc 4.4 KB

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