DownloadEngine.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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_DOWNLOAD_ENGINE_H
  36. #define D_DOWNLOAD_ENGINE_H
  37. #include "common.h"
  38. #include <string>
  39. #include <deque>
  40. #include <map>
  41. #include <vector>
  42. #include "SharedHandle.h"
  43. #include "a2netcompat.h"
  44. #include "TimerA2.h"
  45. #include "a2io.h"
  46. #include "CUIDCounter.h"
  47. #include "FileAllocationMan.h"
  48. #include "CheckIntegrityMan.h"
  49. #include "DNSCache.h"
  50. #ifdef ENABLE_ASYNC_DNS
  51. # include "AsyncNameResolver.h"
  52. #endif // ENABLE_ASYNC_DNS
  53. namespace aria2 {
  54. class Option;
  55. class RequestGroupMan;
  56. class StatCalc;
  57. class SocketCore;
  58. class CookieStorage;
  59. class AuthConfigFactory;
  60. class Request;
  61. class EventPoll;
  62. class Command;
  63. #ifdef ENABLE_BITTORRENT
  64. class BtRegistry;
  65. #endif // ENABLE_BITTORRENT
  66. class DownloadEngine {
  67. private:
  68. void waitData();
  69. std::string sessionId_;
  70. SharedHandle<EventPoll> eventPoll_;
  71. SharedHandle<StatCalc> statCalc_;
  72. bool haltRequested_;
  73. class SocketPoolEntry {
  74. private:
  75. SharedHandle<SocketCore> socket_;
  76. std::map<std::string, std::string> options_;
  77. time_t timeout_;
  78. Timer registeredTime_;
  79. public:
  80. SocketPoolEntry(const SharedHandle<SocketCore>& socket,
  81. const std::map<std::string, std::string>& option,
  82. time_t timeout);
  83. SocketPoolEntry(const SharedHandle<SocketCore>& socket,
  84. time_t timeout);
  85. ~SocketPoolEntry();
  86. bool isTimeout() const;
  87. const SharedHandle<SocketCore>& getSocket() const
  88. {
  89. return socket_;
  90. }
  91. const std::map<std::string, std::string>& getOptions() const
  92. {
  93. return options_;
  94. }
  95. };
  96. // key = IP address:port, value = SocketPoolEntry
  97. std::multimap<std::string, SocketPoolEntry> socketPool_;
  98. Timer lastSocketPoolScan_;
  99. bool noWait_;
  100. static const int64_t DEFAULT_REFRESH_INTERVAL = 900;
  101. // Milliseconds
  102. int64_t refreshInterval_;
  103. std::deque<Command*> routineCommands_;
  104. SharedHandle<CookieStorage> cookieStorage_;
  105. #ifdef ENABLE_BITTORRENT
  106. SharedHandle<BtRegistry> btRegistry_;
  107. #endif // ENABLE_BITTORRENT
  108. CUIDCounter cuidCounter_;
  109. SharedHandle<DNSCache> dnsCache_;
  110. SharedHandle<AuthConfigFactory> authConfigFactory_;
  111. /**
  112. * Delegates to StatCalc
  113. */
  114. void calculateStatistics();
  115. void onEndOfRun();
  116. void afterEachIteration();
  117. void poolSocket(const std::string& key, const SocketPoolEntry& entry);
  118. std::multimap<std::string, SocketPoolEntry>::iterator
  119. findSocketPoolEntry(const std::string& key);
  120. std::deque<Command*> commands_;
  121. SharedHandle<RequestGroupMan> requestGroupMan_;
  122. SharedHandle<FileAllocationMan> fileAllocationMan_;
  123. SharedHandle<CheckIntegrityMan> checkIntegrityMan_;
  124. Option* option_;
  125. public:
  126. DownloadEngine(const SharedHandle<EventPoll>& eventPoll);
  127. ~DownloadEngine();
  128. void run();
  129. void cleanQueue();
  130. bool addSocketForReadCheck(const SharedHandle<SocketCore>& socket,
  131. Command* command);
  132. bool deleteSocketForReadCheck(const SharedHandle<SocketCore>& socket,
  133. Command* command);
  134. bool addSocketForWriteCheck(const SharedHandle<SocketCore>& socket,
  135. Command* command);
  136. bool deleteSocketForWriteCheck(const SharedHandle<SocketCore>& socket,
  137. Command* command);
  138. #ifdef ENABLE_ASYNC_DNS
  139. bool addNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver,
  140. Command* command);
  141. bool deleteNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver,
  142. Command* command);
  143. #endif // ENABLE_ASYNC_DNS
  144. void addCommand(const std::vector<Command*>& commands);
  145. void addCommand(Command* command);
  146. const SharedHandle<RequestGroupMan>& getRequestGroupMan() const
  147. {
  148. return requestGroupMan_;
  149. }
  150. void setRequestGroupMan(const SharedHandle<RequestGroupMan>& rgman);
  151. const SharedHandle<FileAllocationMan>& getFileAllocationMan() const
  152. {
  153. return fileAllocationMan_;
  154. }
  155. void setFileAllocationMan(const SharedHandle<FileAllocationMan>& faman);
  156. const SharedHandle<CheckIntegrityMan>& getCheckIntegrityMan() const
  157. {
  158. return checkIntegrityMan_;
  159. }
  160. void setCheckIntegrityMan(const SharedHandle<CheckIntegrityMan>& ciman);
  161. Option* getOption() const
  162. {
  163. return option_;
  164. }
  165. void setOption(Option* op)
  166. {
  167. option_ = op;
  168. }
  169. void setStatCalc(const SharedHandle<StatCalc>& statCalc);
  170. bool isHaltRequested() const
  171. {
  172. return haltRequested_;
  173. }
  174. void requestHalt();
  175. void requestForceHalt();
  176. void setNoWait(bool b);
  177. void addRoutineCommand(Command* command);
  178. void poolSocket(const std::string& ipaddr, uint16_t port,
  179. const std::string& username,
  180. const std::string& proxyhost, uint16_t proxyport,
  181. const SharedHandle<SocketCore>& sock,
  182. const std::map<std::string, std::string>& options,
  183. time_t timeout = 15);
  184. void poolSocket(const SharedHandle<Request>& request,
  185. const std::string& username,
  186. const SharedHandle<Request>& proxyRequest,
  187. const SharedHandle<SocketCore>& socket,
  188. const std::map<std::string, std::string>& options,
  189. time_t timeout = 15);
  190. void poolSocket(const std::string& ipaddr, uint16_t port,
  191. const std::string& proxyhost, uint16_t proxyport,
  192. const SharedHandle<SocketCore>& sock,
  193. time_t timeout = 15);
  194. void poolSocket(const SharedHandle<Request>& request,
  195. const SharedHandle<Request>& proxyRequest,
  196. const SharedHandle<SocketCore>& socket,
  197. time_t timeout = 15);
  198. SharedHandle<SocketCore> popPooledSocket
  199. (const std::string& ipaddr,
  200. uint16_t port,
  201. const std::string& proxyhost, uint16_t proxyport);
  202. SharedHandle<SocketCore> popPooledSocket
  203. (std::map<std::string, std::string>& options,
  204. const std::string& ipaddr,
  205. uint16_t port,
  206. const std::string& username,
  207. const std::string& proxyhost, uint16_t proxyport);
  208. SharedHandle<SocketCore>
  209. popPooledSocket
  210. (const std::vector<std::string>& ipaddrs, uint16_t port);
  211. SharedHandle<SocketCore>
  212. popPooledSocket
  213. (std::map<std::string, std::string>& options,
  214. const std::vector<std::string>& ipaddrs,
  215. uint16_t port,
  216. const std::string& username);
  217. const SharedHandle<CookieStorage>& getCookieStorage() const
  218. {
  219. return cookieStorage_;
  220. }
  221. #ifdef ENABLE_BITTORRENT
  222. const SharedHandle<BtRegistry>& getBtRegistry() const
  223. {
  224. return btRegistry_;
  225. }
  226. #endif // ENABLE_BITTORRENT
  227. cuid_t newCUID();
  228. const std::string& findCachedIPAddress
  229. (const std::string& hostname, uint16_t port) const;
  230. template<typename OutputIterator>
  231. void findAllCachedIPAddresses
  232. (OutputIterator out, const std::string& hostname, uint16_t port) const
  233. {
  234. dnsCache_->findAll(out, hostname, port);
  235. }
  236. void cacheIPAddress
  237. (const std::string& hostname, const std::string& ipaddr, uint16_t port);
  238. void markBadIPAddress
  239. (const std::string& hostname, const std::string& ipaddr, uint16_t port);
  240. void removeCachedIPAddress(const std::string& hostname, uint16_t port);
  241. void setAuthConfigFactory(const SharedHandle<AuthConfigFactory>& factory);
  242. const SharedHandle<AuthConfigFactory>& getAuthConfigFactory() const
  243. {
  244. return authConfigFactory_;
  245. }
  246. void setRefreshInterval(int64_t interval);
  247. const std::string getSessionId() const
  248. {
  249. return sessionId_;
  250. }
  251. };
  252. typedef SharedHandle<DownloadEngine> DownloadEngineHandle;
  253. } // namespace aria2
  254. #endif // D_DOWNLOAD_ENGINE_H