AbstractCommand.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. class SocketRecvBuffer;
  55. #ifdef ENABLE_ASYNC_DNS
  56. class AsyncNameResolver;
  57. class AsyncNameResolverMan;
  58. #endif // ENABLE_ASYNC_DNS
  59. class AbstractCommand : public Command {
  60. private:
  61. Timer checkPoint_;
  62. time_t timeout_;
  63. RequestGroup* requestGroup_;
  64. SharedHandle<Request> req_;
  65. SharedHandle<FileEntry> fileEntry_;
  66. DownloadEngine* e_;
  67. SharedHandle<SocketCore> socket_;
  68. SharedHandle<SocketRecvBuffer> socketRecvBuffer_;
  69. std::vector<SharedHandle<Segment> > segments_;
  70. #ifdef ENABLE_ASYNC_DNS
  71. SharedHandle<AsyncNameResolverMan> asyncNameResolverMan_;
  72. #endif // ENABLE_ASYNC_DNS
  73. bool checkSocketIsReadable_;
  74. bool checkSocketIsWritable_;
  75. SharedHandle<SocketCore> readCheckTarget_;
  76. SharedHandle<SocketCore> writeCheckTarget_;
  77. bool incNumConnection_;
  78. Timer serverStatTimer_;
  79. int32_t calculateMinSplitSize() const;
  80. void useFasterRequest(const SharedHandle<Request>& fasterRequest);
  81. public:
  82. RequestGroup* getRequestGroup() const
  83. {
  84. return requestGroup_;
  85. }
  86. const SharedHandle<Request>& getRequest() const
  87. {
  88. return req_;
  89. }
  90. void setRequest(const SharedHandle<Request>& request);
  91. // Resets request_. This method is more efficient than
  92. // setRequest(SharedHandle<Request>());
  93. void resetRequest();
  94. const SharedHandle<FileEntry>& getFileEntry() const
  95. {
  96. return fileEntry_;
  97. }
  98. void setFileEntry(const SharedHandle<FileEntry>& fileEntry);
  99. DownloadEngine* getDownloadEngine() const
  100. {
  101. return e_;
  102. }
  103. const SharedHandle<SocketCore>& getSocket() const
  104. {
  105. return socket_;
  106. }
  107. SharedHandle<SocketCore>& getSocket()
  108. {
  109. return socket_;
  110. }
  111. void setSocket(const SharedHandle<SocketCore>& s);
  112. void createSocket();
  113. const SharedHandle<SocketRecvBuffer>& getSocketRecvBuffer() const
  114. {
  115. return socketRecvBuffer_;
  116. }
  117. const std::vector<SharedHandle<Segment> >& getSegments() const
  118. {
  119. return segments_;
  120. }
  121. // Resolves hostname. The resolved addresses are stored in addrs
  122. // and first element is returned. If resolve is not finished,
  123. // return empty string. In this case, call this function with same
  124. // arguments until resolved address is returned. Exception is
  125. // thrown on error. port is used for retrieving cached addresses.
  126. std::string resolveHostname
  127. (std::vector<std::string>& addrs, const std::string& hostname, uint16_t port);
  128. void tryReserved();
  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. // Swaps socket_ with socket. This disables current read and write
  144. // check.
  145. void swapSocket(SharedHandle<SocketCore>& socket);
  146. time_t getTimeout() const
  147. {
  148. return timeout_;
  149. }
  150. void setTimeout(time_t timeout) { timeout_ = timeout; }
  151. void prepareForNextAction
  152. (const SharedHandle<CheckIntegrityEntry>& checkEntry);
  153. // Check if socket is connected. If socket is not connected and
  154. // there are other addresses to try, command is created using
  155. // InitiateConnectionCommandFactory and it is pushed to
  156. // DownloadEngine and returns false. If no addresses left, DlRetryEx
  157. // exception is thrown.
  158. bool checkIfConnectionEstablished
  159. (const SharedHandle<SocketCore>& socket,
  160. const std::string& connectedHostname,
  161. const std::string& connectedAddr,
  162. uint16_t connectedPort);
  163. /*
  164. * Returns true if proxy for the procol indicated by Request::getProtocol()
  165. * is defined. Otherwise, returns false.
  166. */
  167. bool isProxyDefined() const;
  168. /*
  169. * Creates Request object for proxy URI and returns it.
  170. * If no valid proxy is defined, then returns SharedHandle<Request>().
  171. */
  172. SharedHandle<Request> createProxyRequest() const;
  173. // Returns proxy method for given protocol. Either V_GET or V_TUNNEL
  174. // is returned. For HTTPS, always returns V_TUNNEL.
  175. const std::string& resolveProxyMethod(const std::string& protocol) const;
  176. const SharedHandle<Option>& getOption() const;
  177. const SharedHandle<DownloadContext>& getDownloadContext() const;
  178. const SharedHandle<SegmentMan>& getSegmentMan() const;
  179. const SharedHandle<PieceStorage>& getPieceStorage() const;
  180. Timer& getCheckPoint()
  181. {
  182. return checkPoint_;
  183. }
  184. void checkSocketRecvBuffer();
  185. protected:
  186. virtual bool prepareForRetry(time_t wait);
  187. virtual void onAbort();
  188. virtual bool executeInternal() = 0;
  189. // Returns true if the derived class wants to execute
  190. // executeInternal() unconditionally
  191. virtual bool noCheck()
  192. {
  193. return false;
  194. }
  195. public:
  196. AbstractCommand
  197. (cuid_t cuid, const SharedHandle<Request>& req,
  198. const SharedHandle<FileEntry>& fileEntry,
  199. RequestGroup* requestGroup, DownloadEngine* e,
  200. const SharedHandle<SocketCore>& s = SharedHandle<SocketCore>(),
  201. const SharedHandle<SocketRecvBuffer>& socketRecvBuffer
  202. = SharedHandle<SocketRecvBuffer>(),
  203. bool incNumConnection = true);
  204. virtual ~AbstractCommand();
  205. bool execute();
  206. };
  207. // Returns proxy URI for given protocol. If no proxy URI is defined,
  208. // then returns an empty string.
  209. std::string getProxyUri
  210. (const std::string& protocol, const Option* option);
  211. } // namespace aria2
  212. #endif // D_ABSTRACT_COMMAND_H