From 13021233680e3fb3650075e2a71c1f234acb1803 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 13 Feb 2008 15:17:08 +0000 Subject: 2008-02-13 Tatsuhiro Tsujikawa Added the ability to load nodes from torrent file. These nodes are added to the routing table when downloading that torrent. * src/BtContext.h * src/DefaultBtContext.{h, cc} * src/DHTSetup.cc * src/DHTEntryPointNameResolveCommand.{h, cc}: Now accepts list of hostname and port pair, and resolves all of them. * src/NameResolver.{h, cc}: Added reset(). * src/RequestGroup.cc * test/DefaultBtContextTest.cc * test/MockBtContext.h Removed assert() from DefaultBtContext and throw exception instead. * src/DefaultBtContext.cc --- src/DHTEntryPointNameResolveCommand.cc | 76 ++++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 18 deletions(-) (limited to 'src/DHTEntryPointNameResolveCommand.cc') diff --git a/src/DHTEntryPointNameResolveCommand.cc b/src/DHTEntryPointNameResolveCommand.cc index 5de627a7..3ffe73cc 100644 --- a/src/DHTEntryPointNameResolveCommand.cc +++ b/src/DHTEntryPointNameResolveCommand.cc @@ -44,19 +44,23 @@ #include "DHTNode.h" #include "DHTTaskQueue.h" #include "DHTTaskFactory.h" +#include "DHTRoutingTable.h" #include "DHTTask.h" #include "RequestGroupMan.h" #include "Logger.h" namespace aria2 { -DHTEntryPointNameResolveCommand::DHTEntryPointNameResolveCommand(int32_t cuid, DownloadEngine* e): +DHTEntryPointNameResolveCommand::DHTEntryPointNameResolveCommand(int32_t cuid, DownloadEngine* e, const std::deque >& entryPoints): Command(cuid), _e(e), _resolver(new NameResolver()), _taskQueue(0), _taskFactory(0), - _localNode(0) + _routingTable(0), + _localNode(0), + _entryPoints(entryPoints), + _bootstrapEnabled(false) {} DHTEntryPointNameResolveCommand::~DHTEntryPointNameResolveCommand() @@ -71,25 +75,42 @@ bool DHTEntryPointNameResolveCommand::execute() if(_e->_requestGroupMan->downloadFinished() || _e->isHaltRequested()) { return true; } - try { - std::string hostname = _e->option->get(PREF_DHT_ENTRY_POINT_HOST); - if(!Util::isNumbersAndDotsNotation(hostname)) { - if(resolveHostname(hostname, _resolver)) { - hostname = _resolver->getAddrString(); - } else { - _e->commands.push_back(this); - return false; + while(_entryPoints.size()) { + std::string hostname = _entryPoints.front().first; + try { + if(Util::isNumbersAndDotsNotation(hostname)) { + std::pair p(hostname, + _entryPoints.front().second); + _resolvedEntryPoints.push_back(p); + _entryPoints.erase(_entryPoints.begin()); + addPingTask(p); + } else { + if(resolveHostname(hostname, _resolver)) { + hostname = _resolver->getAddrString(); + _resolver->reset(); + std::pair p(hostname, + _entryPoints.front().second); + _resolvedEntryPoints.push_back(p); + _entryPoints.erase(_entryPoints.begin()); + addPingTask(p); + } else { + _e->commands.push_back(this); + return false; + } + } + } catch(RecoverableException* e) { + logger->error(EX_EXCEPTION_CAUGHT, e); + delete e; + _entryPoints.erase(_entryPoints.begin()); + _resolver->reset(); } } - - SharedHandle entryNode = new DHTNode(); - entryNode->setIPAddress(hostname); - entryNode->setPort(_e->option->getAsInt(PREF_DHT_ENTRY_POINT_PORT)); - - _taskQueue->addPeriodicTask1(_taskFactory->createPingTask(entryNode, 10)); - _taskQueue->addPeriodicTask1(_taskFactory->createNodeLookupTask(_localNode->getID())); - _taskQueue->addPeriodicTask1(_taskFactory->createBucketRefreshTask()); + + if(_bootstrapEnabled && _resolvedEntryPoints.size()) { + _taskQueue->addPeriodicTask1(_taskFactory->createNodeLookupTask(_localNode->getID())); + _taskQueue->addPeriodicTask1(_taskFactory->createBucketRefreshTask()); + } } catch(RecoverableException* e) { logger->error(EX_EXCEPTION_CAUGHT, e); delete e; @@ -97,6 +118,15 @@ bool DHTEntryPointNameResolveCommand::execute() return true; } +void DHTEntryPointNameResolveCommand::addPingTask(const std::pair& addr) +{ + SharedHandle entryNode = new DHTNode(); + entryNode->setIPAddress(addr.first); + entryNode->setPort(addr.second); + + _taskQueue->addPeriodicTask1(_taskFactory->createPingTask(entryNode, 10)); +} + bool DHTEntryPointNameResolveCommand::resolveHostname(const std::string& hostname, const NameResolverHandle& resolver) { @@ -148,6 +178,11 @@ void DHTEntryPointNameResolveCommand::disableNameResolverCheck(const SharedHandl } #endif // ENABLE_ASYNC_DNS +void DHTEntryPointNameResolveCommand::setBootstrapEnabled(bool f) +{ + _bootstrapEnabled = f; +} + void DHTEntryPointNameResolveCommand::setTaskQueue(const SharedHandle& taskQueue) { _taskQueue = taskQueue; @@ -158,6 +193,11 @@ void DHTEntryPointNameResolveCommand::setTaskFactory(const SharedHandle& routingTable) +{ + _routingTable = routingTable; +} + void DHTEntryPointNameResolveCommand::setLocalNode(const SharedHandle& localNode) { _localNode = localNode; -- cgit