RequestGroupMan.h 7.5 KB

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