2008-05-11 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

Changed method signature:
	BNode::findClosestKNodes, BNode:: enumerateBucket,
	DHTBucket::getGoodNodes,
	DHTRoutingTable::getClosestKNodes, DHTRoutingTable::getBuckets
	* src/BNode.cc
	* src/BNode.h
	* src/DHTAbstractNodeLookupTask.cc
	* src/DHTAutoSaveCommand.cc
	* src/DHTBucket.cc
	* src/DHTBucket.h
	* src/DHTBucketRefreshTask.cc
	* src/DHTFindNodeMessage.cc
	* src/DHTGetPeersMessage.cc
	* src/DHTRoutingTable.cc
	* src/DHTRoutingTable.h
	* test/BNodeTest.cc
	* test/DHTBucketTest.cc
	* test/DHTRoutingTableTest.cc
pull/1/head
Tatsuhiro Tsujikawa 2008-05-11 08:07:57 +00:00
parent bf5a8c3f78
commit 6d78a7656d
15 changed files with 75 additions and 37 deletions

View File

@ -1,3 +1,24 @@
2008-05-11 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Changed method signature:
BNode::findClosestKNodes, BNode:: enumerateBucket,
DHTBucket::getGoodNodes,
DHTRoutingTable::getClosestKNodes, DHTRoutingTable::getBuckets
* src/BNode.cc
* src/BNode.h
* src/DHTAbstractNodeLookupTask.cc
* src/DHTAutoSaveCommand.cc
* src/DHTBucket.cc
* src/DHTBucket.h
* src/DHTBucketRefreshTask.cc
* src/DHTFindNodeMessage.cc
* src/DHTGetPeersMessage.cc
* src/DHTRoutingTable.cc
* src/DHTRoutingTable.h
* test/BNodeTest.cc
* test/DHTBucketTest.cc
* test/DHTRoutingTableTest.cc
2008-05-11 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> 2008-05-11 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
In piece selection functions, Pass std::deque<...> by reference rather In piece selection functions, Pass std::deque<...> by reference rather

View File

@ -134,27 +134,26 @@ SharedHandle<DHTBucket> BNode::findBucketFor(BNode* b, const unsigned char* key)
} }
std::deque<SharedHandle<DHTNode> > BNode::findClosestKNodes(BNode* b, const unsigned char* key) void BNode::findClosestKNodes(std::deque<SharedHandle<DHTNode> >& nodes,
BNode* b, const unsigned char* key)
{ {
BNode* bnode = findBNodeFor(b, key); BNode* bnode = findBNodeFor(b, key);
std::deque<SharedHandle<DHTNode> > nodes;
if(!bnode) { if(!bnode) {
return nodes; return;
} }
{ {
SharedHandle<DHTBucket> bucket = bnode->getBucket(); SharedHandle<DHTBucket> bucket = bnode->getBucket();
std::deque<SharedHandle<DHTNode> > goodNodes = bucket->getGoodNodes(); bucket->getGoodNodes(nodes);
nodes.insert(nodes.end(), goodNodes.begin(), goodNodes.end());
} }
if(nodes.size() >= DHTBucket::K) { if(nodes.size() >= DHTBucket::K) {
return nodes; return;
} }
std::deque<const BNode*> visited; std::deque<const BNode*> visited;
visited.push_back(bnode); visited.push_back(bnode);
BNode* up = bnode->getUp(); BNode* up = bnode->getUp();
if(!up) { if(!up) {
return nodes; return;
} }
bool leftFirst = false; bool leftFirst = false;
if(up->getLeft() == bnode) { if(up->getLeft() == bnode) {
@ -184,7 +183,8 @@ std::deque<SharedHandle<DHTNode> > BNode::findClosestKNodes(BNode* b, const unsi
{ {
SharedHandle<DHTBucket> bucket = bnode->getBucket(); SharedHandle<DHTBucket> bucket = bnode->getBucket();
if(!bucket.isNull()) { if(!bucket.isNull()) {
std::deque<SharedHandle<DHTNode> > goodNodes = bucket->getGoodNodes(); std::deque<SharedHandle<DHTNode> > goodNodes;
bucket->getGoodNodes(goodNodes);
size_t r = DHTBucket::K-nodes.size(); size_t r = DHTBucket::K-nodes.size();
if(goodNodes.size() <= r) { if(goodNodes.size() <= r) {
nodes.insert(nodes.end(), goodNodes.begin(), goodNodes.end()); nodes.insert(nodes.end(), goodNodes.begin(), goodNodes.end());
@ -194,12 +194,12 @@ std::deque<SharedHandle<DHTNode> > BNode::findClosestKNodes(BNode* b, const unsi
} }
} }
} }
return nodes; return;
} }
std::deque<SharedHandle<DHTBucket> > BNode::enumerateBucket(const BNode* b) void BNode::enumerateBucket(std::deque<SharedHandle<DHTBucket> >& buckets,
const BNode* b)
{ {
std::deque<SharedHandle<DHTBucket> > buckets;
std::deque<const BNode*> visited; std::deque<const BNode*> visited;
visited.push_back(b); visited.push_back(b);
while(1) { while(1) {
@ -219,7 +219,7 @@ std::deque<SharedHandle<DHTBucket> > BNode::enumerateBucket(const BNode* b)
b = b->getUp(); b = b->getUp();
} }
} }
return buckets; return;
} }
} // namespace aria2 } // namespace aria2

View File

@ -81,9 +81,11 @@ public:
static SharedHandle<DHTBucket> findBucketFor(BNode* b, const unsigned char* key); static SharedHandle<DHTBucket> findBucketFor(BNode* b, const unsigned char* key);
static std::deque<SharedHandle<DHTNode> > findClosestKNodes(BNode* b, const unsigned char* key); static void findClosestKNodes(std::deque<SharedHandle<DHTNode> >& nodes,
BNode* b, const unsigned char* key);
static std::deque<SharedHandle<DHTBucket> > enumerateBucket(const BNode* b); static void enumerateBucket(std::deque<SharedHandle<DHTBucket> >& buckets,
const BNode* b);
}; };
} // namespace aria2 } // namespace aria2

