ActivePeerConnectionCommand.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #include "ActivePeerConnectionCommand.h"
  36. #include "PeerInitiateConnectionCommand.h"
  37. #include "message.h"
  38. #include "DownloadEngine.h"
  39. #include "PeerStorage.h"
  40. #include "PieceStorage.h"
  41. #include "BtRuntime.h"
  42. #include "Peer.h"
  43. #include "Logger.h"
  44. #include "LogFactory.h"
  45. #include "prefs.h"
  46. #include "Option.h"
  47. #include "BtConstants.h"
  48. #include "SocketCore.h"
  49. #include "BtAnnounce.h"
  50. #include "RequestGroup.h"
  51. #include "DownloadContext.h"
  52. #include "bittorrent_helper.h"
  53. #include "wallclock.h"
  54. #include "util.h"
  55. #include "fmt.h"
  56. namespace aria2 {
  57. ActivePeerConnectionCommand::ActivePeerConnectionCommand(
  58. cuid_t cuid, RequestGroup* requestGroup, DownloadEngine* e,
  59. std::chrono::seconds interval)
  60. : Command(cuid),
  61. requestGroup_(requestGroup),
  62. interval_(std::move(interval)),
  63. e_(e),
  64. numNewConnection_(5)
  65. {
  66. requestGroup_->increaseNumCommand();
  67. }
  68. ActivePeerConnectionCommand::~ActivePeerConnectionCommand()
  69. {
  70. requestGroup_->decreaseNumCommand();
  71. }
  72. bool ActivePeerConnectionCommand::execute()
  73. {
  74. if (btRuntime_->isHalt()) {
  75. return true;
  76. }
  77. if (checkPoint_.difference(global::wallclock()) >= interval_) {
  78. checkPoint_ = global::wallclock();
  79. NetStat& stat = requestGroup_->getDownloadContext()->getNetStat();
  80. const int maxDownloadLimit = requestGroup_->getMaxDownloadSpeedLimit();
  81. const int maxUploadLimit = requestGroup_->getMaxUploadSpeedLimit();
  82. int thresholdSpeed;
  83. if (!bittorrent::getTorrentAttrs(requestGroup_->getDownloadContext())
  84. ->metadata.empty()) {
  85. thresholdSpeed = requestGroup_->getOption()->getAsInt(
  86. PREF_BT_REQUEST_PEER_SPEED_LIMIT);
  87. }
  88. else {
  89. thresholdSpeed = 0;
  90. }
  91. if (maxDownloadLimit > 0) {
  92. thresholdSpeed = std::min(maxDownloadLimit, thresholdSpeed);
  93. }
  94. if ( // for seeder state
  95. (pieceStorage_->downloadFinished() && btRuntime_->lessThanMaxPeers() &&
  96. (maxUploadLimit == 0 ||
  97. stat.calculateUploadSpeed() < maxUploadLimit * 0.8)) ||
  98. // for leecher state
  99. (!pieceStorage_->downloadFinished() &&
  100. (stat.calculateDownloadSpeed() < thresholdSpeed ||
  101. btRuntime_->lessThanMinPeers()))) {
  102. int numConnection = 0;
  103. if (pieceStorage_->downloadFinished()) {
  104. if (btRuntime_->getMaxPeers() > btRuntime_->getConnections()) {
  105. numConnection =
  106. std::min(numNewConnection_, btRuntime_->getMaxPeers() -
  107. btRuntime_->getConnections());
  108. }
  109. }
  110. else {
  111. numConnection = numNewConnection_;
  112. }
  113. makeNewConnections(numConnection);
  114. if (btRuntime_->getConnections() == 0 &&
  115. !pieceStorage_->downloadFinished()) {
  116. btAnnounce_->overrideMinInterval(BtAnnounce::DEFAULT_ANNOUNCE_INTERVAL);
  117. }
  118. }
  119. }
  120. e_->addCommand(std::unique_ptr<Command>(this));
  121. return false;
  122. }
  123. void ActivePeerConnectionCommand::makeNewConnections(int num)
  124. {
  125. for (; num && peerStorage_->isPeerAvailable(); --num) {
  126. cuid_t ncuid = e_->newCUID();
  127. std::shared_ptr<Peer> peer = peerStorage_->checkoutPeer(ncuid);
  128. // sanity check
  129. if (!peer) {
  130. break;
  131. }
  132. auto command = make_unique<PeerInitiateConnectionCommand>(
  133. ncuid, requestGroup_, peer, e_, btRuntime_);
  134. command->setPeerStorage(peerStorage_);
  135. command->setPieceStorage(pieceStorage_);
  136. e_->addCommand(std::move(command));
  137. A2_LOG_INFO(
  138. fmt(MSG_CONNECTING_TO_PEER, getCuid(), peer->getIPAddress().c_str()));
  139. }
  140. }
  141. void ActivePeerConnectionCommand::setBtRuntime(
  142. const std::shared_ptr<BtRuntime>& btRuntime)
  143. {
  144. btRuntime_ = btRuntime;
  145. }
  146. void ActivePeerConnectionCommand::setPieceStorage(
  147. const std::shared_ptr<PieceStorage>& pieceStorage)
  148. {
  149. pieceStorage_ = pieceStorage;
  150. }
  151. void ActivePeerConnectionCommand::setPeerStorage(
  152. const std::shared_ptr<PeerStorage>& peerStorage)
  153. {
  154. peerStorage_ = peerStorage;
  155. }
  156. void ActivePeerConnectionCommand::setBtAnnounce(
  157. const std::shared_ptr<BtAnnounce>& btAnnounce)
  158. {
  159. btAnnounce_ = btAnnounce;
  160. }
  161. } // namespace aria2