DownloadEngine.h 9.3 KB

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