View File

@ -132,7 +132,9 @@ void DHTAbstractNodeLookupTask::updateBucket()
void DHTAbstractNodeLookupTask::startup() void DHTAbstractNodeLookupTask::startup()
{ {
_entries = toEntries(_routingTable->getClosestKNodes(_targetID)); std::deque<SharedHandle<DHTNode> > nodes;
_routingTable->getClosestKNodes(nodes, _targetID);
_entries = toEntries(nodes);
if(_entries.empty()) { if(_entries.empty()) {
_finished = true; _finished = true;
} else { } else {

View File

@ -92,11 +92,13 @@ void DHTAutoSaveCommand::save()
} }
} }
std::deque<SharedHandle<DHTNode> > nodes; std::deque<SharedHandle<DHTNode> > nodes;
std::deque<SharedHandle<DHTBucket> > buckets = _routingTable->getBuckets(); std::deque<SharedHandle<DHTBucket> > buckets;
_routingTable->getBuckets(buckets);
for(std::deque<SharedHandle<DHTBucket> >::const_iterator i = buckets.begin(); i != buckets.end(); ++i) { for(std::deque<SharedHandle<DHTBucket> >::const_iterator i = buckets.begin(); i != buckets.end(); ++i) {
const SharedHandle<DHTBucket>& bucket = *i; const SharedHandle<DHTBucket>& bucket = *i;
std::deque<SharedHandle<DHTNode> > goodNodes = bucket->getGoodNodes(); std::deque<SharedHandle<DHTNode> > goodNodes;
bucket->getGoodNodes(goodNodes);
nodes.insert(nodes.end(), goodNodes.begin(), goodNodes.end()); nodes.insert(nodes.end(), goodNodes.begin(), goodNodes.end());
} }

View File

@ -218,12 +218,11 @@ const std::deque<SharedHandle<DHTNode> >& DHTBucket::getNodes() const
return _nodes; return _nodes;
} }
std::deque<SharedHandle<DHTNode> > DHTBucket::getGoodNodes() const void DHTBucket::getGoodNodes(std::deque<SharedHandle<DHTNode> >& goodNodes) const
{ {
std::deque<SharedHandle<DHTNode> > goodNodes = _nodes; goodNodes = _nodes;
goodNodes.erase(std::remove_if(goodNodes.begin(), goodNodes.end(), goodNodes.erase(std::remove_if(goodNodes.begin(), goodNodes.end(),
mem_fun_sh(&DHTNode::isBad)), goodNodes.end()); mem_fun_sh(&DHTNode::isBad)), goodNodes.end());
return goodNodes;
} }
SharedHandle<DHTNode> DHTBucket::getNode(const unsigned char* nodeID, const std::string& ipaddr, uint16_t port) const SharedHandle<DHTNode> DHTBucket::getNode(const unsigned char* nodeID, const std::string& ipaddr, uint16_t port) const

