RequestGroupMan.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 <string>
  39. #include <deque>
  40. #include <iosfwd>
  41. #include <vector>
  42. #include "SharedHandle.h"
  43. #include "DownloadResult.h"
  44. #include "TransferStat.h"
  45. namespace aria2 {
  46. class DownloadEngine;
  47. class RequestGroup;
  48. class Command;
  49. class Logger;
  50. class DownloadResult;
  51. class ServerStatMan;
  52. class ServerStat;
  53. class Option;
  54. class RequestGroupMan {
  55. private:
  56. std::deque<SharedHandle<RequestGroup> > _requestGroups;
  57. std::deque<SharedHandle<RequestGroup> > _reservedGroups;
  58. std::deque<SharedHandle<DownloadResult> > _downloadResults;
  59. Logger* _logger;
  60. unsigned int _maxSimultaneousDownloads;
  61. int32_t _gidCounter;
  62. const Option* _option;
  63. SharedHandle<ServerStatMan> _serverStatMan;
  64. unsigned int _maxOverallDownloadSpeedLimit;
  65. unsigned int _maxOverallUploadSpeedLimit;
  66. // truf if XML-RPC is enabled.
  67. bool _xmlRpc;
  68. bool _queueCheck;
  69. std::string
  70. formatDownloadResult(const std::string& status,
  71. const SharedHandle<DownloadResult>& downloadResult) const;
  72. void configureRequestGroup
  73. (const SharedHandle<RequestGroup>& requestGroup) const;
  74. public:
  75. RequestGroupMan(const std::vector<SharedHandle<RequestGroup> >& requestGroups,
  76. unsigned int maxSimultaneousDownloads,
  77. const Option* option);
  78. bool downloadFinished();
  79. void save();
  80. void closeFile();
  81. void halt();
  82. void forceHalt();
  83. void removeStoppedGroup(DownloadEngine* e);
  84. void fillRequestGroupFromReserver(DownloadEngine* e);
  85. void addRequestGroup(const SharedHandle<RequestGroup>& group);
  86. void addReservedGroup(const std::vector<SharedHandle<RequestGroup> >& groups);
  87. void addReservedGroup(const SharedHandle<RequestGroup>& group);
  88. void insertReservedGroup
  89. (size_t pos, const std::vector<SharedHandle<RequestGroup> >& groups);
  90. void insertReservedGroup(size_t pos, const SharedHandle<RequestGroup>& group);
  91. size_t countRequestGroup() const;
  92. SharedHandle<RequestGroup> getRequestGroup(size_t index) const;
  93. const std::deque<SharedHandle<RequestGroup> >& getRequestGroups() const
  94. {
  95. return _requestGroups;
  96. }
  97. SharedHandle<RequestGroup> findRequestGroup(int32_t gid) const;
  98. const std::deque<SharedHandle<RequestGroup> >& getReservedGroups() const
  99. {
  100. return _reservedGroups;
  101. }
  102. SharedHandle<RequestGroup> findReservedGroup(int32_t gid) const;
  103. enum HOW {
  104. POS_SET,
  105. POS_CUR,
  106. POS_END
  107. };
  108. // Changes the position of download denoted by gid. If how is
  109. // POS_SET, it moves the download to a position relative to the
  110. // beginning of the queue. If how is POS_CUR, it moves the download
  111. // to a position relative to the current position. If how is
  112. // POS_END, it moves the download to a position relative to the end
  113. // of the queue. If the destination position is less than 0 or
  114. // beyond the end of the queue, it moves the download to the
  115. // beginning or the end of the queue respectively. Returns the
  116. // destination position.
  117. size_t changeReservedGroupPosition(int32_t gid, int pos, HOW how);
  118. bool removeReservedGroup(int32_t gid);
  119. void showDownloadResults(std::ostream& o) const;
  120. bool isSameFileBeingDownloaded(RequestGroup* requestGroup) const;
  121. TransferStat calculateStat();
  122. class DownloadStat {
  123. private:
  124. size_t _completed;
  125. size_t _error;
  126. size_t _inProgress;
  127. size_t _waiting;
  128. downloadresultcode::RESULT _lastErrorResult;
  129. public:
  130. DownloadStat(size_t completed,
  131. size_t error,
  132. size_t inProgress,
  133. size_t waiting,
  134. downloadresultcode::RESULT lastErrorResult =
  135. downloadresultcode::FINISHED):
  136. _completed(completed),
  137. _error(error),
  138. _inProgress(inProgress),
  139. _waiting(waiting),
  140. _lastErrorResult(lastErrorResult) {}
  141. downloadresultcode::RESULT getLastErrorResult() const
  142. {
  143. return _lastErrorResult;
  144. }
  145. bool allCompleted() const
  146. {
  147. return _error == 0 && _inProgress == 0 && _waiting == 0;
  148. }
  149. size_t getInProgress() const
  150. {
  151. return _inProgress;
  152. }
  153. };
  154. DownloadStat getDownloadStat() const;
  155. const std::deque<SharedHandle<DownloadResult> >& getDownloadResults() const
  156. {
  157. return _downloadResults;
  158. }
  159. SharedHandle<DownloadResult> findDownloadResult(int32_t gid) const;
  160. // Removes all download results.
  161. void purgeDownloadResult();
  162. SharedHandle<ServerStat> findServerStat(const std::string& hostname,
  163. const std::string& protocol) const;
  164. SharedHandle<ServerStat> getOrCreateServerStat(const std::string& hostname,
  165. const std::string& protocol);
  166. bool addServerStat(const SharedHandle<ServerStat>& serverStat);
  167. void updateServerStat();
  168. bool loadServerStat(const std::string& filename);
  169. bool saveServerStat(const std::string& filename) const;
  170. void removeStaleServerStat(time_t timeout);
  171. // Returns true if current download speed exceeds
  172. // _maxOverallDownloadSpeedLimit. Always returns false if
  173. // _maxOverallDownloadSpeedLimit == 0. Otherwise returns false.
  174. bool doesOverallDownloadSpeedExceed();
  175. void setMaxOverallDownloadSpeedLimit(unsigned int speed)
  176. {
  177. _maxOverallDownloadSpeedLimit = speed;
  178. }
  179. unsigned int getMaxOverallDownloadSpeedLimit() const
  180. {
  181. return _maxOverallDownloadSpeedLimit;
  182. }
  183. // Returns true if current upload speed exceeds
  184. // _maxOverallUploadSpeedLimit. Always returns false if
  185. // _maxOverallUploadSpeedLimit == 0. Otherwise returns false.
  186. bool doesOverallUploadSpeedExceed();
  187. void setMaxOverallUploadSpeedLimit(unsigned int speed)
  188. {
  189. _maxOverallUploadSpeedLimit = speed;
  190. }
  191. unsigned int getMaxOverallUploadSpeedLimit() const
  192. {
  193. return _maxOverallUploadSpeedLimit;
  194. }
  195. void setMaxSimultaneousDownloads(unsigned int max)
  196. {
  197. _maxSimultaneousDownloads = max;
  198. }
  199. // Call this function if _requestGroups queue should be maintained.
  200. // This function is added to reduce the call of maintenance, but at
  201. // the same time, it provides fast maintenance reaction.
  202. void requestQueueCheck()
  203. {
  204. _queueCheck = true;
  205. }
  206. void clearQueueCheck()
  207. {
  208. _queueCheck = false;
  209. }
  210. bool queueCheckRequested() const
  211. {
  212. return _queueCheck;
  213. }
  214. };
  215. typedef SharedHandle<RequestGroupMan> RequestGroupManHandle;
  216. } // namespace aria2
  217. #endif // _D_REQUEST_GROUP_MAN_H_