BtSeederStateChoke.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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::unchoke
  81. (std::deque<BtSeederStateChoke::PeerEntry>& peers)
  82. {
  83. int count = (_round == 2) ? 4 : 3;
  84. std::sort(peers.begin(), peers.end());
  85. std::deque<PeerEntry>::iterator r = peers.begin();
  86. for(; r != peers.end() && count; ++r, --count) {
  87. (*r).getPeer()->chokingRequired(false);
  88. _logger->info("RU: %s, ulspd=%u", (*r).getPeer()->ipaddr.c_str(),
  89. (*r).getUploadSpeed());
  90. }
  91. if(_round == 2 && r != peers.end()) {
  92. std::random_shuffle(r, peers.end(),
  93. *(SimpleRandomizer::getInstance().get()));
  94. (*r).getPeer()->optUnchoking(true);
  95. _logger->info("POU: %s", (*r).getPeer()->ipaddr.c_str());
  96. }
  97. }
  98. class ChokingRequired {
  99. public:
  100. void operator()(const SharedHandle<Peer>& peer) const
  101. {
  102. peer->chokingRequired(true);
  103. }
  104. };
  105. class GenPeerEntry {
  106. private:
  107. struct timeval _now;
  108. public:
  109. GenPeerEntry()
  110. {
  111. gettimeofday(&_now, 0);
  112. }
  113. BtSeederStateChoke::PeerEntry operator()(const SharedHandle<Peer>& peer) const
  114. {
  115. return BtSeederStateChoke::PeerEntry(peer, _now);
  116. }
  117. };
  118. class NotInterestedPeer {
  119. public:
  120. bool operator()(const BtSeederStateChoke::PeerEntry& peerEntry) const
  121. {
  122. return !peerEntry.getPeer()->peerInterested();
  123. }
  124. };
  125. void
  126. BtSeederStateChoke::executeChoke(const std::deque<SharedHandle<Peer> >& peerSet)
  127. {
  128. _logger->info("Seeder state, %d choke round started", _round);
  129. _lastRound.reset();
  130. std::deque<PeerEntry> peerEntries;
  131. std::for_each(peerSet.begin(), peerSet.end(), ChokingRequired());
  132. std::transform(peerSet.begin(), peerSet.end(),
  133. std::back_inserter(peerEntries), GenPeerEntry());
  134. peerEntries.erase(std::remove_if(peerEntries.begin(), peerEntries.end(),
  135. NotInterestedPeer()),
  136. peerEntries.end());
  137. unchoke(peerEntries);
  138. if(++_round == 3) {
  139. _round = 0;
  140. }
  141. }
  142. const Time& BtSeederStateChoke::getLastRound() const
  143. {
  144. return _lastRound;
  145. }
  146. } // namespace aria2