TrackerWatcherCommand.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_TRACKER_WATCHER_COMMAND_H
  36. #define D_TRACKER_WATCHER_COMMAND_H
  37. #include "Command.h"
  38. #include <string>
  39. #include <memory>
  40. namespace aria2 {
  41. class DownloadEngine;
  42. class RequestGroup;
  43. class PeerStorage;
  44. class PieceStorage;
  45. class BtRuntime;
  46. class BtAnnounce;
  47. class Option;
  48. struct UDPTrackerRequest;
  49. class UDPTrackerClient;
  50. class AnnRequest {
  51. public:
  52. virtual ~AnnRequest() {}
  53. // Returns true if tracker request is finished, regardless of the
  54. // outcome.
  55. virtual bool stopped() const = 0;
  56. // Returns true if tracker request is successful.
  57. virtual bool success() const = 0;
  58. // Returns true if issuing request is successful.
  59. virtual bool issue(DownloadEngine* e) = 0;
  60. // Stop this request.
  61. virtual void stop(DownloadEngine* e) = 0;
  62. // Returns true if processing tracker response is successful.
  63. virtual bool
  64. processResponse(const std::shared_ptr<BtAnnounce>& btAnnounce) = 0;
  65. };
  66. class HTTPAnnRequest : public AnnRequest {
  67. public:
  68. HTTPAnnRequest(std::unique_ptr<RequestGroup> rg);
  69. virtual ~HTTPAnnRequest();
  70. virtual bool stopped() const CXX11_OVERRIDE;
  71. virtual bool success() const CXX11_OVERRIDE;
  72. virtual bool issue(DownloadEngine* e) CXX11_OVERRIDE;
  73. virtual void stop(DownloadEngine* e) CXX11_OVERRIDE;
  74. virtual bool
  75. processResponse(const std::shared_ptr<BtAnnounce>& btAnnounce) CXX11_OVERRIDE;
  76. private:
  77. std::unique_ptr<RequestGroup> rg_;
  78. };
  79. class UDPAnnRequest : public AnnRequest {
  80. public:
  81. UDPAnnRequest(const std::shared_ptr<UDPTrackerRequest>& req);
  82. virtual ~UDPAnnRequest();
  83. virtual bool stopped() const CXX11_OVERRIDE;
  84. virtual bool success() const CXX11_OVERRIDE;
  85. virtual bool issue(DownloadEngine* e) CXX11_OVERRIDE;
  86. virtual void stop(DownloadEngine* e) CXX11_OVERRIDE;
  87. virtual bool
  88. processResponse(const std::shared_ptr<BtAnnounce>& btAnnounce) CXX11_OVERRIDE;
  89. private:
  90. std::shared_ptr<UDPTrackerRequest> req_;
  91. };
  92. class TrackerWatcherCommand : public Command {
  93. private:
  94. RequestGroup* requestGroup_;
  95. DownloadEngine* e_;
  96. std::shared_ptr<UDPTrackerClient> udpTrackerClient_;
  97. std::shared_ptr<PeerStorage> peerStorage_;
  98. std::shared_ptr<PieceStorage> pieceStorage_;
  99. std::shared_ptr<BtRuntime> btRuntime_;
  100. std::shared_ptr<BtAnnounce> btAnnounce_;
  101. std::unique_ptr<AnnRequest> trackerRequest_;
  102. /**
  103. * Returns a command for announce request. Returns 0 if no announce request
  104. * is needed.
  105. */
  106. std::unique_ptr<AnnRequest> createHTTPAnnRequest(const std::string& uri);
  107. std::unique_ptr<AnnRequest> createUDPAnnRequest(const std::string& host,
  108. uint16_t port,
  109. uint16_t localPort);
  110. void addConnection();
  111. const std::shared_ptr<Option>& getOption() const;
  112. public:
  113. TrackerWatcherCommand(cuid_t cuid, RequestGroup* requestGroup,
  114. DownloadEngine* e);
  115. virtual ~TrackerWatcherCommand();
  116. std::unique_ptr<AnnRequest> createAnnounce(DownloadEngine* e);
  117. virtual bool execute() CXX11_OVERRIDE;
  118. void setPeerStorage(const std::shared_ptr<PeerStorage>& peerStorage);
  119. void setPieceStorage(const std::shared_ptr<PieceStorage>& pieceStorage);
  120. void setBtRuntime(const std::shared_ptr<BtRuntime>& btRuntime);
  121. void setBtAnnounce(const std::shared_ptr<BtAnnounce>& btAnnounce);
  122. };
  123. } // namespace aria2
  124. #endif // D_TRACKER_WATCHER_COMMAND_H