View File

@ -114,7 +114,7 @@ public:
const std::deque<SharedHandle<DHTNode> >& getNodes() const; const std::deque<SharedHandle<DHTNode> >& getNodes() const;
std::deque<SharedHandle<DHTNode> > getGoodNodes() const; void getGoodNodes(std::deque<SharedHandle<DHTNode> >& nodes) const;
void dropNode(const SharedHandle<DHTNode>& node); void dropNode(const SharedHandle<DHTNode>& node);

View File

@ -51,7 +51,8 @@ DHTBucketRefreshTask::~DHTBucketRefreshTask() {}
void DHTBucketRefreshTask::startup() void DHTBucketRefreshTask::startup()
{ {
std::deque<SharedHandle<DHTBucket> > buckets = _routingTable->getBuckets(); std::deque<SharedHandle<DHTBucket> > buckets;
_routingTable->getBuckets(buckets);
for(std::deque<SharedHandle<DHTBucket> >::iterator i = buckets.begin(); i != buckets.end(); ++i) { for(std::deque<SharedHandle<DHTBucket> >::iterator i = buckets.begin(); i != buckets.end(); ++i) {
if(_forceRefresh || (*i)->needsRefresh()) { if(_forceRefresh || (*i)->needsRefresh()) {
(*i)->notifyUpdate(); (*i)->notifyUpdate();

View File

@ -58,7 +58,8 @@ DHTFindNodeMessage::~DHTFindNodeMessage() {}
void DHTFindNodeMessage::doReceivedAction() void DHTFindNodeMessage::doReceivedAction()
{ {
std::deque<SharedHandle<DHTNode> > nodes = _routingTable->getClosestKNodes(_targetNodeID); std::deque<SharedHandle<DHTNode> > nodes;
_routingTable->getClosestKNodes(nodes, _targetNodeID);
SharedHandle<DHTMessage> reply = SharedHandle<DHTMessage> reply =
_factory->createFindNodeReplyMessage(_remoteNode, nodes, _transactionID); _factory->createFindNodeReplyMessage(_remoteNode, nodes, _transactionID);
_dispatcher->addMessageToQueue(reply); _dispatcher->addMessageToQueue(reply);

View File

@ -69,7 +69,8 @@ void DHTGetPeersMessage::doReceivedAction()
Peers peers = _peerAnnounceStorage->getPeers(_infoHash); Peers peers = _peerAnnounceStorage->getPeers(_infoHash);
SharedHandle<DHTMessage> reply; SharedHandle<DHTMessage> reply;
if(peers.empty()) { if(peers.empty()) {
std::deque<SharedHandle<DHTNode> > nodes = _routingTable->getClosestKNodes(_infoHash); std::deque<SharedHandle<DHTNode> > nodes;
_routingTable->getClosestKNodes(nodes, _infoHash);
reply = reply =
_factory->createGetPeersReplyMessage(_remoteNode, nodes, token, _factory->createGetPeersReplyMessage(_remoteNode, nodes, token,
_transactionID); _transactionID);

View File

@ -108,9 +108,10 @@ bool DHTRoutingTable::addNode(const SharedHandle<DHTNode>& node, bool good)
return false; return false;
} }
std::deque<SharedHandle<DHTNode> > DHTRoutingTable::getClosestKNodes(const unsigned char* key) const void DHTRoutingTable::getClosestKNodes(std::deque<SharedHandle<DHTNode> >& nodes,
const unsigned char* key) const
{ {
return BNode::findClosestKNodes(_root, key); BNode::findClosestKNodes(nodes, _root, key);
} }
size_t DHTRoutingTable::countBucket() const size_t DHTRoutingTable::countBucket() const
@ -158,9 +159,9 @@ void DHTRoutingTable::moveBucketTail(const SharedHandle<DHTNode>& node)
getBucketFor(node)->moveToTail(node); getBucketFor(node)->moveToTail(node);
} }
std::deque<SharedHandle<DHTBucket> > DHTRoutingTable::getBuckets() const void DHTRoutingTable::getBuckets(std::deque<SharedHandle<DHTBucket> >& buckets) const
{ {
return BNode::enumerateBucket(_root); BNode::enumerateBucket(buckets, _root);
} }
void DHTRoutingTable::setTaskQueue(const SharedHandle<DHTTaskQueue>& taskQueue) void DHTRoutingTable::setTaskQueue(const SharedHandle<DHTTaskQueue>& taskQueue)

View File

@ -73,7 +73,8 @@ public:
bool addGoodNode(const SharedHandle<DHTNode>& node); bool addGoodNode(const SharedHandle<DHTNode>& node);
std::deque<SharedHandle<DHTNode> > getClosestKNodes(const unsigned char* key) const; void getClosestKNodes(std::deque<SharedHandle<DHTNode> >& nodes,
const unsigned char* key) const;
size_t countBucket() const; size_t countBucket() const;
@ -91,7 +92,7 @@ public:
SharedHandle<DHTNode> getNode(const unsigned char* id, const std::string& ipaddr, uint16_t port) const; SharedHandle<DHTNode> getNode(const unsigned char* id, const std::string& ipaddr, uint16_t port) const;
std::deque<SharedHandle<DHTBucket> > getBuckets() const; void getBuckets(std::deque<SharedHandle<DHTBucket> >& buckets) const;
void setTaskQueue(const SharedHandle<DHTTaskQueue>& taskQueue); void setTaskQueue(const SharedHandle<DHTTaskQueue>& taskQueue);

View File

@ -155,7 +155,8 @@ void BNodeTest::testFindClosestKNodes()
{ {
unsigned char targetID[DHT_ID_LENGTH]; unsigned char targetID[DHT_ID_LENGTH];
memset(targetID, 0x80, DHT_ID_LENGTH); memset(targetID, 0x80, DHT_ID_LENGTH);
std::deque<SharedHandle<DHTNode> > nodes = BNode::findClosestKNodes(bp4, targetID); std::deque<SharedHandle<DHTNode> > nodes;
BNode::findClosestKNodes(nodes, bp4, targetID);
CPPUNIT_ASSERT_EQUAL((size_t)8, nodes.size()); CPPUNIT_ASSERT_EQUAL((size_t)8, nodes.size());
CPPUNIT_ASSERT(bucket4->isInRange(nodes[0])); CPPUNIT_ASSERT(bucket4->isInRange(nodes[0]));
CPPUNIT_ASSERT(bucket4->isInRange(nodes[1])); CPPUNIT_ASSERT(bucket4->isInRange(nodes[1]));
@ -169,7 +170,8 @@ void BNodeTest::testFindClosestKNodes()
{ {
unsigned char targetID[DHT_ID_LENGTH]; unsigned char targetID[DHT_ID_LENGTH];
memset(targetID, 0xf0, DHT_ID_LENGTH); memset(targetID, 0xf0, DHT_ID_LENGTH);
std::deque<SharedHandle<DHTNode> > nodes = BNode::findClosestKNodes(bp4, targetID); std::deque<SharedHandle<DHTNode> > nodes;
BNode::findClosestKNodes(nodes, bp4, targetID);
CPPUNIT_ASSERT_EQUAL((size_t)8, nodes.size()); CPPUNIT_ASSERT_EQUAL((size_t)8, nodes.size());
CPPUNIT_ASSERT(bucket1->isInRange(nodes[0])); CPPUNIT_ASSERT(bucket1->isInRange(nodes[0]));
CPPUNIT_ASSERT(bucket1->isInRange(nodes[1])); CPPUNIT_ASSERT(bucket1->isInRange(nodes[1]));
@ -187,7 +189,8 @@ void BNodeTest::testFindClosestKNodes()
} }
unsigned char targetID[DHT_ID_LENGTH]; unsigned char targetID[DHT_ID_LENGTH];
memset(targetID, 0x80, DHT_ID_LENGTH); memset(targetID, 0x80, DHT_ID_LENGTH);
std::deque<SharedHandle<DHTNode> > nodes = BNode::findClosestKNodes(bp4, targetID); std::deque<SharedHandle<DHTNode> > nodes;
BNode::findClosestKNodes(nodes, bp4, targetID);
CPPUNIT_ASSERT_EQUAL((size_t)8, nodes.size()); CPPUNIT_ASSERT_EQUAL((size_t)8, nodes.size());
for(size_t i = 0; i < DHTBucket::K; ++i) { for(size_t i = 0; i < DHTBucket::K; ++i) {
CPPUNIT_ASSERT(bucket4->isInRange(nodes[i])); CPPUNIT_ASSERT(bucket4->isInRange(nodes[i]));
@ -213,7 +216,8 @@ void BNodeTest::testEnumerateBucket()
{ {
BNode b(bucket1); BNode b(bucket1);
std::deque<SharedHandle<DHTBucket> > buckets = BNode::enumerateBucket(&b); std::deque<SharedHandle<DHTBucket> > buckets;
BNode::enumerateBucket(buckets, &b);
CPPUNIT_ASSERT_EQUAL((size_t)1, buckets.size()); CPPUNIT_ASSERT_EQUAL((size_t)1, buckets.size());
CPPUNIT_ASSERT(bucket1 == buckets[0]); CPPUNIT_ASSERT(bucket1 == buckets[0]);
} }
@ -240,7 +244,8 @@ void BNodeTest::testEnumerateBucket()
bp4->setLeft(bp3); bp4->setLeft(bp3);
bp4->setRight(b2); bp4->setRight(b2);
std::deque<SharedHandle<DHTBucket> > buckets = BNode::enumerateBucket(bp4); std::deque<SharedHandle<DHTBucket> > buckets;
BNode::enumerateBucket(buckets, bp4);
CPPUNIT_ASSERT_EQUAL((size_t)5, buckets.size()); CPPUNIT_ASSERT_EQUAL((size_t)5, buckets.size());
CPPUNIT_ASSERT(bucket1 == buckets[0]); CPPUNIT_ASSERT(bucket1 == buckets[0]);
CPPUNIT_ASSERT(bucket3 == buckets[1]); CPPUNIT_ASSERT(bucket3 == buckets[1]);

View File

@ -333,7 +333,8 @@ void DHTBucketTest::testGetGoodNodes()
} }
nodes[3]->markBad(); nodes[3]->markBad();
nodes[5]->markBad(); nodes[5]->markBad();
std::deque<SharedHandle<DHTNode> > goodNodes = bucket.getGoodNodes(); std::deque<SharedHandle<DHTNode> > goodNodes;
bucket.getGoodNodes(goodNodes);
CPPUNIT_ASSERT_EQUAL((size_t)6, goodNodes.size()); CPPUNIT_ASSERT_EQUAL((size_t)6, goodNodes.size());
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, goodNodes[0]->getPort()); CPPUNIT_ASSERT_EQUAL((uint16_t)6881, goodNodes[0]->getPort());
CPPUNIT_ASSERT_EQUAL((uint16_t)6882, goodNodes[1]->getPort()); CPPUNIT_ASSERT_EQUAL((uint16_t)6882, goodNodes[1]->getPort());

View File

@ -82,7 +82,8 @@ void DHTRoutingTableTest::testGetClosestKNodes()
} }
{ {
createID(id, 0x80, 0x10); createID(id, 0x80, 0x10);
std::deque<SharedHandle<DHTNode> > nodes = table.getClosestKNodes(id); std::deque<SharedHandle<DHTNode> > nodes;
table.getClosestKNodes(nodes, id);
CPPUNIT_ASSERT_EQUAL((size_t)8, nodes.size()); CPPUNIT_ASSERT_EQUAL((size_t)8, nodes.size());
for(size_t i = 0; i < nodes.size(); ++i) { for(size_t i = 0; i < nodes.size(); ++i) {
CPPUNIT_ASSERT(memcmp(nodes2[0]->getID(), nodes[0]->getID(), DHT_ID_LENGTH) == 0); CPPUNIT_ASSERT(memcmp(nodes2[0]->getID(), nodes[0]->getID(), DHT_ID_LENGTH) == 0);