RequestGroupMan.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. #include "RequestGroup.h"
  46. namespace aria2 {
  47. class DownloadEngine;
  48. class Command;
  49. struct 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. unsigned int maxSimultaneousDownloads_;
  59. const Option* option_;
  60. SharedHandle<ServerStatMan> serverStatMan_;
  61. unsigned int maxOverallDownloadSpeedLimit_;
  62. unsigned int maxOverallUploadSpeedLimit_;
  63. // truf if XML-RPC is enabled.
  64. bool xmlRpc_;
  65. bool queueCheck_;
  66. // The number of error DownloadResult removed because of upper limit
  67. // of the queue
  68. size_t removedErrorResult_;
  69. // The last error of removed DownloadResult
  70. error_code::Value removedLastErrorResult_;
  71. size_t maxDownloadResult_;
  72. std::string
  73. formatDownloadResult(const std::string& status,
  74. const SharedHandle<DownloadResult>& downloadResult) const;
  75. void configureRequestGroup
  76. (const SharedHandle<RequestGroup>& requestGroup) const;
  77. public:
  78. RequestGroupMan(const std::vector<SharedHandle<RequestGroup> >& requestGroups,
  79. unsigned int maxSimultaneousDownloads,
  80. const Option* option);
  81. ~RequestGroupMan();
  82. bool downloadFinished();
  83. void save();
  84. void closeFile();
  85. void halt();
  86. void forceHalt();
  87. void removeStoppedGroup(DownloadEngine* e);
  88. void fillRequestGroupFromReserver(DownloadEngine* e);
  89. void addRequestGroup(const SharedHandle<RequestGroup>& group);
  90. void addReservedGroup(const std::vector<SharedHandle<RequestGroup> >& groups);
  91. void addReservedGroup(const SharedHandle<RequestGroup>& group);
  92. void insertReservedGroup
  93. (size_t pos, const std::vector<SharedHandle<RequestGroup> >& groups);
  94. void insertReservedGroup(size_t pos, const SharedHandle<RequestGroup>& group);
  95. size_t countRequestGroup() const;
  96. SharedHandle<RequestGroup> getRequestGroup(size_t index) const;
  97. const std::deque<SharedHandle<RequestGroup> >& getRequestGroups() const
  98. {
  99. return requestGroups_;
  100. }
  101. SharedHandle<RequestGroup> findRequestGroup(gid_t gid) const;
  102. const std::deque<SharedHandle<RequestGroup> >& getReservedGroups() const
  103. {
  104. return reservedGroups_;
  105. }
  106. SharedHandle<RequestGroup> findReservedGroup(gid_t gid) const;
  107. enum HOW {
  108. POS_SET,
  109. POS_CUR,
  110. POS_END
  111. };
  112. // Changes the position of download denoted by gid. If how is
  113. // POS_SET, it moves the download to a position relative to the
  114. // beginning of the queue. If how is POS_CUR, it moves the download
  115. // to a position relative to the current position. If how is
  116. // POS_END, it moves the download to a position relative to the end
  117. // of the queue. If the destination position is less than 0 or
  118. // beyond the end of the queue, it moves the download to the
  119. // beginning or the end of the queue respectively. Returns the
  120. // destination position.
  121. size_t changeReservedGroupPosition(gid_t gid, int pos, HOW how);
  122. bool removeReservedGroup(gid_t gid);
  123. void showDownloadResults(std::ostream& o) const;
  124. bool isSameFileBeingDownloaded(RequestGroup* requestGroup) const;
  125. TransferStat calculateStat();
  126. class DownloadStat {
  127. private:
  128. size_t completed_;
  129. size_t error_;
  130. size_t inProgress_;
  131. size_t waiting_;
  132. error_code::Value lastErrorResult_;
  133. public:
  134. DownloadStat(size_t completed,
  135. size_t error,
  136. size_t inProgress,
  137. size_t waiting,
  138. error_code::Value lastErrorResult =
  139. error_code::FINISHED):
  140. completed_(completed),
  141. error_(error),
  142. inProgress_(inProgress),
  143. waiting_(waiting),
  144. lastErrorResult_(lastErrorResult) {}
  145. error_code::Value getLastErrorResult() const
  146. {
  147. return lastErrorResult_;
  148. }
  149. bool allCompleted() const
  150. {
  151. return error_ == 0 && inProgress_ == 0 && waiting_ == 0;
  152. }
  153. size_t getInProgress() const
  154. {
  155. return inProgress_;
  156. }
  157. };
  158. DownloadStat getDownloadStat() const;
  159. const std::deque<SharedHandle<DownloadResult> >& getDownloadResults() const
  160. {
  161. return downloadResults_;
  162. }
  163. SharedHandle<DownloadResult> findDownloadResult(gid_t gid) const;
  164. // Removes all download results.
  165. void purgeDownloadResult();
  166. void addDownloadResult(const SharedHandle<DownloadResult>& downloadResult);
  167. SharedHandle<ServerStat> findServerStat(const std::string& hostname,
  168. const std::string& protocol) const;
  169. SharedHandle<ServerStat> getOrCreateServerStat(const std::string& hostname,
  170. const std::string& protocol);
  171. bool addServerStat(const SharedHandle<ServerStat>& serverStat);
  172. void updateServerStat();
  173. bool loadServerStat(const std::string& filename);
  174. bool saveServerStat(const std::string& filename) const;
  175. void removeStaleServerStat(time_t timeout);
  176. // Returns true if current download speed exceeds
  177. // maxOverallDownloadSpeedLimit_. Always returns false if
  178. // maxOverallDownloadSpeedLimit_ == 0. Otherwise returns false.
  179. bool doesOverallDownloadSpeedExceed();
  180. void setMaxOverallDownloadSpeedLimit(unsigned int speed)
  181. {
  182. maxOverallDownloadSpeedLimit_ = speed;
  183. }
  184. unsigned int getMaxOverallDownloadSpeedLimit() const
  185. {
  186. return maxOverallDownloadSpeedLimit_;
  187. }
  188. // Returns true if current upload speed exceeds
  189. // maxOverallUploadSpeedLimit_. Always returns false if
  190. // maxOverallUploadSpeedLimit_ == 0. Otherwise returns false.
  191. bool doesOverallUploadSpeedExceed();
  192. void setMaxOverallUploadSpeedLimit(unsigned int speed)
  193. {
  194. maxOverallUploadSpeedLimit_ = speed;
  195. }
  196. unsigned int getMaxOverallUploadSpeedLimit() const
  197. {
  198. return maxOverallUploadSpeedLimit_;
  199. }
  200. void setMaxSimultaneousDownloads(unsigned int max)
  201. {
  202. maxSimultaneousDownloads_ = max;
  203. }
  204. // Call this function if requestGroups_ queue should be maintained.
  205. // This function is added to reduce the call of maintenance, but at
  206. // the same time, it provides fast maintenance reaction.
  207. void requestQueueCheck()
  208. {
  209. queueCheck_ = true;
  210. }
  211. void clearQueueCheck()
  212. {
  213. queueCheck_ = false;
  214. }
  215. bool queueCheckRequested() const
  216. {
  217. return queueCheck_;
  218. }
  219. // Returns currently used hosts and its use count.
  220. void getUsedHosts(std::vector<std::pair<size_t, std::string> >& usedHosts);
  221. };
  222. typedef SharedHandle<RequestGroupMan> RequestGroupManHandle;
  223. } // namespace aria2
  224. #endif // D_REQUEST_GROUP_MAN_H