DownloadEngine.h 9.2 KB

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