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

DNSCache is now part of DownloadEngine.
	* src/DNSCache.h
	* src/DownloadEngine.cc
	* src/DownloadEngine.h
	* src/InitiateConnectionCommand.cc
	* src/MultiUrlRequestInfo.cc
	* test/Makefile.am
	* test/Makefile.in
	* test/SimpleDNSCacheTest.cc
pull/1/head
Tatsuhiro Tsujikawa 2008-11-03 08:18:58 +00:00
parent 220a483004
commit 928e228c89
9 changed files with 101 additions and 26 deletions

View File

@ -1,3 +1,15 @@
2008-11-03 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
DNSCache is now part of DownloadEngine.
* src/DNSCache.h
* src/DownloadEngine.cc
* src/DownloadEngine.h
* src/InitiateConnectionCommand.cc
* src/MultiUrlRequestInfo.cc
* test/Makefile.am
* test/Makefile.in
* test/SimpleDNSCacheTest.cc
2008-11-03 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> 2008-11-03 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Removed SingletonHolder of CUIDCounter. CUIDCounter is now part of Removed SingletonHolder of CUIDCounter. CUIDCounter is now part of

View File

@ -36,26 +36,23 @@
#define _D_DNS_CACHE_H_ #define _D_DNS_CACHE_H_
#include "common.h" #include "common.h"
#include "SharedHandle.h"
#include "SingletonHolder.h"
#include "A2STR.h"
#include <string> #include <string>
#include <map> #include <map>
#include "A2STR.h"
namespace aria2 { namespace aria2 {
class DNSCache { class DNSCache {
public: public:
virtual ~DNSCache() {} virtual ~DNSCache() {}
virtual std::string find(const std::string& hostname) = 0; virtual const std::string& find(const std::string& hostname) const = 0;
virtual void put(const std::string& hostname, const std::string& ipaddr) = 0; virtual void put(const std::string& hostname, const std::string& ipaddr) = 0;
}; };
typedef SharedHandle<DNSCache> DNSCacheHandle;
typedef SingletonHolder<DNSCacheHandle> DNSCacheSingletonHolder;
class SimpleDNSCache : public DNSCache { class SimpleDNSCache : public DNSCache {
private: private:
std::map<std::string, std::string> _table; std::map<std::string, std::string> _table;
@ -64,9 +61,15 @@ public:
virtual ~SimpleDNSCache() {} virtual ~SimpleDNSCache() {}
virtual std::string find(const std::string& hostname) virtual const std::string& find(const std::string& hostname) const
{ {
return _table[hostname]; std::map<std::string, std::string>::const_iterator i =
_table.find(hostname);
if(i == _table.end()) {
return A2STR::NIL;
} else {
return (*i).second;
}
} }
virtual void put(const std::string& hostname, const std::string& ipaddr) virtual void put(const std::string& hostname, const std::string& ipaddr)
@ -80,7 +83,10 @@ class NullDNSCache : public DNSCache {
public: public:
virtual ~NullDNSCache() {} virtual ~NullDNSCache() {}
virtual std::string find(const std::string& hostname) { return A2STR::NIL; } virtual const std::string& find(const std::string& hostname)
{
return A2STR::NIL;
}
virtual void put(const std::string& hostname, const std::string& ipaddr) {} virtual void put(const std::string& hostname, const std::string& ipaddr) {}
}; };

View File

@ -62,6 +62,7 @@
#include "ServerStatMan.h" #include "ServerStatMan.h"
#include "CookieStorage.h" #include "CookieStorage.h"
#include "A2STR.h" #include "A2STR.h"
#include "DNSCache.h"
#include "BtRegistry.h" #include "BtRegistry.h"
#include "BtContext.h" #include "BtContext.h"
@ -411,7 +412,8 @@ DownloadEngine::DownloadEngine():logger(LogFactory::getInstance()),
_haltRequested(false), _haltRequested(false),
_noWait(false), _noWait(false),
_cookieStorage(new CookieStorage()), _cookieStorage(new CookieStorage()),
_btRegistry(new BtRegistry()) _btRegistry(new BtRegistry()),
_dnsCache(new SimpleDNSCache())
{ {
#ifdef HAVE_EPOLL #ifdef HAVE_EPOLL
@ -1064,4 +1066,16 @@ CUID DownloadEngine::newCUID()
return _cuidCounter.newID(); return _cuidCounter.newID();
} }
const std::string& DownloadEngine::findCachedIPAddress
(const std::string& hostname) const
{
return _dnsCache->find(hostname);
}
void DownloadEngine::cacheIPAddress
(const std::string& hostname, const std::string& ipaddr)
{
_dnsCache->put(hostname, ipaddr);
}
} // namespace aria2 } // namespace aria2

View File

@ -66,6 +66,7 @@ class CheckIntegrityMan;
class SocketCore; class SocketCore;
class CookieStorage; class CookieStorage;
class BtRegistry; class BtRegistry;
class DNSCache;
class CommandEvent class CommandEvent
{ {
@ -301,6 +302,8 @@ private:
CUIDCounter _cuidCounter; CUIDCounter _cuidCounter;
SharedHandle<DNSCache> _dnsCache;
void shortSleep() const; void shortSleep() const;
/** /**
@ -418,6 +421,10 @@ public:
SharedHandle<BtRegistry> getBtRegistry() const; SharedHandle<BtRegistry> getBtRegistry() const;
CUID newCUID(); CUID newCUID();
const std::string& findCachedIPAddress(const std::string& hostname) const;
void cacheIPAddress(const std::string& hostname, const std::string& ipaddr);
}; };
typedef SharedHandle<DownloadEngine> DownloadEngineHandle; typedef SharedHandle<DownloadEngine> DownloadEngineHandle;

View File

@ -68,7 +68,7 @@ bool InitiateConnectionCommand::executeInternal() {
hostname = req->getHost(); hostname = req->getHost();
} }
std::deque<std::string> addrs; std::deque<std::string> addrs;
std::string ipaddr = DNSCacheSingletonHolder::instance()->find(hostname); std::string ipaddr = e->findCachedIPAddress(hostname);
if(ipaddr.empty()) { if(ipaddr.empty()) {
#ifdef ENABLE_ASYNC_DNS #ifdef ENABLE_ASYNC_DNS
if(e->option->getAsBool(PREF_ASYNC_DNS)) { if(e->option->getAsBool(PREF_ASYNC_DNS)) {
@ -91,7 +91,7 @@ bool InitiateConnectionCommand::executeInternal() {
logger->info(MSG_NAME_RESOLUTION_COMPLETE, cuid, logger->info(MSG_NAME_RESOLUTION_COMPLETE, cuid,
hostname.c_str(), hostname.c_str(),
addrs.front().c_str()); addrs.front().c_str());
DNSCacheSingletonHolder::instance()->put(hostname, addrs.front()); e->cacheIPAddress(hostname, addrs.front());
} else { } else {
logger->info(MSG_DNS_CACHE_HIT, cuid, hostname.c_str(), ipaddr.c_str()); logger->info(MSG_DNS_CACHE_HIT, cuid, hostname.c_str(), ipaddr.c_str());
addrs.push_back(ipaddr); addrs.push_back(ipaddr);

View File

@ -33,6 +33,11 @@
*/ */
/* copyright --> */ /* copyright --> */
#include "MultiUrlRequestInfo.h" #include "MultiUrlRequestInfo.h"
#include <signal.h>
#include <ostream>
#include "RequestGroupMan.h" #include "RequestGroupMan.h"
#include "DownloadEngine.h" #include "DownloadEngine.h"
#include "LogFactory.h" #include "LogFactory.h"
@ -42,14 +47,11 @@
#include "DownloadEngineFactory.h" #include "DownloadEngineFactory.h"
#include "RecoverableException.h" #include "RecoverableException.h"
#include "message.h" #include "message.h"
#include "DNSCache.h"
#include "Util.h" #include "Util.h"
#include "Option.h" #include "Option.h"
#include "StatCalc.h" #include "StatCalc.h"
#include "CookieStorage.h" #include "CookieStorage.h"
#include "File.h" #include "File.h"
#include <signal.h>
#include <ostream>
namespace aria2 { namespace aria2 {
@ -93,10 +95,6 @@ void MultiUrlRequestInfo::printMessageForContinue()
int MultiUrlRequestInfo::execute() int MultiUrlRequestInfo::execute()
{ {
{
DNSCacheHandle dnsCache(new SimpleDNSCache());
DNSCacheSingletonHolder::instance(dnsCache);
}
int returnValue = 0; int returnValue = 0;
try { try {
DownloadEngineHandle e = DownloadEngineHandle e =

View File

@ -61,7 +61,8 @@ aria2c_SOURCES = AllTest.cc\
TimeTest.cc\ TimeTest.cc\
CopyDiskAdaptorTest.cc\ CopyDiskAdaptorTest.cc\
FtpConnectionTest.cc\ FtpConnectionTest.cc\
OptionParserTest.cc OptionParserTest.cc\
SimpleDNSCacheTest.cc
if HAVE_LIBZ if HAVE_LIBZ
aria2c_SOURCES += GZipDecoderTest.cc aria2c_SOURCES += GZipDecoderTest.cc

View File

@ -196,7 +196,7 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \
ServerStatTest.cc NsCookieParserTest.cc \ ServerStatTest.cc NsCookieParserTest.cc \
DirectDiskAdaptorTest.cc CookieTest.cc CookieStorageTest.cc \ DirectDiskAdaptorTest.cc CookieTest.cc CookieStorageTest.cc \
TimeTest.cc CopyDiskAdaptorTest.cc FtpConnectionTest.cc \ TimeTest.cc CopyDiskAdaptorTest.cc FtpConnectionTest.cc \
OptionParserTest.cc GZipDecoderTest.cc \ OptionParserTest.cc SimpleDNSCacheTest.cc GZipDecoderTest.cc \
Sqlite3MozCookieParserTest.cc MessageDigestHelperTest.cc \ Sqlite3MozCookieParserTest.cc MessageDigestHelperTest.cc \
IteratableChunkChecksumValidatorTest.cc \ IteratableChunkChecksumValidatorTest.cc \
IteratableChecksumValidatorTest.cc BtAllowedFastMessageTest.cc \ IteratableChecksumValidatorTest.cc BtAllowedFastMessageTest.cc \
@ -369,8 +369,8 @@ am_aria2c_OBJECTS = AllTest.$(OBJEXT) TestUtil.$(OBJEXT) \
CookieTest.$(OBJEXT) CookieStorageTest.$(OBJEXT) \ CookieTest.$(OBJEXT) CookieStorageTest.$(OBJEXT) \
TimeTest.$(OBJEXT) CopyDiskAdaptorTest.$(OBJEXT) \ TimeTest.$(OBJEXT) CopyDiskAdaptorTest.$(OBJEXT) \
FtpConnectionTest.$(OBJEXT) OptionParserTest.$(OBJEXT) \ FtpConnectionTest.$(OBJEXT) OptionParserTest.$(OBJEXT) \
$(am__objects_1) $(am__objects_2) $(am__objects_3) \ SimpleDNSCacheTest.$(OBJEXT) $(am__objects_1) $(am__objects_2) \
$(am__objects_4) $(am__objects_5) $(am__objects_3) $(am__objects_4) $(am__objects_5)
aria2c_OBJECTS = $(am_aria2c_OBJECTS) aria2c_OBJECTS = $(am_aria2c_OBJECTS)
am__DEPENDENCIES_1 = am__DEPENDENCIES_1 =
aria2c_DEPENDENCIES = ../src/libaria2c.a $(am__DEPENDENCIES_1) aria2c_DEPENDENCIES = ../src/libaria2c.a $(am__DEPENDENCIES_1)
@ -592,8 +592,9 @@ aria2c_SOURCES = AllTest.cc TestUtil.cc TestUtil.h SocketCoreTest.cc \
ServerStatTest.cc NsCookieParserTest.cc \ ServerStatTest.cc NsCookieParserTest.cc \
DirectDiskAdaptorTest.cc CookieTest.cc CookieStorageTest.cc \ DirectDiskAdaptorTest.cc CookieTest.cc CookieStorageTest.cc \
TimeTest.cc CopyDiskAdaptorTest.cc FtpConnectionTest.cc \ TimeTest.cc CopyDiskAdaptorTest.cc FtpConnectionTest.cc \
OptionParserTest.cc $(am__append_1) $(am__append_2) \ OptionParserTest.cc SimpleDNSCacheTest.cc $(am__append_1) \
$(am__append_3) $(am__append_4) $(am__append_5) $(am__append_2) $(am__append_3) $(am__append_4) \
$(am__append_5)
#aria2c_CXXFLAGS = ${CPPUNIT_CFLAGS} -I../src -I../lib -Wall -D_FILE_OFFSET_BITS=64 #aria2c_CXXFLAGS = ${CPPUNIT_CFLAGS} -I../src -I../lib -Wall -D_FILE_OFFSET_BITS=64
#aria2c_LDFLAGS = ${CPPUNIT_LIBS} #aria2c_LDFLAGS = ${CPPUNIT_LIBS}
@ -811,6 +812,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ShareRatioSeedCriteriaTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ShareRatioSeedCriteriaTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SharedHandleTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SharedHandleTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SignatureTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SignatureTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SimpleDNSCacheTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SingleFileAllocationIteratorTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SingleFileAllocationIteratorTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SingleFileDownloadContextTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SingleFileDownloadContextTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SingletonHolderTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SingletonHolderTest.Po@am__quote@

View File

@ -0,0 +1,35 @@
#include "DNSCache.h"
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
#include "Exception.h"
#include "Util.h"
namespace aria2 {
class SimpleDNSCacheTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(SimpleDNSCacheTest);
CPPUNIT_TEST(testFind);
CPPUNIT_TEST_SUITE_END();
public:
void testFind();
};
CPPUNIT_TEST_SUITE_REGISTRATION(SimpleDNSCacheTest);
void SimpleDNSCacheTest::testFind()
{
SimpleDNSCache cache;
cache.put("host1", "192.168.0.1");
cache.put("host2", "192.168.1.2");
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), cache.find("host1"));
CPPUNIT_ASSERT_EQUAL(std::string("192.168.1.2"), cache.find("host2"));
CPPUNIT_ASSERT_EQUAL(std::string(""), cache.find("host3"));
}
} // namespace aria2