DownloadEngine.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 time_t DEFAULT_REFRESH_INTERVAL = 1;
  103. time_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. SharedHandle<DNSCache> dnsCache_;
  111. SharedHandle<AuthConfigFactory> authConfigFactory_;
  112. /**
  113. * Delegates to StatCalc
  114. */
  115. void calculateStatistics();
  116. void onEndOfRun();
  117. void afterEachIteration();
  118. void poolSocket(const std::string& key, const SocketPoolEntry& entry);
  119. std::multimap<std::string, SocketPoolEntry>::iterator
  120. findSocketPoolEntry(const std::string& key);
  121. std::deque<Command*> commands_;
  122. SharedHandle<RequestGroupMan> requestGroupMan_;
  123. SharedHandle<FileAllocationMan> fileAllocationMan_;
  124. SharedHandle<CheckIntegrityMan> checkIntegrityMan_;
  125. Option* option_;
  126. public:
  127. DownloadEngine(const SharedHandle<EventPoll>& eventPoll);
  128. ~DownloadEngine();
  129. void run();
  130. void cleanQueue();
  131. bool addSocketForReadCheck(const SharedHandle<SocketCore>& socket,
  132. Command* command);
  133. bool deleteSocketForReadCheck(const SharedHandle<SocketCore>& socket,
  134. Command* command);
  135. bool addSocketForWriteCheck(const SharedHandle<SocketCore>& socket,
  136. Command* command);
  137. bool deleteSocketForWriteCheck(const SharedHandle<SocketCore>& socket,
  138. Command* command);
  139. #ifdef ENABLE_ASYNC_DNS
  140. bool addNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver,
  141. Command* command);
  142. bool deleteNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver,
  143. Command* command);
  144. #endif // ENABLE_ASYNC_DNS
  145. void addCommand(const std::vector<Command*>& commands)
  146. {
  147. commands_.insert(commands_.end(), commands.begin(), commands.end());
  148. }
  149. void addCommand(Command* command)
  150. {
  151. commands_.push_back(command);
  152. }
  153. const SharedHandle<RequestGroupMan>& getRequestGroupMan() const
  154. {
  155. return requestGroupMan_;
  156. }
  157. void setRequestGroupMan(const SharedHandle<RequestGroupMan>& rgman)
  158. {
  159. requestGroupMan_ = rgman;
  160. }
  161. const SharedHandle<FileAllocationMan>& getFileAllocationMan() const
  162. {
  163. return fileAllocationMan_;
  164. }
  165. void setFileAllocationMan(const SharedHandle<FileAllocationMan>& faman)
  166. {
  167. fileAllocationMan_ = faman;
  168. }
  169. const SharedHandle<CheckIntegrityMan>& getCheckIntegrityMan() const
  170. {
  171. return checkIntegrityMan_;
  172. }
  173. void setCheckIntegrityMan(const SharedHandle<CheckIntegrityMan>& ciman)
  174. {
  175. checkIntegrityMan_ = ciman;
  176. }
  177. Option* getOption() const
  178. {
  179. return option_;
  180. }
  181. void setOption(Option* op)
  182. {
  183. option_ = op;
  184. }
  185. void setStatCalc(const SharedHandle<StatCalc>& statCalc);
  186. bool isHaltRequested() const
  187. {
  188. return haltRequested_;
  189. }
  190. void requestHalt();
  191. void requestForceHalt();
  192. void setNoWait(bool b);
  193. void addRoutineCommand(Command* command);
  194. void poolSocket(const std::string& ipaddr, uint16_t port,
  195. const std::string& username,
  196. const std::string& proxyhost, uint16_t proxyport,
  197. const SharedHandle<SocketCore>& sock,
  198. const std::map<std::string, std::string>& options,
  199. time_t timeout);
  200. void poolSocket(const SharedHandle<Request>& request,
  201. const std::string& username,
  202. const SharedHandle<Request>& proxyRequest,
  203. const SharedHandle<SocketCore>& socket,
  204. const std::map<std::string, std::string>& options,
  205. time_t timeout = 15);
  206. void poolSocket(const std::string& ipaddr, uint16_t port,
  207. const std::string& proxyhost, uint16_t proxyport,
  208. const SharedHandle<SocketCore>& sock,
  209. time_t timeout);
  210. void poolSocket(const SharedHandle<Request>& request,
  211. const SharedHandle<Request>& proxyRequest,
  212. const SharedHandle<SocketCore>& socket,
  213. time_t timeout = 15);
  214. SharedHandle<SocketCore> popPooledSocket
  215. (const std::string& ipaddr,
  216. uint16_t port,
  217. const std::string& proxyhost, uint16_t proxyport);
  218. SharedHandle<SocketCore> popPooledSocket
  219. (std::map<std::string, std::string>& options,
  220. const std::string& ipaddr,
  221. uint16_t port,
  222. const std::string& username,
  223. const std::string& proxyhost, uint16_t proxyport);
  224. SharedHandle<SocketCore>
  225. popPooledSocket
  226. (const std::vector<std::string>& ipaddrs, uint16_t port);
  227. SharedHandle<SocketCore>
  228. popPooledSocket
  229. (std::map<std::string, std::string>& options,
  230. const std::vector<std::string>& ipaddrs,
  231. uint16_t port,
  232. const std::string& username);
  233. const SharedHandle<CookieStorage>& getCookieStorage() const
  234. {
  235. return cookieStorage_;
  236. }
  237. #ifdef ENABLE_BITTORRENT
  238. const SharedHandle<BtRegistry>& getBtRegistry() const
  239. {
  240. return btRegistry_;
  241. }
  242. #endif // ENABLE_BITTORRENT
  243. cuid_t newCUID();
  244. const std::string& findCachedIPAddress
  245. (const std::string& hostname, uint16_t port) const;
  246. template<typename OutputIterator>
  247. void findAllCachedIPAddresses
  248. (OutputIterator out, const std::string& hostname, uint16_t port) const
  249. {
  250. dnsCache_->findAll(out, hostname, port);
  251. }
  252. void cacheIPAddress
  253. (const std::string& hostname, const std::string& ipaddr, uint16_t port);
  254. void markBadIPAddress
  255. (const std::string& hostname, const std::string& ipaddr, uint16_t port);
  256. void removeCachedIPAddress(const std::string& hostname, uint16_t port);
  257. void setAuthConfigFactory(const SharedHandle<AuthConfigFactory>& factory);
  258. const SharedHandle<AuthConfigFactory>& getAuthConfigFactory() const
  259. {
  260. return authConfigFactory_;
  261. }
  262. void setRefreshInterval(time_t interval);
  263. const std::string getSessionId() const
  264. {
  265. return sessionId_;
  266. }
  267. };
  268. typedef SharedHandle<DownloadEngine> DownloadEngineHandle;
  269. } // namespace aria2
  270. #endif // _D_DOWNLOAD_ENGINE_H_