BtLeecherStateChoke.cc 6.6 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 "a2time.h"
  41. #include "SimpleRandomizer.h"
  42. #include "wallclock.h"
  43. namespace aria2 {
  44. BtLeecherStateChoke::BtLeecherStateChoke():
  45. _round(0),
  46. _lastRound(0),
  47. _logger(LogFactory::getInstance()) {}
  48. BtLeecherStateChoke::~BtLeecherStateChoke() {}
  49. BtLeecherStateChoke::PeerEntry::PeerEntry(const SharedHandle<Peer>& peer):
  50. _peer(peer), _downloadSpeed(peer->calculateDownloadSpeed()),
  51. // peer must be interested to us and sent block in the last 30 seconds
  52. _regularUnchoker
  53. (peer->peerInterested() &&
  54. peer->getLastDownloadUpdate().difference(global::wallclock) < 30) {}
  55. const SharedHandle<Peer>& BtLeecherStateChoke::PeerEntry::getPeer() const
  56. {
  57. return _peer;
  58. }
  59. unsigned int BtLeecherStateChoke::PeerEntry::getDownloadSpeed() const
  60. {
  61. return _downloadSpeed;
  62. }
  63. bool BtLeecherStateChoke::PeerEntry::isRegularUnchoker() const
  64. {
  65. return _regularUnchoker;
  66. }
  67. void BtLeecherStateChoke::PeerEntry::enableChokingRequired()
  68. {
  69. _peer->chokingRequired(true);
  70. }
  71. void BtLeecherStateChoke::PeerEntry::disableChokingRequired()
  72. {
  73. _peer->chokingRequired(false);
  74. }
  75. void BtLeecherStateChoke::PeerEntry::enableOptUnchoking()
  76. {
  77. _peer->optUnchoking(true);
  78. }
  79. void BtLeecherStateChoke::PeerEntry::disableOptUnchoking()
  80. {
  81. _peer->optUnchoking(false);
  82. }
  83. bool BtLeecherStateChoke::PeerEntry::isSnubbing() const
  84. {
  85. return _peer->snubbing();
  86. }
  87. bool BtLeecherStateChoke::PeerEntry::operator<(const PeerEntry& peerEntry) const
  88. {
  89. return _downloadSpeed > peerEntry._downloadSpeed;
  90. }
  91. class PeerFilter {
  92. private:
  93. bool _amChoking;
  94. bool _peerInterested;
  95. public:
  96. PeerFilter(bool amChoking, bool peerInterested):
  97. _amChoking(amChoking),
  98. _peerInterested(peerInterested) {}
  99. bool operator()(const BtLeecherStateChoke::PeerEntry& peerEntry) const
  100. {
  101. return peerEntry.getPeer()->amChoking() == _amChoking &&
  102. peerEntry.getPeer()->peerInterested() == _peerInterested;
  103. }
  104. };
  105. void BtLeecherStateChoke::plannedOptimisticUnchoke
  106. (std::vector<PeerEntry>& peerEntries)
  107. {
  108. std::for_each(peerEntries.begin(), peerEntries.end(),
  109. std::mem_fun_ref(&PeerEntry::disableOptUnchoking));
  110. std::vector<PeerEntry>::iterator i =
  111. std::partition(peerEntries.begin(), peerEntries.end(),
  112. PeerFilter(true, true));
  113. if(i != peerEntries.begin()) {
  114. std::random_shuffle(peerEntries.begin(), i,
  115. *(SimpleRandomizer::getInstance().get()));
  116. (*peerEntries.begin()).enableOptUnchoking();
  117. _logger->info("POU: %s", (*peerEntries.begin()).getPeer()->ipaddr.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()->ipaddr.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()->ipaddr.c_str());
  148. break;
  149. } else {
  150. (*i).disableChokingRequired();
  151. _logger->info("OU: %s", (*i).getPeer()->ipaddr.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.reset();
  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 Time& BtLeecherStateChoke::getLastRound() const
  189. {
  190. return _lastRound;
  191. }
  192. } // namespace aria2