RequestGroupMan.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 <vector>
  41. #include "SharedHandle.h"
  42. #include "DownloadResult.h"
  43. #include "TransferStat.h"
  44. #include "RequestGroup.h"
  45. namespace aria2 {
  46. class DownloadEngine;
  47. class Command;
  48. struct DownloadResult;
  49. class ServerStatMan;
  50. class ServerStat;
  51. class Option;
  52. class OutputFile;
  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. // true if JSON-RPC/XML-RPC is enabled.
  64. bool rpc_;
  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 formatDownloadResult
  73. (const std::string& status,
  74. const DownloadResultHandle& 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(a2_gid_t gid) const;
  102. const std::deque<SharedHandle<RequestGroup> >& getReservedGroups() const
  103. {
  104. return reservedGroups_;
  105. }
  106. SharedHandle<RequestGroup> findReservedGroup(a2_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(a2_gid_t gid, int pos, HOW how);
  122. bool removeReservedGroup(a2_gid_t gid);
  123. void showDownloadResults(OutputFile& 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 removed_;
  132. size_t waiting_;
  133. error_code::Value lastErrorResult_;
  134. public:
  135. DownloadStat(size_t completed,
  136. size_t error,
  137. size_t inProgress,
  138. size_t removed,
  139. size_t waiting,
  140. error_code::Value lastErrorResult =
  141. error_code::FINISHED):
  142. completed_(completed),
  143. error_(error),
  144. inProgress_(inProgress),
  145. removed_(removed),
  146. waiting_(waiting),
  147. lastErrorResult_(lastErrorResult) {}
  148. error_code::Value getLastErrorResult() const
  149. {
  150. return lastErrorResult_;
  151. }
  152. bool allCompleted() const
  153. {
  154. return error_ == 0 && inProgress_ == 0 && waiting_ == 0;
  155. }
  156. size_t getInProgress() const
  157. {
  158. return inProgress_;
  159. }
  160. };
  161. DownloadStat getDownloadStat() const;
  162. const std::deque<SharedHandle<DownloadResult> >& getDownloadResults() const
  163. {
  164. return downloadResults_;
  165. }
  166. SharedHandle<DownloadResult> findDownloadResult(a2_gid_t gid) const;
  167. // Removes all download results.
  168. void purgeDownloadResult();
  169. // Removes download result of given gid. Returns true if download
  170. // result was removed. Otherwise returns false.
  171. bool removeDownloadResult(a2_gid_t gid);
  172. void addDownloadResult(const SharedHandle<DownloadResult>& downloadResult);
  173. SharedHandle<ServerStat> findServerStat(const std::string& hostname,
  174. const std::string& protocol) const;
  175. SharedHandle<ServerStat> getOrCreateServerStat(const std::string& hostname,
  176. const std::string& protocol);
  177. bool addServerStat(const SharedHandle<ServerStat>& serverStat);
  178. void updateServerStat();
  179. bool loadServerStat(const std::string& filename);
  180. bool saveServerStat(const std::string& filename) const;
  181. void removeStaleServerStat(time_t timeout);
  182. // Returns true if current download speed exceeds
  183. // maxOverallDownloadSpeedLimit_. Always returns false if
  184. // maxOverallDownloadSpeedLimit_ == 0. Otherwise returns false.
  185. bool doesOverallDownloadSpeedExceed();
  186. void setMaxOverallDownloadSpeedLimit(unsigned int speed)
  187. {
  188. maxOverallDownloadSpeedLimit_ = speed;
  189. }
  190. unsigned int getMaxOverallDownloadSpeedLimit() const
  191. {
  192. return maxOverallDownloadSpeedLimit_;
  193. }
  194. // Returns true if current upload speed exceeds
  195. // maxOverallUploadSpeedLimit_. Always returns false if
  196. // maxOverallUploadSpeedLimit_ == 0. Otherwise returns false.
  197. bool doesOverallUploadSpeedExceed();
  198. void setMaxOverallUploadSpeedLimit(unsigned int speed)
  199. {
  200. maxOverallUploadSpeedLimit_ = speed;
  201. }
  202. unsigned int getMaxOverallUploadSpeedLimit() const
  203. {
  204. return maxOverallUploadSpeedLimit_;
  205. }
  206. void setMaxSimultaneousDownloads(unsigned int max)
  207. {
  208. maxSimultaneousDownloads_ = max;
  209. }
  210. // Call this function if requestGroups_ queue should be maintained.
  211. // This function is added to reduce the call of maintenance, but at
  212. // the same time, it provides fast maintenance reaction.
  213. void requestQueueCheck()
  214. {
  215. queueCheck_ = true;
  216. }
  217. void clearQueueCheck()
  218. {
  219. queueCheck_ = false;
  220. }
  221. bool queueCheckRequested() const
  222. {
  223. return queueCheck_;
  224. }
  225. // Returns currently used hosts and its use count.
  226. void getUsedHosts(std::vector<std::pair<size_t, std::string> >& usedHosts);
  227. const SharedHandle<ServerStatMan>& getServerStatMan() const
  228. {
  229. return serverStatMan_;
  230. }
  231. };
  232. typedef SharedHandle<RequestGroupMan> RequestGroupManHandle;
  233. } // namespace aria2
  234. #endif // D_REQUEST_GROUP_MAN_H