BtSeederStateChoke.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. namespace aria2 {
  42. BtSeederStateChoke::BtSeederStateChoke():
  43. _round(0),
  44. _lastRound(0),
  45. _logger(LogFactory::getInstance()) {}
  46. BtSeederStateChoke::~BtSeederStateChoke() {}
  47. BtSeederStateChoke::PeerEntry::PeerEntry
  48. (const SharedHandle<Peer>& peer, const struct timeval& now):
  49. _peer(peer),
  50. _outstandingUpload(peer->countOutstandingUpload()),
  51. _lastAmUnchoking(peer->getLastAmUnchoking()),
  52. _recentUnchoking(!_lastAmUnchoking.elapsed(TIME_FRAME)),
  53. _uploadSpeed(peer->calculateUploadSpeed(now))
  54. {}
  55. bool
  56. BtSeederStateChoke::PeerEntry::operator<(const PeerEntry& rhs) const
  57. {
  58. if(this->_outstandingUpload && !rhs._outstandingUpload) {
  59. return true;
  60. } else if(!this->_outstandingUpload && rhs._outstandingUpload) {
  61. return false;
  62. }
  63. if(this->_recentUnchoking &&
  64. this->_lastAmUnchoking.isNewer(rhs._lastAmUnchoking)) {
  65. return true;
  66. } else if(rhs._recentUnchoking) {
  67. return false;
  68. } else {
  69. return this->_uploadSpeed > rhs._uploadSpeed;
  70. }
  71. }
  72. SharedHandle<Peer> BtSeederStateChoke::PeerEntry::getPeer() const
  73. {
  74. return _peer;
  75. }
  76. unsigned int BtSeederStateChoke::PeerEntry::getUploadSpeed() const
  77. {
  78. return _uploadSpeed;
  79. }
  80. void BtSeederStateChoke::PeerEntry::disableOptUnchoking()
  81. {
  82. _peer->optUnchoking(false);
  83. }
  84. void BtSeederStateChoke::unchoke
  85. (std::deque<BtSeederStateChoke::PeerEntry>& peers)
  86. {
  87. int count = (_round == 2) ? 4 : 3;
  88. std::sort(peers.begin(), peers.end());
  89. std::deque<PeerEntry>::iterator r = peers.begin();
  90. for(; r != peers.end() && count; ++r, --count) {
  91. (*r).getPeer()->chokingRequired(false);
  92. _logger->info("RU: %s, ulspd=%u", (*r).getPeer()->ipaddr.c_str(),
  93. (*r).getUploadSpeed());
  94. }
  95. if(_round == 2) {
  96. std::for_each(peers.begin(), peers.end(),
  97. std::mem_fun_ref(&PeerEntry::disableOptUnchoking));
  98. if(r != peers.end()) {
  99. std::random_shuffle(r, peers.end(),
  100. *(SimpleRandomizer::getInstance().get()));
  101. (*r).getPeer()->optUnchoking(true);
  102. _logger->info("POU: %s", (*r).getPeer()->ipaddr.c_str());
  103. }
  104. }
  105. }
  106. class ChokingRequired {
  107. public:
  108. void operator()(const SharedHandle<Peer>& peer) const
  109. {
  110. peer->chokingRequired(true);
  111. }
  112. };
  113. class GenPeerEntry {
  114. private:
  115. struct timeval _now;
  116. public:
  117. GenPeerEntry()
  118. {
  119. gettimeofday(&_now, 0);
  120. }
  121. BtSeederStateChoke::PeerEntry operator()(const SharedHandle<Peer>& peer) const
  122. {
  123. return BtSeederStateChoke::PeerEntry(peer, _now);
  124. }
  125. };
  126. class NotInterestedPeer {
  127. public:
  128. bool operator()(const BtSeederStateChoke::PeerEntry& peerEntry) const
  129. {
  130. return !peerEntry.getPeer()->peerInterested();
  131. }
  132. };
  133. void
  134. BtSeederStateChoke::executeChoke(const std::deque<SharedHandle<Peer> >& peerSet)
  135. {
  136. _logger->info("Seeder state, %d choke round started", _round);
  137. _lastRound.reset();
  138. std::deque<PeerEntry> peerEntries;
  139. std::for_each(peerSet.begin(), peerSet.end(), ChokingRequired());
  140. std::transform(peerSet.begin(), peerSet.end(),
  141. std::back_inserter(peerEntries), GenPeerEntry());
  142. peerEntries.erase(std::remove_if(peerEntries.begin(), peerEntries.end(),
  143. NotInterestedPeer()),
  144. peerEntries.end());
  145. unchoke(peerEntries);
  146. if(++_round == 3) {
  147. _round = 0;
  148. }
  149. }
  150. const Time& BtSeederStateChoke::getLastRound() const
  151. {
  152. return _lastRound;
  153. }
  154. } // namespace aria2