Prechádzať zdrojové kódy

2009-06-17 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

	Rewritten BtRegistry
	* src/BtRegistry.cc
	* src/BtRegistry.h
	* src/BtSetup.cc
	* src/ConsoleStatCalc.cc
	* src/PeerInteractionCommand.cc
	* src/PeerReceiveHandshakeCommand.cc
	* src/ReceiverMSEHandshakeCommand.cc
	* src/RequestGroup.cc
	* src/XmlRpcMethodImpl.cc
	* test/BtRegistryTest.cc
Tatsuhiro Tsujikawa 16 rokov pred
rodič
commit
eaf5217de7

+ 14 - 0
ChangeLog

@@ -1,3 +1,17 @@
+2009-06-17  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
+
+	Rewritten BtRegistry
+	* src/BtRegistry.cc
+	* src/BtRegistry.h
+	* src/BtSetup.cc
+	* src/ConsoleStatCalc.cc
+	* src/PeerInteractionCommand.cc
+	* src/PeerReceiveHandshakeCommand.cc
+	* src/ReceiverMSEHandshakeCommand.cc
+	* src/RequestGroup.cc
+	* src/XmlRpcMethodImpl.cc
+	* test/BtRegistryTest.cc
+
 2009-06-14  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
 
 	Used array_ptr for savedInfoHash, savedBitfield and pieceBitfield.

+ 18 - 85
src/BtRegistry.cc

