From 8902c2a8399f1f2234e9598697de2f91d6c4295e Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 31 Jan 2011 23:00:05 +0900 Subject: [PATCH] Rewritten DefaultPeerStorage::addPeer(const std::vector >&) --- src/DefaultPeerStorage.cc | 22 +++++++++++++++++++--- test/DefaultPeerStorageTest.cc | 21 +++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/DefaultPeerStorage.cc b/src/DefaultPeerStorage.cc index ac390acb..a67e75b6 100644 --- a/src/DefaultPeerStorage.cc +++ b/src/DefaultPeerStorage.cc @@ -113,19 +113,35 @@ bool DefaultPeerStorage::addPeer(const SharedHandle& peer) { deleteUnusedPeer(peers_.size()-maxPeerListSize+1); } peers_.push_front(peer); + A2_LOG_DEBUG(fmt("Now peer list contains %lu peers", + static_cast(peers_.size()))); return true; } void DefaultPeerStorage::addPeer(const std::vector >& peers) { + size_t maxPeerListSize = calculateMaxPeerListSize(btRuntime_); + size_t added = 0; for(std::vector >::const_iterator itr = peers.begin(), - eoi = peers.end(); itr != eoi; ++itr) { + eoi = peers.end(); itr != eoi && added < maxPeerListSize; ++itr) { const SharedHandle& peer = *itr; - if(addPeer(peer)) { + if(isPeerAlreadyAdded(peer)) { + A2_LOG_DEBUG(fmt("Adding %s:%u is rejected because it has been already" + " added.", + peer->getIPAddress().c_str(), peer->getPort())); + continue; + } else { A2_LOG_DEBUG(fmt(MSG_ADDING_PEER, peer->getIPAddress().c_str(), peer->getPort())); } - } + peers_.push_front(peer); + ++added; + } + if(peers_.size() >= maxPeerListSize) { + deleteUnusedPeer(peers_.size()-maxPeerListSize); + } + A2_LOG_DEBUG(fmt("Now peer list contains %lu peers", + static_cast(peers_.size()))); } void DefaultPeerStorage::addDroppedPeer(const SharedHandle& peer) diff --git a/test/DefaultPeerStorageTest.cc b/test/DefaultPeerStorageTest.cc index d0801038..f12bf48d 100644 --- a/test/DefaultPeerStorageTest.cc +++ b/test/DefaultPeerStorageTest.cc @@ -9,6 +9,7 @@ #include "Peer.h" #include "Option.h" #include "BtRuntime.h" +#include "array_fun.h" namespace aria2 { @@ -131,6 +132,26 @@ void DefaultPeerStorageTest::testAddPeer() { // this returns false because the peer which has same ip and port // has already added CPPUNIT_ASSERT_EQUAL(false, ps.addPeer(peer5)); + + SharedHandle pa[] = { + SharedHandle(new Peer("192.168.0.4", 6889)), + SharedHandle(new Peer("192.168.0.5", 6889)), + SharedHandle(new Peer("192.168.0.6", 6889)), + SharedHandle(new Peer("192.168.0.7", 6889)), + SharedHandle(new Peer("192.168.0.8", 6889)) + }; + std::vector > peers(vbegin(pa), vend(pa)); + ps.addPeer(peers); + // peers[0] is not added because it has been already added. + // peers[1], peers[2] and peers[3] are going to be added. peers[4] + // is not added because DefaultPeerStorage::addPeer() limits the + // number of peers to add. Finally, unused peers are removed from + // back and size 3 vector is made. + CPPUNIT_ASSERT_EQUAL((size_t)3, ps.countPeer()); + CPPUNIT_ASSERT(std::find_if(ps.getPeers().begin(), ps.getPeers().end(), + derefEqual(peers[2])) != ps.getPeers().end()); + CPPUNIT_ASSERT(std::find_if(ps.getPeers().begin(), ps.getPeers().end(), + derefEqual(peers[3])) != ps.getPeers().end()); } void DefaultPeerStorageTest::testGetUnusedPeer() {