AbstractCommand.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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_ABSTRACT_COMMAND_H
  36. #define D_ABSTRACT_COMMAND_H
  37. #include "Command.h"
  38. #include <vector>
  39. #include <string>
  40. #include "SharedHandle.h"
  41. #include "TimerA2.h"
  42. namespace aria2 {
  43. class FileEntry;
  44. class RequestGroup;
  45. class CheckIntegrityEntry;
  46. class DownloadContext;
  47. class SegmentMan;
  48. class PieceStorage;
  49. class Request;
  50. class DownloadEngine;
  51. class Segment;
  52. class SocketCore;
  53. class Option;
  54. #ifdef ENABLE_ASYNC_DNS
  55. class AsyncNameResolver;
  56. #endif // ENABLE_ASYNC_DNS
  57. class AbstractCommand : public Command {
  58. private:
  59. Timer checkPoint_;
  60. time_t timeout_;
  61. RequestGroup* requestGroup_;
  62. SharedHandle<Request> req_;
  63. SharedHandle<FileEntry> fileEntry_;
  64. DownloadEngine* e_;
  65. SharedHandle<SocketCore> socket_;
  66. std::vector<SharedHandle<Segment> > segments_;
  67. #ifdef ENABLE_ASYNC_DNS
  68. SharedHandle<AsyncNameResolver> asyncNameResolver_;
  69. #endif // ENABLE_ASYNC_DNS
  70. bool checkSocketIsReadable_;
  71. bool checkSocketIsWritable_;
  72. SharedHandle<SocketCore> readCheckTarget_;
  73. SharedHandle<SocketCore> writeCheckTarget_;
  74. bool nameResolverCheck_;
  75. bool incNumConnection_;
  76. size_t calculateMinSplitSize() const;
  77. #ifdef ENABLE_ASYNC_DNS
  78. void setNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver);
  79. void disableNameResolverCheck
  80. (const SharedHandle<AsyncNameResolver>& resolver);
  81. bool nameResolveFinished() const;
  82. #endif // ENABLE_ASYNC_DNS
  83. protected:
  84. RequestGroup* getRequestGroup() const
  85. {
  86. return requestGroup_;
  87. }
  88. const SharedHandle<Request>& getRequest() const
  89. {
  90. return req_;
  91. }
  92. void setRequest(const SharedHandle<Request>& request);
  93. const SharedHandle<FileEntry>& getFileEntry() const
  94. {
  95. return fileEntry_;
  96. }
  97. void setFileEntry(const SharedHandle<FileEntry>& fileEntry);
  98. DownloadEngine* getDownloadEngine() const
  99. {
  100. return e_;
  101. }
  102. const SharedHandle<SocketCore>& getSocket() const
  103. {
  104. return socket_;
  105. }
  106. void setSocket(const SharedHandle<SocketCore>& s);
  107. void createSocket();
  108. const std::vector<SharedHandle<Segment> >& getSegments() const
  109. {
  110. return segments_;
  111. }
  112. #ifdef ENABLE_ASYNC_DNS
  113. bool isAsyncNameResolverInitialized() const;
  114. void initAsyncNameResolver(const std::string& hostname);
  115. bool asyncResolveHostname();
  116. const std::vector<std::string>& getResolvedAddresses();
  117. #endif // ENABLE_ASYNC_DNS
  118. // Resolves hostname. The resolved addresses are stored in addrs
  119. // and first element is returned. If resolve is not finished,
  120. // return empty string. In this case, call this function with same
  121. // arguments until resolved address is returned. Exception is
  122. // thrown on error. port is used for retrieving cached addresses.
  123. std::string resolveHostname
  124. (std::vector<std::string>& addrs, const std::string& hostname, uint16_t port);
  125. void tryReserved();
  126. virtual bool prepareForRetry(time_t wait);
  127. virtual void onAbort();
  128. virtual bool executeInternal() = 0;
  129. void setReadCheckSocket(const SharedHandle<SocketCore>& socket);
  130. void setWriteCheckSocket(const SharedHandle<SocketCore>& socket);
  131. void disableReadCheckSocket();
  132. void disableWriteCheckSocket();
  133. /**
  134. * If pred == true, calls setReadCheckSocket(socket). Otherwise, calls
  135. * disableReadCheckSocket().
  136. */
  137. void setReadCheckSocketIf(const SharedHandle<SocketCore>& socket, bool pred);
  138. /**
  139. * If pred == true, calls setWriteCheckSocket(socket). Otherwise, calls
  140. * disableWriteCheckSocket().
  141. */
  142. void setWriteCheckSocketIf(const SharedHandle<SocketCore>& socket, bool pred);
  143. time_t getTimeout() const
  144. {
  145. return timeout_;
  146. }
  147. void setTimeout(time_t timeout) { timeout_ = timeout; }
  148. void prepareForNextAction
  149. (const SharedHandle<CheckIntegrityEntry>& checkEntry);
  150. // Check if socket is connected. If socket is not connected and
  151. // there are other addresses to try, command is created using
  152. // InitiateConnectionCommandFactory and it is pushed to
  153. // DownloadEngine and returns false. If no addresses left, DlRetryEx
  154. // exception is thrown.
  155. bool checkIfConnectionEstablished
  156. (const SharedHandle<SocketCore>& socket,
  157. const std::string& connectedHostname,
  158. const std::string& connectedAddr,
  159. uint16_t connectedPort);
  160. /*
  161. * Returns true if proxy for the procol indicated by Request::getProtocol()
  162. * is defined. Otherwise, returns false.
  163. */
  164. bool isProxyDefined() const;
  165. /*
  166. * Creates Request object for proxy URI and returns it.
  167. * If no valid proxy is defined, then returns SharedHandle<Request>().
  168. */
  169. SharedHandle<Request> createProxyRequest() const;
  170. // Returns proxy method for given protocol. Either V_GET or V_TUNNEL
  171. // is returned. For HTTPS, always returns V_TUNNEL.
  172. const std::string& resolveProxyMethod(const std::string& protocol) const;
  173. const SharedHandle<Option>& getOption() const;
  174. const SharedHandle<DownloadContext>& getDownloadContext() const;
  175. const SharedHandle<SegmentMan>& getSegmentMan() const;
  176. const SharedHandle<PieceStorage>& getPieceStorage() const;
  177. Timer& getCheckPoint()
  178. {
  179. return checkPoint_;
  180. }
  181. public:
  182. AbstractCommand
  183. (cuid_t cuid, const SharedHandle<Request>& req,
  184. const SharedHandle<FileEntry>& fileEntry,
  185. RequestGroup* requestGroup, DownloadEngine* e,
  186. const SharedHandle<SocketCore>& s = SharedHandle<SocketCore>(),
  187. bool incNumConnection = true);
  188. virtual ~AbstractCommand();
  189. bool execute();
  190. };
  191. } // namespace aria2
  192. #endif // D_ABSTRACT_COMMAND_H