@@ -43,103 +43,36 @@
 
 namespace aria2 {
 
-BtRegistry::BtRegistry() {}
-
-PeerStorageHandle BtRegistry::getPeerStorage(const std::string& key)
-{
-  return peerStorageMap.getHandle(key);
-}
-
-void BtRegistry::registerPeerStorage(const std::string& key,
-				     const PeerStorageHandle& peerStorage)
-{
-  peerStorageMap.registerHandle(key, peerStorage);
-}
-				  
-PieceStorageHandle
-BtRegistry::getPieceStorage(const std::string& key)
-{
-  return pieceStorageMap.getHandle(key);
-}
-
-void
-BtRegistry::registerPieceStorage(const std::string& key,
-				 const PieceStorageHandle& pieceStorage)
-{
-  pieceStorageMap.registerHandle(key, pieceStorage);
-}
-
-BtRuntimeHandle BtRegistry::getBtRuntime(const std::string& key)
-{
-  return btRuntimeMap.getHandle(key);
-}
-
-void
-BtRegistry::registerBtRuntime(const std::string& key,
-			      const BtRuntimeHandle& btRuntime)
+SharedHandle<BtContext>
+BtRegistry::getBtContext(const std::string& infoHash) const
 {
-  btRuntimeMap.registerHandle(key, btRuntime);
+  return get(infoHash)._btContext;
 }
 
-BtAnnounceHandle BtRegistry::getBtAnnounce(const std::string& key)
+void BtRegistry::put(const std::string& infoHash, const BtObject& obj)
 {
-  return btAnnounceMap.getHandle(key);
+  remove(infoHash);
+  std::map<std::string, BtObject>::value_type p(infoHash, obj);
+  _pool.insert(p);
 }
 
-void
-BtRegistry::registerBtAnnounce(const std::string& key,
-			       const BtAnnounceHandle& btAnnounce)
+BtObject BtRegistry::get(const std::string& infoHash) const
 {
-  btAnnounceMap.registerHandle(key, btAnnounce);
+  std::map<std::string, BtObject>::const_iterator i = _pool.find(infoHash);
+  if(i == _pool.end()) {
+    return BtObject();
+  } else {
+    return (*i).second;
+  }
 }
 
-BtProgressInfoFileHandle BtRegistry::getBtProgressInfoFile(const std::string& key)
+bool BtRegistry::remove(const std::string& infoHash)
 {
-  return btProgressInfoFileMap.getHandle(key);
+  return _pool.erase(infoHash);
 }
 
-void
-BtRegistry::registerBtProgressInfoFile(const std::string& key,
-				       const BtProgressInfoFileHandle& btProgressInfoFile)
-{
-  btProgressInfoFileMap.registerHandle(key, btProgressInfoFile);
-}
-
-BtContextHandle
-BtRegistry::getBtContext(const std::string& key)
-{
-  return btContextMap.getHandle(key);
-}
-
-void
-BtRegistry::registerBtContext(const std::string& key,
-			      const BtContextHandle& btContext)
-{
-  btContextMap.registerHandle(key, btContext);
-}
-
-std::deque<SharedHandle<BtContext> > BtRegistry::getAllBtContext()
-{
-  return btContextMap.getAll();
-}
-
-void BtRegistry::unregisterAll() {
-  btContextMap.clear();
-  peerStorageMap.clear();
-  pieceStorageMap.clear();
-  btAnnounceMap.clear();
-  btRuntimeMap.clear();
-  btProgressInfoFileMap.clear();
-}
-
-void BtRegistry::unregister(const std::string& key)
-{
-  btContextMap.unregisterHandle(key);
-  peerStorageMap.unregisterHandle(key);
-  pieceStorageMap.unregisterHandle(key);
-  btAnnounceMap.unregisterHandle(key);
-  btRuntimeMap.unregisterHandle(key);
-  btProgressInfoFileMap.unregisterHandle(key);
+void BtRegistry::removeAll() {
+  _pool.clear();
 }
 
 } // namespace aria2

+ 53 - 45
src/BtRegistry.h

@@ -39,9 +39,9 @@
 
 #include <string>
 #include <map>
+#include <vector>
 
 #include "SharedHandle.h"
-#include "HandleRegistry.h"
 
 namespace aria2 {
 
@@ -52,54 +52,62 @@ class BtRuntime;
 class BtProgressInfoFile;
 class BtContext;
 
-typedef HandleRegistry<std::string, PeerStorage> PeerStorageMap;
-typedef HandleRegistry<std::string, PieceStorage> PieceStorageMap;
-typedef HandleRegistry<std::string, BtAnnounce> BtAnnounceMap;
-typedef HandleRegistry<std::string, BtRuntime> BtRuntimeMap;
-typedef HandleRegistry<std::string, BtProgressInfoFile> BtProgressInfoFileMap;
-typedef HandleRegistry<std::string, BtContext>  BtContextMap;
+struct BtObject {
+  SharedHandle<BtContext> _btContext;
+  SharedHandle<PieceStorage> _pieceStorage;
+  SharedHandle<PeerStorage> _peerStorage;
+  SharedHandle<BtAnnounce> _btAnnounce;
+  SharedHandle<BtRuntime> _btRuntime;
+  SharedHandle<BtProgressInfoFile> _btProgressInfoFile;
+
+  BtObject(const SharedHandle<BtContext>& btContext,
+	   const SharedHandle<PieceStorage>& pieceStorage,
+	   const SharedHandle<PeerStorage>& peerStorage,
+	   const SharedHandle<BtAnnounce>& btAnnounce,
+	   const SharedHandle<BtRuntime>& btRuntime,
+	   const SharedHandle<BtProgressInfoFile>& btProgressInfoFile):
+    _btContext(btContext),
+    _pieceStorage(pieceStorage),
+    _peerStorage(peerStorage),
+    _btAnnounce(btAnnounce),
+    _btRuntime(btRuntime),
+    _btProgressInfoFile(btProgressInfoFile) {}
+
+  BtObject() {}
+
+  bool isNull() const
+  {
+    return _btContext.isNull() &&
+      _pieceStorage.isNull() &&
+      _peerStorage.isNull() &&
+      _btAnnounce.isNull() &&
+      _btRuntime.isNull() &&
+      _btProgressInfoFile.isNull();
+  }
+};
 
 class BtRegistry {
 private:
-  BtContextMap btContextMap;
-  PeerStorageMap peerStorageMap;
-  PieceStorageMap pieceStorageMap;
-  BtAnnounceMap btAnnounceMap;
-  BtRuntimeMap btRuntimeMap;
-  BtProgressInfoFileMap btProgressInfoFileMap;
+  std::map<std::string, BtObject> _pool;
 public:
-  BtRegistry();
-  
-  SharedHandle<BtContext> getBtContext(const std::string& key);
-  void registerBtContext(const std::string& key,
-			 const SharedHandle<BtContext>& btContext);
-
-  SharedHandle<PeerStorage> getPeerStorage(const std::string& key);
-  void registerPeerStorage(const std::string& key,
-			   const SharedHandle<PeerStorage>& peer);
-				  
-  SharedHandle<PieceStorage> getPieceStorage(const std::string& key);
-  void registerPieceStorage(const std::string& key,
-			    const SharedHandle<PieceStorage>& pieceStorage);
-
-  SharedHandle<BtRuntime> getBtRuntime(const std::string& key);
-  void registerBtRuntime(const std::string& key,
-			 const SharedHandle<BtRuntime>& btRuntime);
-
-  SharedHandle<BtAnnounce> getBtAnnounce(const std::string& key);
-  void registerBtAnnounce(const std::string& key,
-				 const SharedHandle<BtAnnounce>& btAnnounce);
-
-  SharedHandle<BtProgressInfoFile>
-  getBtProgressInfoFile(const std::string& key);
-  void registerBtProgressInfoFile(const std::string& key,
-				  const SharedHandle<BtProgressInfoFile>& file);
-
-  std::deque<SharedHandle<BtContext> > getAllBtContext();
-
-  void unregisterAll();
-
-  void unregister(const std::string& key);
+  SharedHandle<BtContext> getBtContext(const std::string& infoHash) const;
+
+  void put(const std::string& infoHash, const BtObject& obj);
+
+  BtObject get(const std::string& infoHash) const;
+
+  template<typename OutputIterator>
+  void getAllBtContext(OutputIterator dest)
+  {
+    for(std::map<std::string, BtObject>::const_iterator i = _pool.begin();
+	i != _pool.end(); ++i) {
+      *dest++ = (*i).second._btContext;
+    }
+  }
+
+  void removeAll();
+
+  bool remove(const std::string& infoHash);
 };
 
 } // namespace aria2

+ 5 - 10
src/BtSetup.cc

@@ -72,16 +72,11 @@ void BtSetup::setup(std::deque<Command*>& commands,
   if(btContext.isNull()) {
     return;
   }
-  SharedHandle<BtRegistry> btRegistry = e->getBtRegistry();
-  SharedHandle<PieceStorage> pieceStorage =
-    btRegistry->getPieceStorage(btContext->getInfoHashAsString());
-  SharedHandle<PeerStorage> peerStorage =
-    btRegistry->getPeerStorage(btContext->getInfoHashAsString());
-  SharedHandle<BtRuntime> btRuntime =
-    btRegistry->getBtRuntime(btContext->getInfoHashAsString());
-  SharedHandle<BtAnnounce> btAnnounce =
-    btRegistry->getBtAnnounce(btContext->getInfoHashAsString());
-
+  BtObject btObject = e->getBtRegistry()->get(btContext->getInfoHashAsString());
+  SharedHandle<PieceStorage> pieceStorage = btObject._pieceStorage;
+  SharedHandle<PeerStorage> peerStorage = btObject._peerStorage;
+  SharedHandle<BtRuntime> btRuntime = btObject._btRuntime;
+  SharedHandle<BtAnnounce> btAnnounce = btObject._btAnnounce;
   // commands
   {
     TrackerWatcherCommand* c =

+ 1 - 1
src/ConsoleStatCalc.cc

@@ -116,7 +116,7 @@ static void printProgress
 #ifdef ENABLE_BITTORRENT
   if(!btctx.isNull()) {
     SharedHandle<PeerStorage> ps =
-      e->getBtRegistry()->getPeerStorage(btctx->getInfoHashAsString());
+      e->getBtRegistry()->get(btctx->getInfoHashAsString())._peerStorage;
     std::deque<SharedHandle<Peer> > peers;
     ps->getActivePeers(peers);
     o << " " << "SEED:"

+ 1 - 1
src/PeerInteractionCommand.cc

@@ -100,7 +100,7 @@ PeerInteractionCommand::PeerInteractionCommand
 
   SharedHandle<BtRegistry> btRegistry = e->getBtRegistry();
   SharedHandle<PeerStorage> peerStorage =
-    btRegistry->getPeerStorage(_btContext->getInfoHashAsString());
+    btRegistry->get(_btContext->getInfoHashAsString())._peerStorage;
 
   SharedHandle<ExtensionMessageRegistry> exMsgRegistry
     (new ExtensionMessageRegistry());

+ 5 - 7
src/PeerReceiveHandshakeCommand.cc

@@ -93,13 +93,11 @@ bool PeerReceiveHandshakeCommand::executeInternal()
     // check info_hash
     std::string infoHash = Util::toHex(&data[28], INFO_HASH_LENGTH);
 
-    SharedHandle<BtRegistry> btRegistry = e->getBtRegistry();
-    SharedHandle<BtContext> btContext = btRegistry->getBtContext(infoHash);
-    SharedHandle<BtRuntime> btRuntime = btRegistry->getBtRuntime(infoHash);
-    SharedHandle<PieceStorage> pieceStorage =
-      btRegistry->getPieceStorage(infoHash);
-    SharedHandle<PeerStorage> peerStorage =
-      btRegistry->getPeerStorage(infoHash);
+    BtObject btObject = e->getBtRegistry()->get(infoHash);
+    SharedHandle<BtContext> btContext = btObject._btContext;
+    SharedHandle<BtRuntime> btRuntime = btObject._btRuntime;
+    SharedHandle<PieceStorage> pieceStorage = btObject._pieceStorage;
+    SharedHandle<PeerStorage> peerStorage = btObject._peerStorage;
 
     if(btContext.isNull() || !btRuntime->ready()) {
       throw DL_ABORT_EX

+ 3 - 2
src/ReceiverMSEHandshakeCommand.cc

@@ -135,8 +135,9 @@ bool ReceiverMSEHandshakeCommand::executeInternal()
     break;
   }
   case RECEIVER_RECEIVE_PAD_C_LENGTH: {
-    if(_mseHandshake->receiveReceiverHashAndPadCLength
-       (e->getBtRegistry()->getAllBtContext())) {
+    std::deque<SharedHandle<BtContext> > btContexts;
+    e->getBtRegistry()->getAllBtContext(std::back_inserter(btContexts));
+    if(_mseHandshake->receiveReceiverHashAndPadCLength(btContexts)) {
       _sequence = RECEIVER_RECEIVE_PAD_C;
     }
     break;

+ 10 - 16
src/RequestGroup.cc

@@ -243,20 +243,10 @@ void RequestGroup::createInitialCommand(std::deque<Command*>& commands,
 	progressInfoFile(new DefaultBtProgressInfoFile(_downloadContext,
 						       _pieceStorage,
 						       _option.get()));
-      
-      btRegistry->registerBtContext(btContext->getInfoHashAsString(),
-				    btContext);
-      btRegistry->registerPieceStorage(btContext->getInfoHashAsString(),
-				       _pieceStorage);
-      btRegistry->registerBtProgressInfoFile(btContext->getInfoHashAsString(),
-					     progressInfoFile);
-
-  
+        
       BtRuntimeHandle btRuntime(new BtRuntime());
       btRuntime->setListenPort(_option->getAsInt(PREF_LISTEN_PORT));
       btRuntime->setMaxPeers(_option->getAsInt(PREF_BT_MAX_PEERS));
-      btRegistry->registerBtRuntime(btContext->getInfoHashAsString(),
-				    btRuntime);
       _btRuntime = btRuntime;
       progressInfoFile->setBtRuntime(btRuntime);
 
@@ -264,8 +254,6 @@ void RequestGroup::createInitialCommand(std::deque<Command*>& commands,
 	(new DefaultPeerStorage(btContext, _option.get()));
       peerStorage->setBtRuntime(btRuntime);
       peerStorage->setPieceStorage(_pieceStorage);
-      btRegistry->registerPeerStorage(btContext->getInfoHashAsString(),
-				      peerStorage);
       _peerStorage = peerStorage;
       progressInfoFile->setPeerStorage(peerStorage);
 
@@ -276,10 +264,16 @@ void RequestGroup::createInitialCommand(std::deque<Command*>& commands,
       btAnnounce->setPeerStorage(peerStorage);
       btAnnounce->setUserDefinedInterval
 	(_option->getAsInt(PREF_BT_TRACKER_INTERVAL));
-      btRegistry->registerBtAnnounce(btContext->getInfoHashAsString(),
-				     btAnnounce);
       btAnnounce->shuffleAnnounce();
       
+      btRegistry->put(btContext->getInfoHashAsString(),
+		      BtObject(btContext,
+			       _pieceStorage,
+			       peerStorage,
+			       btAnnounce,
+			       btRuntime,
+			       progressInfoFile));
+
       // Remove the control file if download file doesn't exist
       if(progressInfoFile->exists() && !_pieceStorage->getDiskAdaptor()->fileExists()) {
 	progressInfoFile->removeFile();
@@ -828,7 +822,7 @@ void RequestGroup::releaseRuntimeResource(DownloadEngine* e)
     if(!btContextInReg.isNull() &&
        btContextInReg->getOwnerRequestGroup()->getGID() ==
 	btContext->getOwnerRequestGroup()->getGID()) {
-      btRegistry->unregister(btContext->getInfoHashAsString());
+      btRegistry->remove(btContext->getInfoHashAsString());
       if(!DHTRegistry::_peerAnnounceStorage.isNull()) {
 	DHTRegistry::_peerAnnounceStorage->
 	  removeLocalPeerAnnounce(btContext->getInfoHash());

+ 2 - 2
src/XmlRpcMethodImpl.cc

@@ -292,7 +292,7 @@ static void gatherProgressBitTorrent
   entryDict["infoHash"] = btctx->getInfoHashAsString();
 
   SharedHandle<PeerStorage> peerStorage =
-    btreg->getPeerStorage(btctx->getInfoHashAsString());
+    btreg->get(btctx->getInfoHashAsString())._peerStorage;
   assert(!peerStorage.isNull());
 
   std::deque<SharedHandle<Peer> > peers;
@@ -460,7 +460,7 @@ BDE GetPeersXmlRpcMethod::process
   if(!btctx.isNull()) {
     SharedHandle<BtRegistry> btreg = e->getBtRegistry();
     SharedHandle<PeerStorage> peerStorage =
-      btreg->getPeerStorage(btctx->getInfoHashAsString());
+      btreg->get(btctx->getInfoHashAsString())._peerStorage;
     assert(!peerStorage.isNull());
     BDE entry = BDE::dict();
     gatherPeer(peers, peerStorage);

+ 37 - 54
test/BtRegistryTest.cc

@@ -17,21 +17,17 @@ class BtRegistryTest:public CppUnit::TestFixture {
 
   CPPUNIT_TEST_SUITE(BtRegistryTest);
   CPPUNIT_TEST(testGetBtContext);
-  CPPUNIT_TEST(testGetPeerStorage);
-  CPPUNIT_TEST(testGetPieceStorage);
-  CPPUNIT_TEST(testGetBtRuntime);
-  CPPUNIT_TEST(testGetBtAnnounce);
-  CPPUNIT_TEST(testGetBtProgressInfoFile);
+  CPPUNIT_TEST(testGetAllBtContext);
+  CPPUNIT_TEST(testRemove);
+  CPPUNIT_TEST(testRemoveAll);
   CPPUNIT_TEST_SUITE_END();
 private:
 
 public:
   void testGetBtContext();
-  void testGetPeerStorage();
-  void testGetPieceStorage();
-  void testGetBtRuntime();
-  void testGetBtAnnounce();
-  void testGetBtProgressInfoFile();
+  void testGetAllBtContext();
+  void testRemove();
+  void testRemoveAll();
 };
 
 
@@ -42,64 +38,51 @@ void BtRegistryTest::testGetBtContext()
   BtRegistry btRegistry;
   CPPUNIT_ASSERT(btRegistry.getBtContext("test").isNull());
   SharedHandle<BtContext> btContext(new MockBtContext());
-  btRegistry.registerBtContext("test", btContext);
+  BtObject btObject;
+  btObject._btContext = btContext;
+  btRegistry.put("test", btObject);
   CPPUNIT_ASSERT_EQUAL(btContext.get(),
 		       btRegistry.getBtContext("test").get());
 }
 
-void BtRegistryTest::testGetPeerStorage() {
-  BtRegistry btRegistry;
-  CPPUNIT_ASSERT(!btRegistry.getPeerStorage("test").get());
-
-  SharedHandle<PeerStorage> peerStorage(new MockPeerStorage());
-
-  btRegistry.registerPeerStorage("test", peerStorage);
-  CPPUNIT_ASSERT_EQUAL(peerStorage.get(),
-		       btRegistry.getPeerStorage("test").get());
-}
-
-void BtRegistryTest::testGetPieceStorage() {
-  BtRegistry btRegistry;
-  CPPUNIT_ASSERT(!btRegistry.getPieceStorage("test").get());
-
-  SharedHandle<PieceStorage> pieceStorage(new MockPieceStorage());
-
-  btRegistry.registerPieceStorage("test", pieceStorage);
-  CPPUNIT_ASSERT_EQUAL(pieceStorage.get(),
-		       btRegistry.getPieceStorage("test").get());
+static void addTwoBtContext(BtRegistry& btRegistry)
+{
+  SharedHandle<BtContext> btContext1(new MockBtContext());
+  SharedHandle<BtContext> btContext2(new MockBtContext());
+  BtObject btObject1;
+  btObject1._btContext = btContext1;
+  BtObject btObject2;
+  btObject2._btContext = btContext2;
+  btRegistry.put("ctx1", btObject1);
+  btRegistry.put("ctx2", btObject2);
 }
 
-void BtRegistryTest::testGetBtRuntime() {
+void BtRegistryTest::testGetAllBtContext()
+{
   BtRegistry btRegistry;
-  CPPUNIT_ASSERT(!btRegistry.getBtRuntime("test").get());
+  addTwoBtContext(btRegistry);
 
-  SharedHandle<BtRuntime> runtime;
-
-  btRegistry.registerBtRuntime("test", runtime);
-  CPPUNIT_ASSERT_EQUAL(runtime.get(),
-		       btRegistry.getBtRuntime("test").get());
+  std::vector<SharedHandle<BtContext> > result;
+  btRegistry.getAllBtContext(std::back_inserter(result));
+  CPPUNIT_ASSERT_EQUAL((size_t)2, result.size());
 }
 
-void BtRegistryTest::testGetBtAnnounce() {
+void BtRegistryTest::testRemove()
+{
   BtRegistry btRegistry;
-  CPPUNIT_ASSERT(!btRegistry.getBtAnnounce("test").get());
-  
-  SharedHandle<BtAnnounce> btAnnounce(new MockBtAnnounce());
-
-  btRegistry.registerBtAnnounce("test", btAnnounce);
-  CPPUNIT_ASSERT_EQUAL(btAnnounce.get(),
-		       btRegistry.getBtAnnounce("test").get());
+  addTwoBtContext(btRegistry);
+  CPPUNIT_ASSERT(btRegistry.remove("ctx1"));
+  CPPUNIT_ASSERT(btRegistry.get("ctx1").isNull());
+  CPPUNIT_ASSERT(!btRegistry.get("ctx2").isNull());
 }
 
-void BtRegistryTest::testGetBtProgressInfoFile() {
+void BtRegistryTest::testRemoveAll()
+{
   BtRegistry btRegistry;
-  CPPUNIT_ASSERT(!btRegistry.getBtProgressInfoFile("test").get());
-
-  SharedHandle<BtProgressInfoFile> btProgressInfoFile(new MockBtProgressInfoFile());
-
-  btRegistry.registerBtProgressInfoFile("test", btProgressInfoFile);
-  CPPUNIT_ASSERT_EQUAL(btProgressInfoFile.get(),
-  		       btRegistry.getBtProgressInfoFile("test").get());
+  addTwoBtContext(btRegistry);
+  btRegistry.removeAll();
+  CPPUNIT_ASSERT(btRegistry.get("ctx1").isNull());
+  CPPUNIT_ASSERT(btRegistry.get("ctx2").isNull());
 }
 
 } // namespace aria2