RequestGroupMan.h 7.9 KB

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