AbstractCommand.h 6.8 KB

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