浏览代码

Changed interface of BtRegistry.

BtRegistry now uses SharedHandle<BtObject> instead of BtObject.
Tatsuhiro Tsujikawa 14 年之前
父节点
当前提交
9a51cc356b

+ 37 - 42
src/BtRegistry.cc

@@ -42,6 +42,7 @@
 #include "BtProgressInfoFile.h"
 #include "bittorrent_helper.h"
 #include "LpdMessageReceiver.h"
+#include "NullHandle.h"
 
 namespace aria2 {
 
@@ -51,37 +52,41 @@ BtRegistry::BtRegistry()
 
 BtRegistry::~BtRegistry() {}
 
-SharedHandle<DownloadContext>
+const SharedHandle<DownloadContext>&
 BtRegistry::getDownloadContext(a2_gid_t gid) const
 {
-  return get(gid).downloadContext_;
+  const SharedHandle<BtObject>& res = get(gid);
+  if(res) {
+    return res->downloadContext;
+  } else {
+    return getNull<DownloadContext>();
+  }
 }
 
-SharedHandle<DownloadContext>
+const SharedHandle<DownloadContext>&
 BtRegistry::getDownloadContext(const std::string& infoHash) const
 {
-  SharedHandle<DownloadContext> dctx;
-  for(std::map<a2_gid_t, BtObject>::const_iterator i = pool_.begin(),
-        eoi = pool_.end(); i != eoi; ++i) {
-    if(bittorrent::getTorrentAttrs((*i).second.downloadContext_)->infoHash ==
+  for(std::map<a2_gid_t, SharedHandle<BtObject> >::const_iterator i =
+        pool_.begin(), eoi = pool_.end(); i != eoi; ++i) {
+    if(bittorrent::getTorrentAttrs((*i).second->downloadContext)->infoHash ==
        infoHash) {
-      dctx = (*i).second.downloadContext_;
-      break;
+      return (*i).second->downloadContext;
     }
   }      
-  return dctx;
+  return getNull<DownloadContext>();
 }
 
-void BtRegistry::put(a2_gid_t gid, const BtObject& obj)
+void BtRegistry::put(a2_gid_t gid, const SharedHandle<BtObject>& obj)
 {
   pool_[gid] = obj;
 }
 
-BtObject BtRegistry::get(a2_gid_t gid) const
+const SharedHandle<BtObject>& BtRegistry::get(a2_gid_t gid) const
 {
-  std::map<a2_gid_t, BtObject>::const_iterator i = pool_.find(gid);
+  std::map<a2_gid_t, SharedHandle<BtObject> >::const_iterator i =
+    pool_.find(gid);
   if(i == pool_.end()) {
-    return BtObject();
+    return getNull<BtObject>();
   } else {
     return (*i).second;
   }
@@ -109,23 +114,23 @@ BtObject::BtObject
  const SharedHandle<BtAnnounce>& btAnnounce,
  const SharedHandle<BtRuntime>& btRuntime,
  const SharedHandle<BtProgressInfoFile>& btProgressInfoFile)
-  : downloadContext_(downloadContext),
-    pieceStorage_(pieceStorage),
-    peerStorage_(peerStorage),
-    btAnnounce_(btAnnounce),
-    btRuntime_(btRuntime),
-    btProgressInfoFile_(btProgressInfoFile)
+  : downloadContext(downloadContext),
+    pieceStorage(pieceStorage),
+    peerStorage(peerStorage),
+    btAnnounce(btAnnounce),
+    btRuntime(btRuntime),
+    btProgressInfoFile(btProgressInfoFile)
 {}
 
 BtObject::BtObject() {}
 
 BtObject::BtObject(const BtObject& c)
-  : downloadContext_(c.downloadContext_),
-    pieceStorage_(c.pieceStorage_),
-    peerStorage_(c.peerStorage_),
-    btAnnounce_(c.btAnnounce_),
-    btRuntime_(c.btRuntime_),
-    btProgressInfoFile_(c.btProgressInfoFile_)
+  : downloadContext(c.downloadContext),
+    pieceStorage(c.pieceStorage),
+    peerStorage(c.peerStorage),
+    btAnnounce(c.btAnnounce),
+    btRuntime(c.btRuntime),
+    btProgressInfoFile(c.btProgressInfoFile)
 {}
 
 BtObject::~BtObject() {}
@@ -133,24 +138,14 @@ BtObject::~BtObject() {}
 BtObject& BtObject::operator=(const BtObject& c)
 {
   if(this != &c) {
-    downloadContext_ = c.downloadContext_;
-    pieceStorage_ = c.pieceStorage_;
-    peerStorage_ = c.peerStorage_;
-    btAnnounce_ = c.btAnnounce_;
-    btRuntime_ = c.btRuntime_;
-    btProgressInfoFile_ = c.btProgressInfoFile_;
+    downloadContext = c.downloadContext;
+    pieceStorage = c.pieceStorage;
+    peerStorage = c.peerStorage;
+    btAnnounce = c.btAnnounce;
+    btRuntime = c.btRuntime;
+    btProgressInfoFile = c.btProgressInfoFile;
   }
   return *this;
 }
 
-bool BtObject::isNull() const
-{
-  return !downloadContext_ &&
-    !pieceStorage_ &&
-    !peerStorage_ &&
-    !btAnnounce_ &&
-    !btRuntime_ &&
-    !btProgressInfoFile_;
-}
-
 } // namespace aria2

+ 14 - 16
src/BtRegistry.h

@@ -53,12 +53,12 @@ class DownloadContext;
 class LpdMessageReceiver;
 
 struct BtObject {
-  SharedHandle<DownloadContext> downloadContext_;
-  SharedHandle<PieceStorage> pieceStorage_;
-  SharedHandle<PeerStorage> peerStorage_;
-  SharedHandle<BtAnnounce> btAnnounce_;
-  SharedHandle<BtRuntime> btRuntime_;
-  SharedHandle<BtProgressInfoFile> btProgressInfoFile_;
+  SharedHandle<DownloadContext> downloadContext;
+  SharedHandle<PieceStorage> pieceStorage;
+  SharedHandle<PeerStorage> peerStorage;
+  SharedHandle<BtAnnounce> btAnnounce;
+  SharedHandle<BtRuntime> btRuntime;
+  SharedHandle<BtProgressInfoFile> btProgressInfoFile;
 
   BtObject(const SharedHandle<DownloadContext>& downloadContext,
            const SharedHandle<PieceStorage>& pieceStorage,
@@ -74,35 +74,33 @@ struct BtObject {
   ~BtObject();
 
   BtObject& operator=(const BtObject& c);
-
-  bool isNull() const;
 };
 
 class BtRegistry {
 private:
-  std::map<a2_gid_t, BtObject> pool_;
+  std::map<a2_gid_t, SharedHandle<BtObject> > pool_;
   uint16_t tcpPort_;
   SharedHandle<LpdMessageReceiver> lpdMessageReceiver_;
 public:
   BtRegistry();
   ~BtRegistry();
 
-  SharedHandle<DownloadContext>
+  const SharedHandle<DownloadContext>&
   getDownloadContext(a2_gid_t gid) const;
 
-  SharedHandle<DownloadContext>
+  const SharedHandle<DownloadContext>&
   getDownloadContext(const std::string& infoHash) const;
 
-  void put(a2_gid_t gid, const BtObject& obj);
+  void put(a2_gid_t gid, const SharedHandle<BtObject>& obj);
 
-  BtObject get(a2_gid_t gid) const;
+  const SharedHandle<BtObject>& get(a2_gid_t gid) const;
 
   template<typename OutputIterator>
   OutputIterator getAllDownloadContext(OutputIterator dest)
   {
-    for(std::map<a2_gid_t, BtObject>::const_iterator i = pool_.begin(),
-          eoi = pool_.end(); i != eoi; ++i) {
-      *dest++ = (*i).second.downloadContext_;
+    for(std::map<a2_gid_t, SharedHandle<BtObject> >::const_iterator i =
+          pool_.begin(), eoi = pool_.end(); i != eoi; ++i) {
+      *dest++ = (*i).second->downloadContext;
     }
     return dest;
   }

+ 5 - 5
src/BtSetup.cc

@@ -101,11 +101,11 @@ void BtSetup::setup(std::vector<Command*>& commands,
     bittorrent::getTorrentAttrs(requestGroup->getDownloadContext());
   bool metadataGetMode = torrentAttrs->metadata.empty();
   const SharedHandle<BtRegistry>& btReg = e->getBtRegistry();
-  BtObject btObject = btReg->get(requestGroup->getGID());
-  SharedHandle<PieceStorage> pieceStorage = btObject.pieceStorage_;
-  SharedHandle<PeerStorage> peerStorage = btObject.peerStorage_;
-  SharedHandle<BtRuntime> btRuntime = btObject.btRuntime_;
-  SharedHandle<BtAnnounce> btAnnounce = btObject.btAnnounce_;
+  const SharedHandle<BtObject>& btObject = btReg->get(requestGroup->getGID());
+  const SharedHandle<PieceStorage>& pieceStorage = btObject->pieceStorage;
+  const SharedHandle<PeerStorage>& peerStorage = btObject->peerStorage;
+  const SharedHandle<BtRuntime>& btRuntime = btObject->btRuntime;
+  const SharedHandle<BtAnnounce>& btAnnounce = btObject->btAnnounce;
   // commands
   {
     TrackerWatcherCommand* c =

+ 3 - 4
src/ConsoleStatCalc.cc

@@ -140,11 +140,10 @@ void printProgress
     << "CN:"
     << rg->getNumConnection();
 #ifdef ENABLE_BITTORRENT
-  SharedHandle<PeerStorage> ps =
-    e->getBtRegistry()->get(rg->getGID()).peerStorage_;
-  if(ps) {
+  const SharedHandle<BtObject>& btObj = e->getBtRegistry()->get(rg->getGID());
+  if(btObj) {
     std::vector<SharedHandle<Peer> > peers;
-    ps->getActivePeers(peers);
+    btObj->peerStorage->getActivePeers(peers);
     o << " " << "SEED:"
       << countSeeder(peers.begin(), peers.end());
   }

+ 3 - 3
src/LpdReceiveMessageCommand.cc

@@ -94,9 +94,9 @@ bool LpdReceiveMessageCommand::execute()
     }
     RequestGroup* group = dctx->getOwnerRequestGroup();
     assert(group);
-    BtObject btobj = reg->get(group->getGID());
-    assert(!btobj.isNull());
-    SharedHandle<PeerStorage> peerStorage = btobj.peerStorage_;
+    const SharedHandle<BtObject>& btobj = reg->get(group->getGID());
+    assert(btobj);
+    const SharedHandle<PeerStorage>& peerStorage = btobj->peerStorage;
     assert(peerStorage);
     SharedHandle<Peer> peer = m->peer;
     if(peerStorage->addPeer(peer)) {

+ 2 - 1
src/Makefile.am

@@ -229,7 +229,8 @@ SRCS =  Socket.h\
 	NullOutputFile.h\
 	console.cc console.h\
 	BufferedFile.cc BufferedFile.h\
-	SegList.h
+	SegList.h\
+	NullHandle.h
 
 if MINGW_BUILD
 SRCS += WinConsoleFile.cc WinConsoleFile.h

+ 53 - 0
src/NullHandle.h

@@ -0,0 +1,53 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2011 Tatsuhiro Tsujikawa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef D_NULL_HANDLE_H
+#define D_NULL_HANDLE_H
+
+#include "SharedHandle.h"
+
+namespace aria2 {
+
+// Returns const reference of SharedHandle<T>().  Static variable null
+// is shared by all instantiation of this function template.
+template<typename T>
+const SharedHandle<T>& getNull()
+{
+  static SharedHandle<T> null;
+  return null;
+}
+
+} // namespace aria2
+
+#endif // D_NULL_HANDLE_H

+ 5 - 4
src/PeerReceiveHandshakeCommand.cc

@@ -109,11 +109,12 @@ bool PeerReceiveHandshakeCommand::executeInternal()
         (fmt("Unknown info hash %s",
              util::toHex(infoHash).c_str()));
     }
-    BtObject btObject = getDownloadEngine()->getBtRegistry()->get
+    const SharedHandle<BtObject>& btObject =
+      getDownloadEngine()->getBtRegistry()->get
       (downloadContext->getOwnerRequestGroup()->getGID());
-    SharedHandle<BtRuntime> btRuntime = btObject.btRuntime_;
-    SharedHandle<PieceStorage> pieceStorage = btObject.pieceStorage_;
-    SharedHandle<PeerStorage> peerStorage = btObject.peerStorage_;
+    const SharedHandle<BtRuntime>& btRuntime = btObject->btRuntime;
+    const SharedHandle<PieceStorage>& pieceStorage = btObject->pieceStorage;
+    const SharedHandle<PeerStorage>& peerStorage = btObject->peerStorage;
     if(!btRuntime->ready()) {
       throw DL_ABORT_EX
         (fmt("Unknown info hash %s",

+ 11 - 10
src/RequestGroup.cc

@@ -354,17 +354,18 @@ void RequestGroup::createInitialCommand
         (option_->getAsInt(PREF_BT_TRACKER_INTERVAL));
       btAnnounce->shuffleAnnounce();
       
-      assert(btRegistry->get(gid_).isNull());
+      assert(!btRegistry->get(gid_));
       btRegistry->put
-        (gid_, BtObject
-         (downloadContext_,
-          pieceStorage_,
-          peerStorage,
-          btAnnounce,
-          btRuntime,
-          (progressInfoFile ?
-           SharedHandle<BtProgressInfoFile>(progressInfoFile) :
-           progressInfoFile_)));
+        (gid_, SharedHandle<BtObject>
+         (new BtObject
+          (downloadContext_,
+           pieceStorage_,
+           peerStorage,
+           btAnnounce,
+           btRuntime,
+           (progressInfoFile ?
+            SharedHandle<BtProgressInfoFile>(progressInfoFile) :
+            progressInfoFile_))));
       if(metadataGetMode) {
         if(option_->getAsBool(PREF_ENABLE_DHT) ||
            (!e->getOption()->getAsBool(PREF_DISABLE_IPV6) &&

+ 14 - 11
src/RpcMethodImpl.cc

@@ -753,7 +753,7 @@ namespace {
 void gatherProgressBitTorrent
 (const SharedHandle<Dict>& entryDict,
  const SharedHandle<TorrentAttribute>& torrentAttrs,
- const BtObject& btObject,
+ const SharedHandle<BtObject>& btObject,
  const std::vector<std::string>& keys)
 {
   if(requested_key(keys, KEY_INFO_HASH)) {
@@ -765,10 +765,10 @@ void gatherProgressBitTorrent
     entryDict->put(KEY_BITTORRENT, btDict);
   }
   if(requested_key(keys, KEY_NUM_SEEDERS)) {
-    if(btObject.isNull()) {
+    if(!btObject) {
       entryDict->put(KEY_NUM_SEEDERS, VLB_ZERO);
     } else {
-      SharedHandle<PeerStorage> peerStorage = btObject.peerStorage_;
+      const SharedHandle<PeerStorage>& peerStorage = btObject->peerStorage;
       assert(peerStorage);
       std::vector<SharedHandle<Peer> > peers;
       peerStorage->getActivePeers(peers);
@@ -822,7 +822,8 @@ void gatherProgress
   if(group->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)) {
     SharedHandle<TorrentAttribute> torrentAttrs =
       bittorrent::getTorrentAttrs(group->getDownloadContext());
-    BtObject btObject = e->getBtRegistry()->get(group->getGID());
+    const SharedHandle<BtObject>& btObject =
+      e->getBtRegistry()->get(group->getGID());
     gatherProgressBitTorrent(entryDict, torrentAttrs, btObject, keys);
   }
 #endif // ENABLE_BITTORRENT
@@ -981,10 +982,11 @@ SharedHandle<ValueBase> GetPeersRpcMethod::process
            util::itos(gid).c_str()));
   }
   SharedHandle<List> peers = List::g();
-  BtObject btObject = e->getBtRegistry()->get(group->getGID());
-  if(!btObject.isNull()) {
-    assert(btObject.peerStorage_);
-    gatherPeer(peers, btObject.peerStorage_);
+  const SharedHandle<BtObject>& btObject =
+    e->getBtRegistry()->get(group->getGID());
+  if(btObject) {
+    assert(btObject->peerStorage);
+    gatherPeer(peers, btObject->peerStorage);
   }
   return peers;
 }
@@ -1153,10 +1155,11 @@ void changeOption
     group->setMaxUploadSpeedLimit(option.getAsInt(PREF_MAX_UPLOAD_LIMIT));
   }
 #ifdef ENABLE_BITTORRENT
-  BtObject btObject = e->getBtRegistry()->get(group->getGID());
-  if(!btObject.isNull()) {
+  const SharedHandle<BtObject>& btObject =
+    e->getBtRegistry()->get(group->getGID());
+  if(btObject) {
     if(option.defined(PREF_BT_MAX_PEERS)) {
-      btObject.btRuntime_->setMaxPeers(option.getAsInt(PREF_BT_MAX_PEERS));
+      btObject->btRuntime->setMaxPeers(option.getAsInt(PREF_BT_MAX_PEERS));
     }
   }
 #endif // ENABLE_BITTORRENT

+ 10 - 10
test/BtRegistryTest.cc

@@ -41,8 +41,8 @@ void BtRegistryTest::testGetDownloadContext()
   BtRegistry btRegistry;
   CPPUNIT_ASSERT(!btRegistry.getDownloadContext(1));
   SharedHandle<DownloadContext> dctx(new DownloadContext());
-  BtObject btObject;
-  btObject.downloadContext_ = dctx;
+  SharedHandle<BtObject> btObject(new BtObject());
+  btObject->downloadContext = dctx;
   btRegistry.put(1, btObject);
   CPPUNIT_ASSERT_EQUAL(dctx.get(), btRegistry.getDownloadContext(1).get());
 }
@@ -52,10 +52,10 @@ void addTwoDownloadContext(BtRegistry& btRegistry)
 {
   SharedHandle<DownloadContext> dctx1(new DownloadContext());
   SharedHandle<DownloadContext> dctx2(new DownloadContext());
-  BtObject btObject1;
-  btObject1.downloadContext_ = dctx1;
-  BtObject btObject2;
-  btObject2.downloadContext_ = dctx2;
+  SharedHandle<BtObject> btObject1(new BtObject());
+  btObject1->downloadContext = dctx1;
+  SharedHandle<BtObject> btObject2(new BtObject());
+  btObject2->downloadContext = dctx2;
   btRegistry.put(1, btObject1);
   btRegistry.put(2, btObject2);
 }
@@ -95,8 +95,8 @@ void BtRegistryTest::testRemove()
   BtRegistry btRegistry;
   addTwoDownloadContext(btRegistry);
   CPPUNIT_ASSERT(btRegistry.remove(1));
-  CPPUNIT_ASSERT(btRegistry.get(1).isNull());
-  CPPUNIT_ASSERT(!btRegistry.get(2).isNull());
+  CPPUNIT_ASSERT(!btRegistry.get(1));
+  CPPUNIT_ASSERT(btRegistry.get(2));
 }
 
 void BtRegistryTest::testRemoveAll()
@@ -104,8 +104,8 @@ void BtRegistryTest::testRemoveAll()
   BtRegistry btRegistry;
   addTwoDownloadContext(btRegistry);
   btRegistry.removeAll();
-  CPPUNIT_ASSERT(btRegistry.get(1).isNull());
-  CPPUNIT_ASSERT(btRegistry.get(2).isNull());
+  CPPUNIT_ASSERT(!btRegistry.get(1));
+  CPPUNIT_ASSERT(!btRegistry.get(2));
 }
 
 } // namespace aria2

+ 3 - 3
test/RpcMethodTest.cc

@@ -452,8 +452,8 @@ void RpcMethodTest::testChangeOption()
   opt->put(PREF_BT_REQUEST_PEER_SPEED_LIMIT->k, "300K");
   opt->put(PREF_MAX_UPLOAD_LIMIT->k, "50K");
 
-  BtObject btObject;
-  btObject.btRuntime_ = SharedHandle<BtRuntime>(new BtRuntime());
+  SharedHandle<BtObject> btObject(new BtObject());
+  btObject->btRuntime = SharedHandle<BtRuntime>(new BtRuntime());
   e_->getBtRegistry()->put(group->getGID(), btObject);
 #endif // ENABLE_BITTORRENT
   req.params->append(opt);
@@ -471,7 +471,7 @@ void RpcMethodTest::testChangeOption()
                        option->get(PREF_BT_REQUEST_PEER_SPEED_LIMIT));
 
   CPPUNIT_ASSERT_EQUAL(std::string("100"), option->get(PREF_BT_MAX_PEERS));
-  CPPUNIT_ASSERT_EQUAL((unsigned int)100, btObject.btRuntime_->getMaxPeers());
+  CPPUNIT_ASSERT_EQUAL((unsigned int)100, btObject->btRuntime->getMaxPeers());
 
   CPPUNIT_ASSERT_EQUAL((unsigned int)50*1024,
                        group->getMaxUploadSpeedLimit());