ソースを参照

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
Tatsuhiro Tsujikawa 17 年 前
コミット
928e228c89

+ 12 - 0
ChangeLog

@@ -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>
 
 	Removed SingletonHolder of CUIDCounter. CUIDCounter is now part of

+ 16 - 10
src/DNSCache.h

@@ -36,26 +36,23 @@
 #define _D_DNS_CACHE_H_
 
 #include "common.h"
-#include "SharedHandle.h"
-#include "SingletonHolder.h"
-#include "A2STR.h"
+
 #include <string>
 #include <map>
 
+#include "A2STR.h"
+
 namespace aria2 {
 
 class DNSCache {
 public:
   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;
 };
 
-typedef SharedHandle<DNSCache> DNSCacheHandle;
-typedef SingletonHolder<DNSCacheHandle> DNSCacheSingletonHolder;
-
 class SimpleDNSCache : public DNSCache {
 private:
   std::map<std::string, std::string> _table;
@@ -64,9 +61,15 @@ public:
 
   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)
@@ -80,7 +83,10 @@ class NullDNSCache : public DNSCache {
 public:
   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) {}
 };

+ 15 - 1
src/DownloadEngine.cc

@@ -62,6 +62,7 @@
 #include "ServerStatMan.h"
 #include "CookieStorage.h"
 #include "A2STR.h"
+#include "DNSCache.h"
 
 #include "BtRegistry.h"
 #include "BtContext.h"
@@ -411,7 +412,8 @@ DownloadEngine::DownloadEngine():logger(LogFactory::getInstance()),
 				 _haltRequested(false),
 				 _noWait(false),
 				 _cookieStorage(new CookieStorage()),
-				 _btRegistry(new BtRegistry())
+				 _btRegistry(new BtRegistry()),
+				 _dnsCache(new SimpleDNSCache())
 {
 #ifdef HAVE_EPOLL
 
@@ -1064,4 +1066,16 @@ CUID DownloadEngine::newCUID()
   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

+ 7 - 0
src/DownloadEngine.h

@@ -66,6 +66,7 @@ class CheckIntegrityMan;
 class SocketCore;
 class CookieStorage;
 class BtRegistry;
+class DNSCache;
 
 class CommandEvent
 {
@@ -301,6 +302,8 @@ private:
 
   CUIDCounter _cuidCounter;
 
+  SharedHandle<DNSCache> _dnsCache;
+
   void shortSleep() const;
 
   /**
@@ -418,6 +421,10 @@ public:
   SharedHandle<BtRegistry> getBtRegistry() const;
 
   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;

+ 2 - 2
src/InitiateConnectionCommand.cc

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

+ 5 - 7
src/MultiUrlRequestInfo.cc

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

+ 2 - 1
test/Makefile.am

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

+ 7 - 5
test/Makefile.in

@@ -196,7 +196,7 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \
 	ServerStatTest.cc NsCookieParserTest.cc \
 	DirectDiskAdaptorTest.cc CookieTest.cc CookieStorageTest.cc \
 	TimeTest.cc CopyDiskAdaptorTest.cc FtpConnectionTest.cc \
-	OptionParserTest.cc GZipDecoderTest.cc \
+	OptionParserTest.cc SimpleDNSCacheTest.cc GZipDecoderTest.cc \
 	Sqlite3MozCookieParserTest.cc MessageDigestHelperTest.cc \
 	IteratableChunkChecksumValidatorTest.cc \
 	IteratableChecksumValidatorTest.cc BtAllowedFastMessageTest.cc \
@@ -369,8 +369,8 @@ am_aria2c_OBJECTS = AllTest.$(OBJEXT) TestUtil.$(OBJEXT) \
 	CookieTest.$(OBJEXT) CookieStorageTest.$(OBJEXT) \
 	TimeTest.$(OBJEXT) CopyDiskAdaptorTest.$(OBJEXT) \
 	FtpConnectionTest.$(OBJEXT) OptionParserTest.$(OBJEXT) \
-	$(am__objects_1) $(am__objects_2) $(am__objects_3) \
-	$(am__objects_4) $(am__objects_5)
+	SimpleDNSCacheTest.$(OBJEXT) $(am__objects_1) $(am__objects_2) \
+	$(am__objects_3) $(am__objects_4) $(am__objects_5)
 aria2c_OBJECTS = $(am_aria2c_OBJECTS)
 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 \
 	DirectDiskAdaptorTest.cc CookieTest.cc CookieStorageTest.cc \
 	TimeTest.cc CopyDiskAdaptorTest.cc FtpConnectionTest.cc \
-	OptionParserTest.cc $(am__append_1) $(am__append_2) \
-	$(am__append_3) $(am__append_4) $(am__append_5)
+	OptionParserTest.cc SimpleDNSCacheTest.cc $(am__append_1) \
+	$(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_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)/SharedHandleTest.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)/SingleFileDownloadContextTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SingletonHolderTest.Po@am__quote@

+ 35 - 0
test/SimpleDNSCacheTest.cc

@@ -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