AbstractCommand.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "TimeA2.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. Time 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. void tryReserved();
  71. virtual bool prepareForRetry(time_t wait);
  72. virtual void onAbort();
  73. virtual bool executeInternal() = 0;
  74. void setReadCheckSocket(const SharedHandle<SocketCore>& socket);
  75. void setWriteCheckSocket(const SharedHandle<SocketCore>& socket);
  76. void disableReadCheckSocket();
  77. void disableWriteCheckSocket();
  78. /**
  79. * If pred == true, calls setReadCheckSocket(socket). Otherwise, calls
  80. * disableReadCheckSocket().
  81. */
  82. void setReadCheckSocketIf(const SharedHandle<SocketCore>& socket, bool pred);
  83. /**
  84. * If pred == true, calls setWriteCheckSocket(socket). Otherwise, calls
  85. * disableWriteCheckSocket().
  86. */
  87. void setWriteCheckSocketIf(const SharedHandle<SocketCore>& socket, bool pred);
  88. void setTimeout(time_t timeout) { this->timeout = timeout; }
  89. void prepareForNextAction(Command* nextCommand = 0);
  90. // Check if socket is connected. If socket is not connected and
  91. // there are other addresses to try, command is created using
  92. // InitiateConnectionCommandFactory and it is pushed to
  93. // DownloadEngine and returns false. If no addresses left, DlRetryEx
  94. // exception is thrown.
  95. bool checkIfConnectionEstablished
  96. (const SharedHandle<SocketCore>& socket,
  97. const std::string& connectedHostname,
  98. const std::string& connectedAddr,
  99. uint16_t connectedPort);
  100. /*
  101. * Returns true if proxy for the procol indicated by Request::getProtocol()
  102. * is defined. Otherwise, returns false.
  103. */
  104. bool isProxyDefined() const;
  105. /*
  106. * Creates Request object for proxy URI and returns it.
  107. * If no valid proxy is defined, then returns SharedHandle<Request>().
  108. */
  109. SharedHandle<Request> createProxyRequest() const;
  110. // Returns proxy method for given protocol. Either V_GET or V_TUNNEL
  111. // is returned. For HTTPS, always returns V_TUNNEL.
  112. const std::string& resolveProxyMethod(const std::string& protocol) const;
  113. const SharedHandle<Option>& getOption() const;
  114. const SharedHandle<DownloadContext>& getDownloadContext() const
  115. {
  116. return _requestGroup->getDownloadContext();
  117. }
  118. private:
  119. bool checkSocketIsReadable;
  120. bool checkSocketIsWritable;
  121. SharedHandle<SocketCore> readCheckTarget;
  122. SharedHandle<SocketCore> writeCheckTarget;
  123. bool nameResolverCheck;
  124. #ifdef ENABLE_ASYNC_DNS
  125. void setNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver);
  126. void disableNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver);
  127. bool nameResolveFinished() const;
  128. #endif // ENABLE_ASYNC_DNS
  129. public:
  130. AbstractCommand(int32_t cuid, const SharedHandle<Request>& req,
  131. const SharedHandle<FileEntry>& fileEntry,
  132. RequestGroup* requestGroup, DownloadEngine* e,
  133. const SharedHandle<SocketCore>& s = SharedHandle<SocketCore>());
  134. virtual ~AbstractCommand();
  135. bool execute();
  136. };
  137. } // namespace aria2
  138. #endif // _D_ABSTRACT_COMMAND_H_