AbstractCommand.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. protected:
  57. RequestGroup* _requestGroup;
  58. SharedHandle<Request> req;
  59. SharedHandle<FileEntry> _fileEntry;
  60. DownloadEngine* e;
  61. SharedHandle<SocketCore> socket;
  62. std::vector<SharedHandle<Segment> > _segments;
  63. #ifdef ENABLE_ASYNC_DNS
  64. SharedHandle<AsyncNameResolver> _asyncNameResolver;
  65. bool isAsyncNameResolverInitialized() const;
  66. void initAsyncNameResolver(const std::string& hostname);
  67. bool asyncResolveHostname();
  68. const std::vector<std::string>& getResolvedAddresses();
  69. #endif // ENABLE_ASYNC_DNS
  70. // Resolves hostname. The resolved addresses are stored in addrs
  71. // and first element is returned. If resolve is not finished,
  72. // return empty string. In this case, call this function with same
  73. // arguments until resolved address is returned. Exception is
  74. // thrown on error. port is used for retrieving cached addresses.
  75. std::string resolveHostname
  76. (std::vector<std::string>& addrs, const std::string& hostname, uint16_t port);
  77. void tryReserved();
  78. virtual bool prepareForRetry(time_t wait);
  79. virtual void onAbort();
  80. virtual bool executeInternal() = 0;
  81. void setReadCheckSocket(const SharedHandle<SocketCore>& socket);
  82. void setWriteCheckSocket(const SharedHandle<SocketCore>& socket);
  83. void disableReadCheckSocket();
  84. void disableWriteCheckSocket();
  85. /**
  86. * If pred == true, calls setReadCheckSocket(socket). Otherwise, calls
  87. * disableReadCheckSocket().
  88. */
  89. void setReadCheckSocketIf(const SharedHandle<SocketCore>& socket, bool pred);
  90. /**
  91. * If pred == true, calls setWriteCheckSocket(socket). Otherwise, calls
  92. * disableWriteCheckSocket().
  93. */
  94. void setWriteCheckSocketIf(const SharedHandle<SocketCore>& socket, bool pred);
  95. void setTimeout(time_t timeout) { this->timeout = timeout; }
  96. void prepareForNextAction(Command* nextCommand = 0);
  97. // Check if socket is connected. If socket is not connected and
  98. // there are other addresses to try, command is created using
  99. // InitiateConnectionCommandFactory and it is pushed to
  100. // DownloadEngine and returns false. If no addresses left, DlRetryEx
  101. // exception is thrown.
  102. bool checkIfConnectionEstablished
  103. (const SharedHandle<SocketCore>& socket,
  104. const std::string& connectedHostname,
  105. const std::string& connectedAddr,
  106. uint16_t connectedPort);
  107. /*
  108. * Returns true if proxy for the procol indicated by Request::getProtocol()
  109. * is defined. Otherwise, returns false.
  110. */
  111. bool isProxyDefined() const;
  112. /*
  113. * Creates Request object for proxy URI and returns it.
  114. * If no valid proxy is defined, then returns SharedHandle<Request>().
  115. */
  116. SharedHandle<Request> createProxyRequest() const;
  117. // Returns proxy method for given protocol. Either V_GET or V_TUNNEL
  118. // is returned. For HTTPS, always returns V_TUNNEL.
  119. const std::string& resolveProxyMethod(const std::string& protocol) const;
  120. const SharedHandle<Option>& getOption() const;
  121. const SharedHandle<DownloadContext>& getDownloadContext() const
  122. {
  123. return _requestGroup->getDownloadContext();
  124. }
  125. private:
  126. bool checkSocketIsReadable;
  127. bool checkSocketIsWritable;
  128. SharedHandle<SocketCore> readCheckTarget;
  129. SharedHandle<SocketCore> writeCheckTarget;
  130. bool nameResolverCheck;
  131. #ifdef ENABLE_ASYNC_DNS
  132. void setNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver);
  133. void disableNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver);
  134. bool nameResolveFinished() const;
  135. #endif // ENABLE_ASYNC_DNS
  136. public:
  137. AbstractCommand(cuid_t cuid, const SharedHandle<Request>& req,
  138. const SharedHandle<FileEntry>& fileEntry,
  139. RequestGroup* requestGroup, DownloadEngine* e,
  140. const SharedHandle<SocketCore>& s = SharedHandle<SocketCore>());
  141. virtual ~AbstractCommand();
  142. bool execute();
  143. };
  144. } // namespace aria2
  145. #endif // _D_ABSTRACT_COMMAND_H_