BtSeederStateChoke.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. namespace aria2 {
  43. BtSeederStateChoke::BtSeederStateChoke():
  44. round_(0),
  45. lastRound_(0),
  46. logger_(LogFactory::getInstance()) {}
  47. BtSeederStateChoke::~BtSeederStateChoke() {}
  48. BtSeederStateChoke::PeerEntry::PeerEntry
  49. (const SharedHandle<Peer>& peer):
  50. peer_(peer),
  51. outstandingUpload_(peer->countOutstandingUpload()),
  52. lastAmUnchoking_(peer->getLastAmUnchoking()),
  53. recentUnchoking_(lastAmUnchoking_.difference(global::wallclock) < TIME_FRAME),
  54. uploadSpeed_(peer->calculateUploadSpeed())
  55. {}
  56. bool
  57. BtSeederStateChoke::PeerEntry::operator<(const PeerEntry& rhs) const
  58. {
  59. if(this->outstandingUpload_ && !rhs.outstandingUpload_) {
  60. return true;
  61. } else if(!this->outstandingUpload_ && rhs.outstandingUpload_) {
  62. return false;
  63. }
  64. if(this->recentUnchoking_ &&
  65. (this->lastAmUnchoking_ > rhs.lastAmUnchoking_)) {
  66. return true;
  67. } else if(rhs.recentUnchoking_) {
  68. return false;
  69. } else {
  70. return this->uploadSpeed_ > rhs.uploadSpeed_;
  71. }
  72. }
  73. void BtSeederStateChoke::PeerEntry::disableOptUnchoking()
  74. {
  75. peer_->optUnchoking(false);
  76. }
  77. bool BtSeederStateChoke::NotInterestedPeer::operator()
  78. (const PeerEntry& peerEntry) const
  79. {
  80. return !peerEntry.getPeer()->peerInterested();
  81. }
  82. void BtSeederStateChoke::unchoke
  83. (std::vector<BtSeederStateChoke::PeerEntry>& peers)
  84. {
  85. int count = (round_ == 2) ? 4 : 3;
  86. std::sort(peers.begin(), peers.end());
  87. std::vector<PeerEntry>::iterator r = peers.begin();
  88. for(std::vector<PeerEntry>::iterator eoi = peers.end();
  89. r != eoi && count; ++r, --count) {
  90. (*r).getPeer()->chokingRequired(false);
  91. logger_->info("RU: %s, ulspd=%u", (*r).getPeer()->getIPAddress().c_str(),
  92. (*r).getUploadSpeed());
  93. }
  94. if(round_ < 2) {
  95. std::for_each(peers.begin(), peers.end(),
  96. std::mem_fun_ref(&PeerEntry::disableOptUnchoking));
  97. if(r != peers.end()) {
  98. std::random_shuffle(r, peers.end(),
  99. *(SimpleRandomizer::getInstance().get()));
  100. (*r).getPeer()->optUnchoking(true);
  101. logger_->info("POU: %s", (*r).getPeer()->getIPAddress().c_str());
  102. }
  103. }
  104. }
  105. namespace {
  106. class ChokingRequired {
  107. public:
  108. void operator()(const SharedHandle<Peer>& peer) const
  109. {
  110. peer->chokingRequired(true);
  111. }
  112. };
  113. }
  114. void
  115. BtSeederStateChoke::executeChoke
  116. (const std::vector<SharedHandle<Peer> >& peerSet)
  117. {
  118. logger_->info("Seeder state, %d choke round started", round_);
  119. lastRound_ = global::wallclock;
  120. std::vector<PeerEntry> peerEntries;
  121. std::for_each(peerSet.begin(), peerSet.end(), ChokingRequired());
  122. std::transform(peerSet.begin(), peerSet.end(),
  123. std::back_inserter(peerEntries), GenPeerEntry());
  124. peerEntries.erase(std::remove_if(peerEntries.begin(), peerEntries.end(),
  125. NotInterestedPeer()),
  126. peerEntries.end());
  127. unchoke(peerEntries);
  128. if(++round_ == 3) {
  129. round_ = 0;
  130. }
  131. }
  132. } // namespace aria2