RequestGroupMan.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #ifndef _D_REQUEST_GROUP_MAN_H_
  36. #define _D_REQUEST_GROUP_MAN_H_
  37. #include "common.h"
  38. #include "RequestGroup.h"
  39. #include "LogFactory.h"
  40. class DownloadEngine;
  41. class RequestGroupMan {
  42. private:
  43. RequestGroups _requestGroups;
  44. RequestGroups _reservedGroups;
  45. const Logger* _logger;
  46. int32_t _maxSimultaneousDownloads;
  47. public:
  48. RequestGroupMan(const RequestGroups& requestGroups = RequestGroups(), int32_t maxSimultaneousDownloads = 1):
  49. _requestGroups(requestGroups),
  50. _logger(LogFactory::getInstance()),
  51. _maxSimultaneousDownloads(maxSimultaneousDownloads) {}
  52. bool downloadFinished()
  53. {
  54. for(RequestGroups::iterator itr = _requestGroups.begin();
  55. itr != _requestGroups.end(); ++itr) {
  56. if((*itr)->numConnection > 0 || !(*itr)->getSegmentMan()->finished()) {
  57. return false;
  58. }
  59. }
  60. return true;
  61. }
  62. void save()
  63. {
  64. for(RequestGroups::iterator itr = _requestGroups.begin();
  65. itr != _requestGroups.end(); ++itr) {
  66. if(!(*itr)->getSegmentMan()->finished()) {
  67. (*itr)->getSegmentMan()->save();
  68. }
  69. }
  70. }
  71. void closeFile()
  72. {
  73. for(RequestGroups::iterator itr = _requestGroups.begin();
  74. itr != _requestGroups.end(); ++itr) {
  75. (*itr)->getSegmentMan()->diskWriter->closeFile();
  76. }
  77. }
  78. int64_t getDownloadLength() const
  79. {
  80. int64_t downloadLength = 0;
  81. for(RequestGroups::const_iterator itr = _requestGroups.begin();
  82. itr != _requestGroups.end(); ++itr) {
  83. downloadLength += (*itr)->getSegmentMan()->getDownloadLength();
  84. }
  85. return downloadLength;
  86. }
  87. int64_t getTotalLength() const
  88. {
  89. int64_t totalLength = 0;
  90. for(RequestGroups::const_iterator itr = _requestGroups.begin();
  91. itr != _requestGroups.end(); ++itr) {
  92. totalLength += (*itr)->getSegmentMan()->totalSize;
  93. }
  94. return totalLength;
  95. }
  96. Commands getInitialCommands(DownloadEngine* e) const
  97. {
  98. Commands commands;
  99. for(RequestGroups::const_iterator itr = _requestGroups.begin();
  100. itr != _requestGroups.end(); ++itr) {
  101. (*itr)->initSegmentMan();
  102. commands.push_back((*itr)->getNextCommand(e, 1).front());
  103. }
  104. return commands;
  105. }
  106. void removeStoppedGroup();
  107. void fillRequestGroupFromReserver(DownloadEngine* e);
  108. void addRequestGroup(const RequestGroupHandle& group)
  109. {
  110. _requestGroups.push_back(group);
  111. }
  112. void addReservedGroup(const RequestGroups& groups)
  113. {
  114. _reservedGroups.insert(_reservedGroups.end(), groups.begin(), groups.end());
  115. }
  116. void addReservedGroup(const RequestGroupHandle& group)
  117. {
  118. _reservedGroups.push_back(group);
  119. }
  120. int32_t countRequestGroup() const
  121. {
  122. return _requestGroups.size();
  123. }
  124. RequestGroupHandle getRequestGroup(int32_t index) const
  125. {
  126. if(index < (int32_t)_requestGroups.size()) {
  127. return _requestGroups[index];
  128. } else {
  129. return 0;
  130. }
  131. }
  132. void showDownloadResults(ostream& o) const;
  133. int32_t getErrors() const
  134. {
  135. int32_t errors = 0;
  136. for(RequestGroups::const_iterator itr = _requestGroups.begin();
  137. itr != _requestGroups.end(); ++itr) {
  138. errors += (*itr)->getSegmentMan()->errors;
  139. }
  140. return errors;
  141. }
  142. };
  143. typedef SharedHandle<RequestGroupMan> RequestGroupManHandle;
  144. #endif // _D_REQUEST_GROUP_MAN_H_