BtLeecherStateChoke.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. class PeerFilter {
  91. private:
  92. bool amChoking_;
  93. bool peerInterested_;
  94. public:
  95. PeerFilter(bool amChoking, bool peerInterested):
  96. amChoking_(amChoking),
  97. peerInterested_(peerInterested) {}
  98. bool operator()(const BtLeecherStateChoke::PeerEntry& peerEntry) const
  99. {
  100. return peerEntry.getPeer()->amChoking() == amChoking_ &&
  101. peerEntry.getPeer()->peerInterested() == peerInterested_;
  102. }
  103. };
  104. void BtLeecherStateChoke::plannedOptimisticUnchoke
  105. (std::vector<PeerEntry>& peerEntries)
  106. {
  107. std::for_each(peerEntries.begin(), peerEntries.end(),
  108. std::mem_fun_ref(&PeerEntry::disableOptUnchoking));
  109. std::vector<PeerEntry>::iterator i =
  110. std::partition(peerEntries.begin(), peerEntries.end(),
  111. PeerFilter(true, true));
  112. if(i != peerEntries.begin()) {
  113. std::random_shuffle(peerEntries.begin(), i,
  114. *(SimpleRandomizer::getInstance().get()));
  115. (*peerEntries.begin()).enableOptUnchoking();
  116. logger_->info
  117. ("POU: %s", (*peerEntries.begin()).getPeer()->getIPAddress().c_str());
  118. }
  119. }
  120. void BtLeecherStateChoke::regularUnchoke(std::vector<PeerEntry>& peerEntries)
  121. {
  122. std::vector<PeerEntry>::iterator rest =
  123. std::partition(peerEntries.begin(), peerEntries.end(),
  124. std::mem_fun_ref(&PeerEntry::isRegularUnchoker));
  125. std::sort(peerEntries.begin(), rest);
  126. // the number of regular unchokers
  127. int count = 3;
  128. bool fastOptUnchoker = false;
  129. std::vector<PeerEntry>::iterator peerIter = peerEntries.begin();
  130. for(;peerIter != rest && count; ++peerIter, --count) {
  131. (*peerIter).disableChokingRequired();
  132. logger_->info("RU: %s, dlspd=%u",
  133. (*peerIter).getPeer()->getIPAddress().c_str(),
  134. (*peerIter).getDownloadSpeed());
  135. if((*peerIter).getPeer()->optUnchoking()) {
  136. fastOptUnchoker = true;
  137. (*peerIter).disableOptUnchoking();
  138. }
  139. }
  140. if(fastOptUnchoker) {
  141. std::random_shuffle(peerIter, peerEntries.end(),
  142. *(SimpleRandomizer::getInstance().get()));
  143. for(std::vector<PeerEntry>::iterator i = peerIter,
  144. eoi = peerEntries.end(); i != eoi; ++i) {
  145. if((*i).getPeer()->peerInterested()) {
  146. (*i).enableOptUnchoking();
  147. logger_->info("OU: %s", (*i).getPeer()->getIPAddress().c_str());
  148. break;
  149. } else {
  150. (*i).disableChokingRequired();
  151. logger_->info("OU: %s", (*i).getPeer()->getIPAddress().c_str());
  152. }
  153. }
  154. }
  155. }
  156. class BtLeecherStateChokeGenPeerEntry {
  157. public:
  158. BtLeecherStateChoke::PeerEntry operator()
  159. (const SharedHandle<Peer>& peer) const
  160. {
  161. return BtLeecherStateChoke::PeerEntry(peer);
  162. }
  163. };
  164. void
  165. BtLeecherStateChoke::executeChoke
  166. (const std::vector<SharedHandle<Peer> >& peerSet)
  167. {
  168. logger_->info("Leecher state, %d choke round started", round_);
  169. lastRound_ = global::wallclock;
  170. std::vector<PeerEntry> peerEntries;
  171. std::transform(peerSet.begin(), peerSet.end(),
  172. std::back_inserter(peerEntries),
  173. BtLeecherStateChokeGenPeerEntry());
  174. peerEntries.erase(std::remove_if(peerEntries.begin(), peerEntries.end(),
  175. std::mem_fun_ref(&PeerEntry::isSnubbing)),
  176. peerEntries.end());
  177. std::for_each(peerEntries.begin(), peerEntries.end(),
  178. std::mem_fun_ref(&PeerEntry::enableChokingRequired));
  179. // planned optimistic unchoke
  180. if(round_ == 0) {
  181. plannedOptimisticUnchoke(peerEntries);
  182. }
  183. regularUnchoke(peerEntries);
  184. if(++round_ == 3) {
  185. round_ = 0;
  186. }
  187. }
  188. const Timer& BtLeecherStateChoke::getLastRound() const
  189. {
  190. return lastRound_;
  191. }
  192. } // namespace aria2