From 70eeebfae9049ecd0f4c26a2d7bde8822895c14b Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 29 Jul 2011 23:49:25 +0900 Subject: [PATCH] Retry DHT get_peers task if the number of peers in PeerStorage is less than max peers. Added virtual size_t countPeer() const to PeerStorage. --- src/DHTGetPeersCommand.cc | 9 +++++++-- src/DefaultPeerStorage.h | 2 +- src/PeerStorage.h | 6 ++++++ test/MockPeerStorage.h | 5 +++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/DHTGetPeersCommand.cc b/src/DHTGetPeersCommand.cc index 0582cef0..25eab93f 100644 --- a/src/DHTGetPeersCommand.cc +++ b/src/DHTGetPeersCommand.cc @@ -110,9 +110,14 @@ bool DHTGetPeersCommand::execute() } else if(task_ && task_->finished()) { A2_LOG_DEBUG("task finished detected"); lastGetPeerTime_ = global::wallclock; - if(numRetry_ < MAX_RETRIES && btRuntime_->lessThanMinPeers()) { + if(numRetry_ < MAX_RETRIES && + (btRuntime_->getMaxPeers() == 0 || + btRuntime_->getMaxPeers() > peerStorage_->countPeer())) { ++numRetry_; - A2_LOG_DEBUG(fmt("Too few peers. Try again(%lu)", + A2_LOG_DEBUG(fmt("Too few peers. peers=%lu, max_peers=%lu." + " Try again(%lu)", + static_cast(peerStorage_->countPeer()), + static_cast(btRuntime_->getMaxPeers()), static_cast(numRetry_))); } else { numRetry_ = 0; diff --git a/src/DefaultPeerStorage.h b/src/DefaultPeerStorage.h index fe6a5eb1..2089831e 100644 --- a/src/DefaultPeerStorage.h +++ b/src/DefaultPeerStorage.h @@ -78,7 +78,7 @@ public: virtual bool addPeer(const SharedHandle& peer); - size_t countPeer() const; + virtual size_t countPeer() const; virtual SharedHandle getUnusedPeer(); diff --git a/src/PeerStorage.h b/src/PeerStorage.h index 442fd719..b3bcce88 100644 --- a/src/PeerStorage.h +++ b/src/PeerStorage.h @@ -67,6 +67,12 @@ public: */ virtual const std::deque >& getPeers() = 0; + + /** + * Returns the number of peers. + */ + virtual size_t countPeer() const = 0; + /** * Returns internal dropped peer list. */ diff --git a/test/MockPeerStorage.h b/test/MockPeerStorage.h index 1736c302..91e04d39 100644 --- a/test/MockPeerStorage.h +++ b/test/MockPeerStorage.h @@ -33,6 +33,11 @@ public: return peers; } + virtual size_t countPeer() const + { + return peers.size(); + } + virtual const std::deque >& getDroppedPeers() { return droppedPeers; }