PeerChokeCommand.cc 4.9 KB

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