BtSeederStateChoke.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "BtSeederStateChoke.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. BtSeederStateChoke::BtSeederStateChoke()
  45. : round_(0),
  46. lastRound_(0)
  47. {}
  48. BtSeederStateChoke::~BtSeederStateChoke() {}
  49. BtSeederStateChoke::PeerEntry::PeerEntry
  50. (const SharedHandle<Peer>& peer):
  51. peer_(peer),
  52. outstandingUpload_(peer->countOutstandingUpload()),
  53. lastAmUnchoking_(peer->getLastAmUnchoking()),
  54. recentUnchoking_(lastAmUnchoking_.difference(global::wallclock()) < TIME_FRAME),
  55. uploadSpeed_(peer->calculateUploadSpeed())
  56. {}
  57. BtSeederStateChoke::PeerEntry::PeerEntry(const PeerEntry& c)
  58. : peer_(c.peer_),
  59. outstandingUpload_(c.outstandingUpload_),
  60. lastAmUnchoking_(c.lastAmUnchoking_),
  61. recentUnchoking_(c.recentUnchoking_),
  62. uploadSpeed_(c.uploadSpeed_)
  63. {}
  64. BtSeederStateChoke::PeerEntry::~PeerEntry() {}
  65. void BtSeederStateChoke::PeerEntry::swap(PeerEntry& c)
  66. {
  67. using std::swap;
  68. swap(peer_, c.peer_);
  69. swap(outstandingUpload_, c.outstandingUpload_);
  70. swap(lastAmUnchoking_, c.lastAmUnchoking_);
  71. swap(recentUnchoking_, c.recentUnchoking_);
  72. swap(uploadSpeed_, c.uploadSpeed_);
  73. }
  74. BtSeederStateChoke::PeerEntry& BtSeederStateChoke::PeerEntry::operator=
  75. (const PeerEntry& c)
  76. {
  77. if(this != &c) {
  78. peer_ = c.peer_;
  79. outstandingUpload_ = c.outstandingUpload_;
  80. lastAmUnchoking_ = c.lastAmUnchoking_;
  81. recentUnchoking_ = c.recentUnchoking_;
  82. uploadSpeed_ = c.uploadSpeed_;
  83. }
  84. return *this;
  85. }
  86. bool
  87. BtSeederStateChoke::PeerEntry::operator<(const PeerEntry& rhs) const
  88. {
  89. if(this->outstandingUpload_ && !rhs.outstandingUpload_) {
  90. return true;
  91. } else if(!this->outstandingUpload_ && rhs.outstandingUpload_) {
  92. return false;
  93. }
  94. if(this->recentUnchoking_ &&
  95. (this->lastAmUnchoking_ > rhs.lastAmUnchoking_)) {
  96. return true;
  97. } else if(rhs.recentUnchoking_) {
  98. return false;
  99. } else {
  100. return this->uploadSpeed_ > rhs.uploadSpeed_;
  101. }
  102. }
  103. void BtSeederStateChoke::PeerEntry::disableOptUnchoking()
  104. {
  105. peer_->optUnchoking(false);
  106. }
  107. bool BtSeederStateChoke::NotInterestedPeer::operator()
  108. (const PeerEntry& peerEntry) const
  109. {
  110. return !peerEntry.getPeer()->peerInterested();
  111. }
  112. void BtSeederStateChoke::unchoke
  113. (std::vector<BtSeederStateChoke::PeerEntry>& peers)
  114. {
  115. int count = (round_ == 2) ? 4 : 3;
  116. std::sort(peers.begin(), peers.end());
  117. std::vector<PeerEntry>::iterator r = peers.begin();
  118. for(std::vector<PeerEntry>::iterator eoi = peers.end();
  119. r != eoi && count; ++r, --count) {
  120. (*r).getPeer()->chokingRequired(false);
  121. A2_LOG_INFO(fmt("RU: %s, ulspd=%d",
  122. (*r).getPeer()->getIPAddress().c_str(),
  123. (*r).getUploadSpeed()));
  124. }
  125. if(round_ < 2) {
  126. std::for_each(peers.begin(), peers.end(),
  127. std::mem_fun_ref(&PeerEntry::disableOptUnchoking));
  128. if(r != peers.end()) {
  129. std::random_shuffle(r, peers.end(),
  130. *(SimpleRandomizer::getInstance().get()));
  131. (*r).getPeer()->optUnchoking(true);
  132. A2_LOG_INFO(fmt("POU: %s", (*r).getPeer()->getIPAddress().c_str()));
  133. }
  134. }
  135. }
  136. namespace {
  137. class ChokingRequired {
  138. public:
  139. void operator()(const SharedHandle<Peer>& peer) const
  140. {
  141. peer->chokingRequired(true);
  142. }
  143. };
  144. } // namespace
  145. void
  146. BtSeederStateChoke::executeChoke
  147. (const std::vector<SharedHandle<Peer> >& peerSet)
  148. {
  149. A2_LOG_INFO(fmt("Seeder state, %d choke round started", round_));
  150. lastRound_ = global::wallclock();
  151. std::vector<PeerEntry> peerEntries;
  152. std::for_each(peerSet.begin(), peerSet.end(), ChokingRequired());
  153. std::transform(peerSet.begin(), peerSet.end(),
  154. std::back_inserter(peerEntries), GenPeerEntry());
  155. peerEntries.erase(std::remove_if(peerEntries.begin(), peerEntries.end(),
  156. NotInterestedPeer()),
  157. peerEntries.end());
  158. unchoke(peerEntries);
  159. if(++round_ == 3) {
  160. round_ = 0;
  161. }
  162. }
  163. void swap
  164. (BtSeederStateChoke::PeerEntry& a,
  165. BtSeederStateChoke::PeerEntry& b)
  166. {
  167. a.swap(b);
  168. }
  169. } // namespace aria2