BtLeecherStateChoke.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #include "fmt.h"
  43. namespace aria2 {
  44. BtLeecherStateChoke::BtLeecherStateChoke()
  45. : round_(0),
  46. lastRound_(0)
  47. {}
  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. BtLeecherStateChoke::PeerEntry::PeerEntry(const PeerEntry& c)
  56. : peer_(c.peer_),
  57. downloadSpeed_(c.downloadSpeed_),
  58. regularUnchoker_(c.regularUnchoker_)
  59. {}
  60. void BtLeecherStateChoke::PeerEntry::swap(PeerEntry& c)
  61. {
  62. using std::swap;
  63. swap(peer_, c.peer_);
  64. swap(downloadSpeed_, c.downloadSpeed_);
  65. swap(regularUnchoker_, c.regularUnchoker_);
  66. }
  67. BtLeecherStateChoke::PeerEntry& BtLeecherStateChoke::PeerEntry::operator=
  68. (const PeerEntry& c)
  69. {
  70. if(this != &c) {
  71. peer_ = c.peer_;
  72. downloadSpeed_ = c.downloadSpeed_;
  73. regularUnchoker_ = c.regularUnchoker_;
  74. }
  75. return *this;
  76. }
  77. BtLeecherStateChoke::PeerEntry::~PeerEntry() {}
  78. const SharedHandle<Peer>& BtLeecherStateChoke::PeerEntry::getPeer() const
  79. {
  80. return peer_;
  81. }
  82. unsigned int BtLeecherStateChoke::PeerEntry::getDownloadSpeed() const
  83. {
  84. return downloadSpeed_;
  85. }
  86. bool BtLeecherStateChoke::PeerEntry::isRegularUnchoker() const
  87. {
  88. return regularUnchoker_;
  89. }
  90. void BtLeecherStateChoke::PeerEntry::enableChokingRequired()
  91. {
  92. peer_->chokingRequired(true);
  93. }
  94. void BtLeecherStateChoke::PeerEntry::disableChokingRequired()
  95. {
  96. peer_->chokingRequired(false);
  97. }
  98. void BtLeecherStateChoke::PeerEntry::enableOptUnchoking()
  99. {
  100. peer_->optUnchoking(true);
  101. }
  102. void BtLeecherStateChoke::PeerEntry::disableOptUnchoking()
  103. {
  104. peer_->optUnchoking(false);
  105. }
  106. bool BtLeecherStateChoke::PeerEntry::isSnubbing() const
  107. {
  108. return peer_->snubbing();
  109. }
  110. bool BtLeecherStateChoke::PeerEntry::operator<(const PeerEntry& peerEntry) const
  111. {
  112. return downloadSpeed_ > peerEntry.downloadSpeed_;
  113. }
  114. void swap
  115. (BtLeecherStateChoke::PeerEntry& a,
  116. BtLeecherStateChoke::PeerEntry& b)
  117. {
  118. a.swap(b);
  119. }
  120. bool BtLeecherStateChoke::PeerFilter::operator()
  121. (const PeerEntry& peerEntry) const
  122. {
  123. return peerEntry.getPeer()->amChoking() == amChoking_ &&
  124. peerEntry.getPeer()->peerInterested() == peerInterested_;
  125. }
  126. void BtLeecherStateChoke::plannedOptimisticUnchoke
  127. (std::vector<PeerEntry>& peerEntries)
  128. {
  129. std::for_each(peerEntries.begin(), peerEntries.end(),
  130. std::mem_fun_ref(&PeerEntry::disableOptUnchoking));
  131. std::vector<PeerEntry>::iterator i =
  132. std::partition(peerEntries.begin(), peerEntries.end(),
  133. PeerFilter(true, true));
  134. if(i != peerEntries.begin()) {
  135. std::random_shuffle(peerEntries.begin(), i,
  136. *(SimpleRandomizer::getInstance().get()));
  137. (*peerEntries.begin()).enableOptUnchoking();
  138. A2_LOG_INFO(fmt("POU: %s",
  139. (*peerEntries.begin()).getPeer()->getIPAddress().c_str()));
  140. }
  141. }
  142. void BtLeecherStateChoke::regularUnchoke(std::vector<PeerEntry>& peerEntries)
  143. {
  144. std::vector<PeerEntry>::iterator rest =
  145. std::partition(peerEntries.begin(), peerEntries.end(),
  146. std::mem_fun_ref(&PeerEntry::isRegularUnchoker));
  147. std::sort(peerEntries.begin(), rest);
  148. // the number of regular unchokers
  149. int count = 3;
  150. bool fastOptUnchoker = false;
  151. std::vector<PeerEntry>::iterator peerIter = peerEntries.begin();
  152. for(;peerIter != rest && count; ++peerIter, --count) {
  153. (*peerIter).disableChokingRequired();
  154. A2_LOG_INFO(fmt("RU: %s, dlspd=%u",
  155. (*peerIter).getPeer()->getIPAddress().c_str(),
  156. (*peerIter).getDownloadSpeed()));
  157. if((*peerIter).getPeer()->optUnchoking()) {
  158. fastOptUnchoker = true;
  159. (*peerIter).disableOptUnchoking();
  160. }
  161. }
  162. if(fastOptUnchoker) {
  163. std::random_shuffle(peerIter, peerEntries.end(),
  164. *(SimpleRandomizer::getInstance().get()));
  165. for(std::vector<PeerEntry>::iterator i = peerIter,
  166. eoi = peerEntries.end(); i != eoi; ++i) {
  167. if((*i).getPeer()->peerInterested()) {
  168. (*i).enableOptUnchoking();
  169. A2_LOG_INFO(fmt("OU: %s", (*i).getPeer()->getIPAddress().c_str()));
  170. break;
  171. } else {
  172. (*i).disableChokingRequired();
  173. A2_LOG_INFO(fmt("OU: %s", (*i).getPeer()->getIPAddress().c_str()));
  174. }
  175. }
  176. }
  177. }
  178. void
  179. BtLeecherStateChoke::executeChoke
  180. (const std::vector<SharedHandle<Peer> >& peerSet)
  181. {
  182. A2_LOG_INFO(fmt("Leecher state, %d choke round started", round_));
  183. lastRound_ = global::wallclock;
  184. std::vector<PeerEntry> peerEntries;
  185. std::transform(peerSet.begin(), peerSet.end(),
  186. std::back_inserter(peerEntries),
  187. BtLeecherStateChokeGenPeerEntry());
  188. peerEntries.erase(std::remove_if(peerEntries.begin(), peerEntries.end(),
  189. std::mem_fun_ref(&PeerEntry::isSnubbing)),
  190. peerEntries.end());
  191. std::for_each(peerEntries.begin(), peerEntries.end(),
  192. std::mem_fun_ref(&PeerEntry::enableChokingRequired));
  193. // planned optimistic unchoke
  194. if(round_ == 0) {
  195. plannedOptimisticUnchoke(peerEntries);
  196. }
  197. regularUnchoke(peerEntries);
  198. if(++round_ == 3) {
  199. round_ = 0;
  200. }
  201. }
  202. const Timer& BtLeecherStateChoke::getLastRound() const
  203. {
  204. return lastRound_;
  205. }
  206. } // namespace aria2