BtSeederStateChoke.cc 4.8 KB

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