diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2016-03-26 17:12:52 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2016-03-26 17:20:27 +0900 |
| commit | d64089632e82a2548752c7a6baaa68365a795a74 (patch) | |
| tree | 57fc7b85a8c1290975d58fa4761b04a7695bf7e3 /src/ActivePeerConnectionCommand.cc | |
| parent | c81b1321122e8509b55c94c0cbca0d8d6d319f0c (diff) | |
| parent | a7cd943a06b1a5ff4515b57060b84037f8d01f9f (diff) | |
Merge branch 'master' into random-webseeding
Diffstat (limited to 'src/ActivePeerConnectionCommand.cc')
| -rw-r--r-- | src/ActivePeerConnectionCommand.cc | 97 |
1 files changed, 49 insertions, 48 deletions
diff --git a/src/ActivePeerConnectionCommand.cc b/src/ActivePeerConnectionCommand.cc index 1e2b44da..7e566b42 100644 --- a/src/ActivePeerConnectionCommand.cc +++ b/src/ActivePeerConnectionCommand.cc @@ -56,16 +56,14 @@ namespace aria2 { -ActivePeerConnectionCommand::ActivePeerConnectionCommand -(cuid_t cuid, - RequestGroup* requestGroup, - DownloadEngine* e, - std::chrono::seconds interval) - : Command(cuid), - requestGroup_(requestGroup), - interval_(std::move(interval)), - e_(e), - numNewConnection_(5) +ActivePeerConnectionCommand::ActivePeerConnectionCommand( + cuid_t cuid, RequestGroup* requestGroup, DownloadEngine* e, + std::chrono::seconds interval) + : Command(cuid), + requestGroup_(requestGroup), + interval_(std::move(interval)), + e_(e), + numNewConnection_(5) { requestGroup_->increaseNumCommand(); } @@ -75,50 +73,53 @@ ActivePeerConnectionCommand::~ActivePeerConnectionCommand() requestGroup_->decreaseNumCommand(); } -bool ActivePeerConnectionCommand::execute() { - if(btRuntime_->isHalt()) { +bool ActivePeerConnectionCommand::execute() +{ + if (btRuntime_->isHalt()) { return true; } - if(checkPoint_.difference(global::wallclock()) >= interval_) { + if (checkPoint_.difference(global::wallclock()) >= interval_) { checkPoint_ = global::wallclock(); NetStat& stat = requestGroup_->getDownloadContext()->getNetStat(); const int maxDownloadLimit = requestGroup_->getMaxDownloadSpeedLimit(); const int maxUploadLimit = requestGroup_->getMaxUploadSpeedLimit(); int thresholdSpeed; - if(!bittorrent::getTorrentAttrs - (requestGroup_->getDownloadContext())->metadata.empty()) { - thresholdSpeed = - requestGroup_->getOption()->getAsInt(PREF_BT_REQUEST_PEER_SPEED_LIMIT); - } else { + if (!bittorrent::getTorrentAttrs(requestGroup_->getDownloadContext()) + ->metadata.empty()) { + thresholdSpeed = requestGroup_->getOption()->getAsInt( + PREF_BT_REQUEST_PEER_SPEED_LIMIT); + } + else { thresholdSpeed = 0; } - if(maxDownloadLimit > 0) { + if (maxDownloadLimit > 0) { thresholdSpeed = std::min(maxDownloadLimit, thresholdSpeed); } - if(// for seeder state - (pieceStorage_->downloadFinished() && btRuntime_->lessThanMaxPeers() && - (maxUploadLimit == 0 || - stat.calculateUploadSpeed() < maxUploadLimit*0.8)) || - // for leecher state - (!pieceStorage_->downloadFinished() && - (stat.calculateDownloadSpeed() < thresholdSpeed || - btRuntime_->lessThanMinPeers()))) { + if ( // for seeder state + (pieceStorage_->downloadFinished() && btRuntime_->lessThanMaxPeers() && + (maxUploadLimit == 0 || + stat.calculateUploadSpeed() < maxUploadLimit * 0.8)) || + // for leecher state + (!pieceStorage_->downloadFinished() && + (stat.calculateDownloadSpeed() < thresholdSpeed || + btRuntime_->lessThanMinPeers()))) { int numConnection = 0; - if(pieceStorage_->downloadFinished()) { - if(btRuntime_->getMaxPeers() > btRuntime_->getConnections()) { + if (pieceStorage_->downloadFinished()) { + if (btRuntime_->getMaxPeers() > btRuntime_->getConnections()) { numConnection = - std::min(numNewConnection_, - btRuntime_->getMaxPeers()-btRuntime_->getConnections()); + std::min(numNewConnection_, btRuntime_->getMaxPeers() - + btRuntime_->getConnections()); } - } else { + } + else { numConnection = numNewConnection_; } makeNewConnections(numConnection); - if(btRuntime_->getConnections() == 0 && - !pieceStorage_->downloadFinished()) { + if (btRuntime_->getConnections() == 0 && + !pieceStorage_->downloadFinished()) { btAnnounce_->overrideMinInterval(BtAnnounce::DEFAULT_ANNOUNCE_INTERVAL); } } @@ -129,43 +130,43 @@ bool ActivePeerConnectionCommand::execute() { void ActivePeerConnectionCommand::makeNewConnections(int num) { - for(; num && peerStorage_->isPeerAvailable(); --num) { + for (; num && peerStorage_->isPeerAvailable(); --num) { cuid_t ncuid = e_->newCUID(); std::shared_ptr<Peer> peer = peerStorage_->checkoutPeer(ncuid); // sanity check - if(!peer) { + if (!peer) { break; } - auto command = make_unique<PeerInitiateConnectionCommand> - (ncuid, requestGroup_, peer, e_, btRuntime_); + auto command = make_unique<PeerInitiateConnectionCommand>( + ncuid, requestGroup_, peer, e_, btRuntime_); command->setPeerStorage(peerStorage_); command->setPieceStorage(pieceStorage_); e_->addCommand(std::move(command)); - A2_LOG_INFO(fmt(MSG_CONNECTING_TO_PEER, getCuid(), - peer->getIPAddress().c_str())); + A2_LOG_INFO( + fmt(MSG_CONNECTING_TO_PEER, getCuid(), peer->getIPAddress().c_str())); } } -void ActivePeerConnectionCommand::setBtRuntime -(const std::shared_ptr<BtRuntime>& btRuntime) +void ActivePeerConnectionCommand::setBtRuntime( + const std::shared_ptr<BtRuntime>& btRuntime) { btRuntime_ = btRuntime; } -void ActivePeerConnectionCommand::setPieceStorage -(const std::shared_ptr<PieceStorage>& pieceStorage) +void ActivePeerConnectionCommand::setPieceStorage( + const std::shared_ptr<PieceStorage>& pieceStorage) { pieceStorage_ = pieceStorage; } -void ActivePeerConnectionCommand::setPeerStorage -(const std::shared_ptr<PeerStorage>& peerStorage) +void ActivePeerConnectionCommand::setPeerStorage( + const std::shared_ptr<PeerStorage>& peerStorage) { peerStorage_ = peerStorage; } -void ActivePeerConnectionCommand::setBtAnnounce -(const std::shared_ptr<BtAnnounce>& btAnnounce) +void ActivePeerConnectionCommand::setBtAnnounce( + const std::shared_ptr<BtAnnounce>& btAnnounce) { btAnnounce_ = btAnnounce; } |
