AbstractCommand.h 6.8 KB

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