BtLeecherStateChoke.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "BtLeecherStateChoke.h"
  36. #include <algorithm>
  37. #include "Peer.h"
  38. #include "Logger.h"
  39. #include "LogFactory.h"
  40. #include "SimpleRandomizer.h"
  41. #include "wallclock.h"
  42. namespace aria2 {
  43. BtLeecherStateChoke::BtLeecherStateChoke():
  44. round_(0),
  45. lastRound_(0),
  46. logger_(LogFactory::getInstance()) {}
  47. BtLeecherStateChoke::~BtLeecherStateChoke() {}
  48. BtLeecherStateChoke::PeerEntry::PeerEntry(const SharedHandle<Peer>& peer):
  49. peer_(peer), downloadSpeed_(peer->calculateDownloadSpeed()),
  50. // peer must be interested to us and sent block in the last 30 seconds
  51. regularUnchoker_
  52. (peer->peerInterested() &&
  53. peer->getLastDownloadUpdate().difference(global::wallclock) < 30) {}
  54. const SharedHandle<Peer>& BtLeecherStateChoke::PeerEntry::getPeer() const
  55. {
  56. return peer_;
  57. }
  58. unsigned int BtLeecherStateChoke::PeerEntry::getDownloadSpeed() const
  59. {
  60. return downloadSpeed_;
  61. }
  62. bool BtLeecherStateChoke::PeerEntry::isRegularUnchoker() const
  63. {
  64. return regularUnchoker_;
  65. }
  66. void BtLeecherStateChoke::PeerEntry::enableChokingRequired()
  67. {
  68. peer_->chokingRequired(true);
  69. }
  70. void BtLeecherStateChoke::PeerEntry::disableChokingRequired()
  71. {
  72. peer_->chokingRequired(false);
  73. }
  74. void BtLeecherStateChoke::PeerEntry::enableOptUnchoking()
  75. {
  76. peer_->optUnchoking(true);
  77. }
  78. void BtLeecherStateChoke::PeerEntry::disableOptUnchoking()
  79. {
  80. peer_->optUnchoking(false);
  81. }
  82. bool BtLeecherStateChoke::PeerEntry::isSnubbing() const
  83. {
  84. return peer_->snubbing();
  85. }
  86. bool BtLeecherStateChoke::PeerEntry::operator<(const PeerEntry& peerEntry) const
  87. {
  88. return downloadSpeed_ > peerEntry.downloadSpeed_;
  89. }
  90. bool BtLeecherStateChoke::PeerFilter::operator()
  91. (const PeerEntry& peerEntry) const
  92. {
  93. return peerEntry.getPeer()->amChoking() == amChoking_ &&
  94. peerEntry.getPeer()->peerInterested() == peerInterested_;
  95. }
  96. void BtLeecherStateChoke::plannedOptimisticUnchoke
  97. (std::vector<PeerEntry>& peerEntries)
  98. {
  99. std::for_each(peerEntries.begin(), peerEntries.end(),
  100. std::mem_fun_ref(&PeerEntry::disableOptUnchoking));
  101. std::vector<PeerEntry>::iterator i =
  102. std::partition(peerEntries.begin(), peerEntries.end(),
  103. PeerFilter(true, true));
  104. if(i != peerEntries.begin()) {
  105. std::random_shuffle(peerEntries.begin(), i,
  106. *(SimpleRandomizer::getInstance().get()));
  107. (*peerEntries.begin()).enableOptUnchoking();
  108. logger_->info
  109. ("POU: %s", (*peerEntries.begin()).getPeer()->getIPAddress().c_str());
  110. }
  111. }
  112. void BtLeecherStateChoke::regularUnchoke(std::vector<PeerEntry>& peerEntries)
  113. {
  114. std::vector<PeerEntry>::iterator rest =
  115. std::partition(peerEntries.begin(), peerEntries.end(),
  116. std::mem_fun_ref(&PeerEntry::isRegularUnchoker));
  117. std::sort(peerEntries.begin(), rest);
  118. // the number of regular unchokers
  119. int count = 3;
  120. bool fastOptUnchoker = false;
  121. std::vector<PeerEntry>::iterator peerIter = peerEntries.begin();
  122. for(;peerIter != rest && count; ++peerIter, --count) {
  123. (*peerIter).disableChokingRequired();
  124. logger_->info("RU: %s, dlspd=%u",
  125. (*peerIter).getPeer()->getIPAddress().c_str(),
  126. (*peerIter).getDownloadSpeed());
  127. if((*peerIter).getPeer()->optUnchoking()) {
  128. fastOptUnchoker = true;
  129. (*peerIter).disableOptUnchoking();
  130. }
  131. }
  132. if(fastOptUnchoker) {
  133. std::random_shuffle(peerIter, peerEntries.end(),
  134. *(SimpleRandomizer::getInstance().get()));
  135. for(std::vector<PeerEntry>::iterator i = peerIter,
  136. eoi = peerEntries.end(); i != eoi; ++i) {
  137. if((*i).getPeer()->peerInterested()) {
  138. (*i).enableOptUnchoking();
  139. logger_->info("OU: %s", (*i).getPeer()->getIPAddress().c_str());
  140. break;
  141. } else {
  142. (*i).disableChokingRequired();
  143. logger_->info("OU: %s", (*i).getPeer()->getIPAddress().c_str());
  144. }
  145. }
  146. }
  147. }
  148. void
  149. BtLeecherStateChoke::executeChoke
  150. (const std::vector<SharedHandle<Peer> >& peerSet)
  151. {
  152. logger_->info("Leecher state, %d choke round started", round_);
  153. lastRound_ = global::wallclock;
  154. std::vector<PeerEntry> peerEntries;
  155. std::transform(peerSet.begin(), peerSet.end(),
  156. std::back_inserter(peerEntries),
  157. BtLeecherStateChokeGenPeerEntry());
  158. peerEntries.erase(std::remove_if(peerEntries.begin(), peerEntries.end(),
  159. std::mem_fun_ref(&PeerEntry::isSnubbing)),
  160. peerEntries.end());
  161. std::for_each(peerEntries.begin(), peerEntries.end(),
  162. std::mem_fun_ref(&PeerEntry::enableChokingRequired));
  163. // planned optimistic unchoke
  164. if(round_ == 0) {
  165. plannedOptimisticUnchoke(peerEntries);
  166. }
  167. regularUnchoke(peerEntries);
  168. if(++round_ == 3) {
  169. round_ = 0;
  170. }
  171. }
  172. const Timer& BtLeecherStateChoke::getLastRound() const
  173. {
  174. return lastRound_;
  175. }
  176. } // namespace aria2