PeerChokeCommand.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "PeerChokeCommand.h"
  36. #include "Util.h"
  37. #include "Peer.h"
  38. #include "DownloadEngine.h"
  39. #include "BtContext.h"
  40. #include "BtRuntime.h"
  41. #include "PieceStorage.h"
  42. #include "PeerStorage.h"
  43. #include "Logger.h"
  44. #include <algorithm>
  45. namespace aria2 {
  46. PeerChokeCommand::PeerChokeCommand(int32_t cuid,
  47. RequestGroup* requestGroup,
  48. DownloadEngine* e,
  49. const BtContextHandle& btContext,
  50. int32_t interval):
  51. Command(cuid),
  52. BtContextAwareCommand(btContext),
  53. RequestGroupAware(requestGroup),
  54. interval(interval),
  55. e(e),
  56. rotate(0)
  57. {}
  58. PeerChokeCommand::~PeerChokeCommand() {}
  59. class ChokePeer {
  60. public:
  61. ChokePeer() {}
  62. void operator()(PeerHandle& peer) {
  63. peer->chokingRequired(true);
  64. }
  65. };
  66. void PeerChokeCommand::optUnchokingPeer(Peers& peers) const {
  67. if(peers.empty()) {
  68. return;
  69. }
  70. std::random_shuffle(peers.begin(), peers.end());
  71. int32_t optUnchokCount = 1;
  72. for(Peers::iterator itr = peers.begin(); itr != peers.end(); itr++) {
  73. Peers::value_type peer = *itr;
  74. if(optUnchokCount > 0 && !peer->snubbing()) {
  75. optUnchokCount--;
  76. peer->optUnchoking(true);
  77. logger->debug("opt, unchoking %s, download speed=%d",
  78. peer->ipaddr.c_str(), peer->calculateDownloadSpeed());
  79. } else {
  80. peer->optUnchoking(false);
  81. }
  82. }
  83. }
  84. class UploadFaster {
  85. public:
  86. bool operator() (const PeerHandle& left, const PeerHandle& right) const {
  87. return left->calculateUploadSpeed() > right->calculateUploadSpeed();
  88. }
  89. };
  90. void PeerChokeCommand::orderByUploadRate(Peers& peers) const {
  91. std::sort(peers.begin(), peers.end(), UploadFaster());
  92. }
  93. class DownloadFaster {
  94. public:
  95. bool operator() (const PeerHandle& left, const PeerHandle& right) const {
  96. return left->calculateDownloadSpeed() > right->calculateDownloadSpeed();
  97. }
  98. };
  99. void PeerChokeCommand::orderByDownloadRate(Peers& peers) const {
  100. std::sort(peers.begin(), peers.end(), DownloadFaster());
  101. }
  102. bool PeerChokeCommand::execute() {
  103. if(btRuntime->isHalt()) {
  104. return true;
  105. }
  106. if(checkPoint.elapsed(interval)) {
  107. checkPoint.reset();
  108. Peers peers = peerStorage->getActivePeers();
  109. std::for_each(peers.begin(), peers.end(), ChokePeer());
  110. if(pieceStorage->downloadFinished()) {
  111. orderByUploadRate(peers);
  112. } else {
  113. orderByDownloadRate(peers);
  114. }
  115. int32_t unchokingCount = 4;//peers.size() >= 4 ? 4 : peers.size();
  116. for(Peers::iterator itr = peers.begin(); itr != peers.end() && unchokingCount > 0; ) {
  117. PeerHandle peer = *itr;
  118. if(peer->peerInterested() && !peer->snubbing()) {
  119. unchokingCount--;
  120. peer->chokingRequired(false);
  121. peer->optUnchoking(false);
  122. itr = peers.erase(itr);
  123. if(pieceStorage->downloadFinished()) {
  124. logger->debug("cat01, unchoking %s, upload speed=%d",
  125. peer->ipaddr.c_str(),
  126. peer->calculateUploadSpeed());
  127. } else {
  128. logger->debug("cat01, unchoking %s, download speed=%d",
  129. peer->ipaddr.c_str(),
  130. peer->calculateDownloadSpeed());
  131. }
  132. } else {
  133. itr++;
  134. }
  135. }
  136. for(Peers::iterator itr = peers.begin(); itr != peers.end(); ) {
  137. PeerHandle peer = *itr;
  138. if(!peer->peerInterested() && !peer->snubbing()) {
  139. peer->chokingRequired(false);
  140. peer->optUnchoking(false);
  141. itr = peers.erase(itr);
  142. if(pieceStorage->downloadFinished()) {
  143. logger->debug("cat02, unchoking %s, upload speed=%d",
  144. peer->ipaddr.c_str(),
  145. peer->calculateUploadSpeed());
  146. } else {
  147. logger->debug("cat02, unchoking %s, download speed=%d",
  148. peer->ipaddr.c_str(),
  149. peer->calculateDownloadSpeed());
  150. }
  151. break;
  152. } else {
  153. itr++;
  154. }
  155. }
  156. if(rotate%3 == 0) {
  157. optUnchokingPeer(peers);
  158. rotate = 0;
  159. }
  160. rotate++;
  161. }
  162. e->commands.push_back(this);
  163. return false;
  164. }
  165. } // namespace aria2