diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2013-01-26 17:50:06 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2013-01-26 17:50:06 +0900 |
| commit | ba69f5c0c318132bb95c561f89488c754341ee96 (patch) | |
| tree | 26f016e6e2f2fd104c01fb6e2ac48374f5f2586c /src/ActivePeerConnectionCommand.cc | |
| parent | 8524ac38067ba2ca2e692de39166edd6612e1d5b (diff) | |
Refactor peer list management in DefaultPeerStorage
Peer list is now divided into 2: unusedPeers_ and usedPeers_.
Duplicate check is done using std::set by comparing pair of IP address
and port. For this, only IP address and port given to the Peer
constructor are used. In other words, TCP port received from extended
message is not used for this purpose.
Diffstat (limited to 'src/ActivePeerConnectionCommand.cc')
| -rw-r--r-- | src/ActivePeerConnectionCommand.cc | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/src/ActivePeerConnectionCommand.cc b/src/ActivePeerConnectionCommand.cc index 47183e4a..4c0dbf65 100644 --- a/src/ActivePeerConnectionCommand.cc +++ b/src/ActivePeerConnectionCommand.cc @@ -115,13 +115,8 @@ bool ActivePeerConnectionCommand::execute() { numConnection = numNewConnection_; } - for(; numConnection > 0; --numConnection) { - SharedHandle<Peer> peer = peerStorage_->getUnusedPeer(); - if(!peer) { - break; - } - connectToPeer(peer); - } + makeNewConnections(numConnection); + if(btRuntime_->getConnections() == 0 && !pieceStorage_->downloadFinished()) { btAnnounce_->overrideMinInterval(BtAnnounce::DEFAULT_ANNOUNCE_INTERVAL); @@ -132,18 +127,24 @@ bool ActivePeerConnectionCommand::execute() { return false; } -void ActivePeerConnectionCommand::connectToPeer(const SharedHandle<Peer>& peer) +void ActivePeerConnectionCommand::makeNewConnections(int num) { - peer->usedBy(e_->newCUID()); - PeerInitiateConnectionCommand* command = - new PeerInitiateConnectionCommand(peer->usedBy(), requestGroup_, peer, e_, - btRuntime_); - command->setPeerStorage(peerStorage_); - command->setPieceStorage(pieceStorage_); - e_->addCommand(command); - A2_LOG_INFO(fmt(MSG_CONNECTING_TO_PEER, - getCuid(), - peer->getIPAddress().c_str())); + for(; num && peerStorage_->isPeerAvailable(); --num) { + cuid_t ncuid = e_->newCUID(); + SharedHandle<Peer> peer = peerStorage_->checkoutPeer(ncuid); + // sanity check + if(!peer) { + break; + } + PeerInitiateConnectionCommand* command; + command = new PeerInitiateConnectionCommand(ncuid, requestGroup_, peer, e_, + btRuntime_); + command->setPeerStorage(peerStorage_); + command->setPieceStorage(pieceStorage_); + e_->addCommand(command); + A2_LOG_INFO(fmt(MSG_CONNECTING_TO_PEER, getCuid(), + peer->getIPAddress().c_str())); + } } void ActivePeerConnectionCommand::setBtRuntime |
