BtLeecherStateChoke.cc 6.6 KB

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