DownloadEngine.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "SharedHandle.h"
  42. #include "a2netcompat.h"
  43. #include "TimeA2.h"
  44. #include "a2io.h"
  45. #ifdef ENABLE_ASYNC_DNS
  46. # include "AsyncNameResolver.h"
  47. #endif // ENABLE_ASYNC_DNS
  48. #include "CUIDCounter.h"
  49. #include "FileAllocationMan.h"
  50. #include "CheckIntegrityMan.h"
  51. namespace aria2 {
  52. class Logger;
  53. class Option;
  54. class RequestGroupMan;
  55. class StatCalc;
  56. class SocketCore;
  57. class CookieStorage;
  58. class BtRegistry;
  59. class DNSCache;
  60. class AuthConfigFactory;
  61. class Request;
  62. class EventPoll;
  63. class Command;
  64. class DownloadEngine {
  65. private:
  66. void waitData();
  67. SharedHandle<EventPoll> _eventPoll;
  68. Logger* logger;
  69. SharedHandle<StatCalc> _statCalc;
  70. bool _haltRequested;
  71. class SocketPoolEntry {
  72. private:
  73. SharedHandle<SocketCore> _socket;
  74. std::map<std::string, std::string> _options;
  75. time_t _timeout;
  76. Time _registeredTime;
  77. public:
  78. SocketPoolEntry(const SharedHandle<SocketCore>& socket,
  79. const std::map<std::string, std::string>& option,
  80. time_t timeout);
  81. ~SocketPoolEntry();
  82. bool isTimeout() const;
  83. SharedHandle<SocketCore> getSocket() const;
  84. const std::map<std::string, std::string>& getOptions() const;
  85. };
  86. // key = IP address:port, value = SocketPoolEntry
  87. std::multimap<std::string, SocketPoolEntry> _socketPool;
  88. Time _lastSocketPoolScan;
  89. bool _noWait;
  90. static const time_t DEFAULT_REFRESH_INTERVAL = 1;
  91. time_t _refreshInterval;
  92. std::deque<Command*> _routineCommands;
  93. SharedHandle<CookieStorage> _cookieStorage;
  94. SharedHandle<BtRegistry> _btRegistry;
  95. CUIDCounter _cuidCounter;
  96. SharedHandle<DNSCache> _dnsCache;
  97. SharedHandle<AuthConfigFactory> _authConfigFactory;
  98. /**
  99. * Delegates to StatCalc
  100. */
  101. void calculateStatistics();
  102. void onEndOfRun();
  103. void afterEachIteration();
  104. void poolSocket(const std::string& ipaddr,
  105. uint16_t port,
  106. const SocketPoolEntry& entry);
  107. std::multimap<std::string, SocketPoolEntry>::iterator
  108. findSocketPoolEntry(const std::string& ipaddr, uint16_t port);
  109. public:
  110. std::deque<Command*> commands;
  111. SharedHandle<RequestGroupMan> _requestGroupMan;
  112. SharedHandle<FileAllocationMan> _fileAllocationMan;
  113. SharedHandle<CheckIntegrityMan> _checkIntegrityMan;
  114. const Option* option;
  115. DownloadEngine(const SharedHandle<EventPoll>& eventPoll);
  116. virtual ~DownloadEngine();
  117. void run();
  118. void cleanQueue();
  119. bool addSocketForReadCheck(const SharedHandle<SocketCore>& socket,
  120. Command* command);
  121. bool deleteSocketForReadCheck(const SharedHandle<SocketCore>& socket,
  122. Command* command);
  123. bool addSocketForWriteCheck(const SharedHandle<SocketCore>& socket,
  124. Command* command);
  125. bool deleteSocketForWriteCheck(const SharedHandle<SocketCore>& socket,
  126. Command* command);
  127. #ifdef ENABLE_ASYNC_DNS
  128. bool addNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver,
  129. Command* command);
  130. bool deleteNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver,
  131. Command* command);
  132. #endif // ENABLE_ASYNC_DNS
  133. void addCommand(const std::deque<Command*>& commands);
  134. void fillCommand();
  135. void setStatCalc(const SharedHandle<StatCalc>& statCalc);
  136. bool isHaltRequested() const
  137. {
  138. return _haltRequested;
  139. }
  140. void requestHalt();
  141. void setNoWait(bool b);
  142. void addRoutineCommand(Command* command);
  143. void poolSocket(const std::string& ipaddr, uint16_t port,
  144. const SharedHandle<SocketCore>& sock,
  145. const std::map<std::string, std::string>& options,
  146. time_t timeout = 15);
  147. void poolSocket(const std::string& ipaddr, uint16_t port,
  148. const SharedHandle<SocketCore>& sock,
  149. time_t timeout = 15);
  150. void poolSocket(const SharedHandle<Request>& request,
  151. bool proxyDefined,
  152. const SharedHandle<SocketCore>& socket,
  153. const std::map<std::string, std::string>& options,
  154. time_t timeout = 15);
  155. void poolSocket(const SharedHandle<Request>& request,
  156. bool proxyDefined,
  157. const SharedHandle<SocketCore>& socket,
  158. time_t timeout = 15);
  159. SharedHandle<SocketCore> popPooledSocket(const std::string& ipaddr,
  160. uint16_t port);
  161. SharedHandle<SocketCore> popPooledSocket
  162. (std::map<std::string, std::string>& options,
  163. const std::string& ipaddr,
  164. uint16_t port);
  165. SharedHandle<SocketCore>
  166. popPooledSocket(const std::deque<std::string>& ipaddrs, uint16_t port);
  167. SharedHandle<SocketCore>
  168. popPooledSocket
  169. (std::map<std::string, std::string>& options,
  170. const std::deque<std::string>& ipaddrs,
  171. uint16_t port);
  172. SharedHandle<CookieStorage> getCookieStorage() const;
  173. SharedHandle<BtRegistry> getBtRegistry() const;
  174. CUID newCUID();
  175. const std::string& findCachedIPAddress(const std::string& hostname) const;
  176. void cacheIPAddress(const std::string& hostname, const std::string& ipaddr);
  177. void setAuthConfigFactory(const SharedHandle<AuthConfigFactory>& factory);
  178. SharedHandle<AuthConfigFactory> getAuthConfigFactory() const;
  179. void setRefreshInterval(time_t interval);
  180. };
  181. typedef SharedHandle<DownloadEngine> DownloadEngineHandle;
  182. } // namespace aria2
  183. #endif // _D_DOWNLOAD_ENGINE_H_