Bläddra i källkod

Use std::unique_ptr in DHTRegistry

Tatsuhiro Tsujikawa 12 år sedan
förälder
incheckning
f022402dc9

+ 4 - 4
src/BtSetup.cc

@@ -139,8 +139,8 @@ void BtSetup::setup(std::vector<std::unique_ptr<Command>>& commands,
     if(DHTRegistry::isInitialized()) {
       auto command = make_unique<DHTGetPeersCommand>
         (e->newCUID(), requestGroup, e);
-      command->setTaskQueue(DHTRegistry::getData().taskQueue);
-      command->setTaskFactory(DHTRegistry::getData().taskFactory);
+      command->setTaskQueue(DHTRegistry::getData().taskQueue.get());
+      command->setTaskFactory(DHTRegistry::getData().taskFactory.get());
       command->setBtRuntime(btRuntime);
       command->setPeerStorage(peerStorage);
       commands.push_back(std::move(command));
@@ -148,8 +148,8 @@ void BtSetup::setup(std::vector<std::unique_ptr<Command>>& commands,
     if(DHTRegistry::isInitialized6()) {
       auto command = make_unique<DHTGetPeersCommand>
         (e->newCUID(), requestGroup, e);
-      command->setTaskQueue(DHTRegistry::getData6().taskQueue);
-      command->setTaskFactory(DHTRegistry::getData6().taskFactory);
+      command->setTaskQueue(DHTRegistry::getData6().taskQueue.get());
+      command->setTaskFactory(DHTRegistry::getData6().taskFactory.get());
       command->setBtRuntime(btRuntime);
       command->setPeerStorage(peerStorage);
       commands.push_back(std::move(command));

+ 4 - 4
src/DHTAutoSaveCommand.cc

@@ -58,8 +58,9 @@ namespace aria2 {
 
 DHTAutoSaveCommand::DHTAutoSaveCommand
 (cuid_t cuid, DownloadEngine* e, int family, time_t interval)
- : TimeBasedCommand(cuid, e, interval),
-   family_(family)
+  : TimeBasedCommand{cuid, e, interval},
+    family_{family},
+    routingTable_{nullptr}
 {}
 
 DHTAutoSaveCommand::~DHTAutoSaveCommand() {}
@@ -122,8 +123,7 @@ void DHTAutoSaveCommand::setLocalNode(const std::shared_ptr<DHTNode>& localNode)
   localNode_ = localNode;
 }
 
-void DHTAutoSaveCommand::setRoutingTable
-(const std::shared_ptr<DHTRoutingTable>& routingTable)
+void DHTAutoSaveCommand::setRoutingTable(DHTRoutingTable* routingTable)
 {
   routingTable_ = routingTable;
 }

+ 2 - 2
src/DHTAutoSaveCommand.h

@@ -51,7 +51,7 @@ private:
 
   std::shared_ptr<DHTNode> localNode_;
 
-  std::shared_ptr<DHTRoutingTable> routingTable_;
+  DHTRoutingTable* routingTable_;
 
   void save();
 public:
@@ -66,7 +66,7 @@ public:
 
   void setLocalNode(const std::shared_ptr<DHTNode>& localNode);
 
-  void setRoutingTable(const std::shared_ptr<DHTRoutingTable>& routingTable);
+  void setRoutingTable(DHTRoutingTable* routingTable);
 };
 
 } // namespace aria2

+ 9 - 8
src/DHTBucketRefreshCommand.cc

@@ -43,8 +43,12 @@
 namespace aria2 {
 
 DHTBucketRefreshCommand::DHTBucketRefreshCommand
-(cuid_t cuid, DownloadEngine* e, time_t interval):
-  TimeBasedCommand(cuid, e, interval) {}
+(cuid_t cuid, DownloadEngine* e, time_t interval)
+  : TimeBasedCommand{cuid, e, interval},
+    routingTable_{nullptr},
+    taskQueue_{nullptr},
+    taskFactory_{nullptr}
+{}
 
 DHTBucketRefreshCommand::~DHTBucketRefreshCommand() {}
 
@@ -61,20 +65,17 @@ void DHTBucketRefreshCommand::process()
   taskQueue_->addPeriodicTask1(taskFactory_->createBucketRefreshTask());
 }
 
-void DHTBucketRefreshCommand::setRoutingTable
-(const std::shared_ptr<DHTRoutingTable>& routingTable)
+void DHTBucketRefreshCommand::setRoutingTable(DHTRoutingTable* routingTable)
 {
   routingTable_ = routingTable;
 }
 
-void DHTBucketRefreshCommand::setTaskQueue
-(const std::shared_ptr<DHTTaskQueue>& taskQueue)
+void DHTBucketRefreshCommand::setTaskQueue(DHTTaskQueue* taskQueue)
 {
   taskQueue_ = taskQueue;
 }
 
-void DHTBucketRefreshCommand::setTaskFactory
-(const std::shared_ptr<DHTTaskFactory>& taskFactory)
+void DHTBucketRefreshCommand::setTaskFactory(DHTTaskFactory* taskFactory)
 {
   taskFactory_ = taskFactory;
 }

+ 6 - 6
src/DHTBucketRefreshCommand.h

@@ -47,11 +47,11 @@ class DHTTaskFactory;
 
 class DHTBucketRefreshCommand:public TimeBasedCommand {
 private:
-  std::shared_ptr<DHTRoutingTable> routingTable_;
+  DHTRoutingTable* routingTable_;
 
-  std::shared_ptr<DHTTaskQueue> taskQueue_;
+  DHTTaskQueue* taskQueue_;
 
-  std::shared_ptr<DHTTaskFactory> taskFactory_;
+  DHTTaskFactory* taskFactory_;
 public:
   DHTBucketRefreshCommand(cuid_t cuid, DownloadEngine* e, time_t interval);
 
@@ -61,11 +61,11 @@ public:
 
   virtual void process();
 
-  void setRoutingTable(const std::shared_ptr<DHTRoutingTable>& routingTable);
+  void setRoutingTable(DHTRoutingTable* routingTable);
 
-  void setTaskQueue(const std::shared_ptr<DHTTaskQueue>& taskQueue);
+  void setTaskQueue(DHTTaskQueue* taskQueue);
 
-  void setTaskFactory(const std::shared_ptr<DHTTaskFactory>& taskFactory);
+  void setTaskFactory(DHTTaskFactory* taskFactory);
 };
 
 } // namespace aria2

+ 13 - 11
src/DHTEntryPointNameResolveCommand.cc

@@ -58,15 +58,18 @@ namespace aria2 {
 
 DHTEntryPointNameResolveCommand::DHTEntryPointNameResolveCommand
 (cuid_t cuid, DownloadEngine* e,
- const std::vector<std::pair<std::string, uint16_t> >& entryPoints):
-  Command(cuid),
-  e_(e),
+ const std::vector<std::pair<std::string, uint16_t> >& entryPoints)
+  : Command{cuid},
+    e_{e},
 #ifdef ENABLE_ASYNC_DNS
-  asyncNameResolverMan_(new AsyncNameResolverMan()),
+    asyncNameResolverMan_{make_unique<AsyncNameResolverMan>()},
 #endif // ENABLE_ASYNC_DNS
-  entryPoints_(entryPoints.begin(), entryPoints.end()),
-  numSuccess_(0),
-  bootstrapEnabled_(false)
+    taskQueue_{nullptr},
+    taskFactory_{nullptr},
+    routingTable_{nullptr},
+    entryPoints_(std::begin(entryPoints), std::end(entryPoints)),
+    numSuccess_{0},
+    bootstrapEnabled_{false}
 {
 #ifdef ENABLE_ASYNC_DNS
   configureAsyncNameResolverMan(asyncNameResolverMan_.get(), e_->getOption());
@@ -197,20 +200,19 @@ void DHTEntryPointNameResolveCommand::setBootstrapEnabled(bool f)
   bootstrapEnabled_ = f;
 }
 
-void DHTEntryPointNameResolveCommand::setTaskQueue
-(const std::shared_ptr<DHTTaskQueue>& taskQueue)
+void DHTEntryPointNameResolveCommand::setTaskQueue(DHTTaskQueue* taskQueue)
 {
   taskQueue_ = taskQueue;
 }
 
 void DHTEntryPointNameResolveCommand::setTaskFactory
-(const std::shared_ptr<DHTTaskFactory>& taskFactory)
+(DHTTaskFactory* taskFactory)
 {
   taskFactory_ = taskFactory;
 }
 
 void DHTEntryPointNameResolveCommand::setRoutingTable
-(const std::shared_ptr<DHTRoutingTable>& routingTable)
+(DHTRoutingTable* routingTable)
 {
   routingTable_ = routingTable;
 }

+ 7 - 7
src/DHTEntryPointNameResolveCommand.h

@@ -62,15 +62,15 @@ private:
   std::shared_ptr<AsyncNameResolverMan> asyncNameResolverMan_;
 #endif // ENABLE_ASYNC_DNS
 
-  std::shared_ptr<DHTTaskQueue> taskQueue_;
+  DHTTaskQueue* taskQueue_;
 
-  std::shared_ptr<DHTTaskFactory> taskFactory_;
+  DHTTaskFactory* taskFactory_;
 
-  std::shared_ptr<DHTRoutingTable> routingTable_;
+  DHTRoutingTable* routingTable_;
 
   std::shared_ptr<DHTNode> localNode_;
 
-  std::deque<std::pair<std::string, uint16_t> > entryPoints_;
+  std::deque<std::pair<std::string, uint16_t>> entryPoints_;
 
   int numSuccess_;
 
@@ -94,11 +94,11 @@ public:
 
   void setBootstrapEnabled(bool f);
 
-  void setTaskQueue(const std::shared_ptr<DHTTaskQueue>& taskQueue);
+  void setTaskQueue(DHTTaskQueue* taskQueue);
 
-  void setTaskFactory(const std::shared_ptr<DHTTaskFactory>& taskFactory);
+  void setTaskFactory(DHTTaskFactory* taskFactory);
 
-  void setRoutingTable(const std::shared_ptr<DHTRoutingTable>& routingTable);
+  void setRoutingTable(DHTRoutingTable* routingTable);
 
   void setLocalNode(const std::shared_ptr<DHTNode>& localNode);
 };

+ 9 - 7
src/DHTGetPeersCommand.cc

@@ -71,11 +71,13 @@ DHTGetPeersCommand::DHTGetPeersCommand
 (cuid_t cuid,
  RequestGroup* requestGroup,
  DownloadEngine* e)
-  : Command(cuid),
-    requestGroup_(requestGroup),
-    e_(e),
-    numRetry_(0),
-    lastGetPeerTime_(0)
+  : Command{cuid},
+    requestGroup_{requestGroup},
+    e_{e},
+    taskQueue_{nullptr},
+    taskFactory_{nullptr},
+    numRetry_{0},
+    lastGetPeerTime_{0}
 {
   requestGroup_->increaseNumCommand();
 }
@@ -130,12 +132,12 @@ bool DHTGetPeersCommand::execute()
   return false;
 }
 
-void DHTGetPeersCommand::setTaskQueue(const std::shared_ptr<DHTTaskQueue>& taskQueue)
+void DHTGetPeersCommand::setTaskQueue(DHTTaskQueue* taskQueue)
 {
   taskQueue_ = taskQueue;
 }
 
-void DHTGetPeersCommand::setTaskFactory(const std::shared_ptr<DHTTaskFactory>& taskFactory)
+void DHTGetPeersCommand::setTaskFactory(DHTTaskFactory* taskFactory)
 {
   taskFactory_ = taskFactory;
 }

+ 4 - 4
src/DHTGetPeersCommand.h

@@ -62,9 +62,9 @@ private:
 
   DownloadEngine* e_;
 
-  std::shared_ptr<DHTTaskQueue> taskQueue_;
+  DHTTaskQueue* taskQueue_;
 
-  std::shared_ptr<DHTTaskFactory> taskFactory_;
+  DHTTaskFactory* taskFactory_;
 
   std::shared_ptr<DHTTask> task_;
 
@@ -79,9 +79,9 @@ public:
 
   virtual bool execute();
 
-  void setTaskQueue(const std::shared_ptr<DHTTaskQueue>& taskQueue);
+  void setTaskQueue(DHTTaskQueue* taskQueue);
 
-  void setTaskFactory(const std::shared_ptr<DHTTaskFactory>& taskFactory);
+  void setTaskFactory(DHTTaskFactory* taskFactory);
 
   void setBtRuntime(const std::shared_ptr<BtRuntime>& btRuntime);
 

+ 9 - 5
src/DHTInteractionCommand.cc

@@ -57,8 +57,11 @@ namespace aria2 {
 // TODO This name of this command is misleading, because now it also
 // handles UDP trackers as well as DHT.
 DHTInteractionCommand::DHTInteractionCommand(cuid_t cuid, DownloadEngine* e)
-  : Command(cuid),
-    e_(e)
+  : Command{cuid},
+    e_{e},
+    dispatcher_{nullptr},
+    receiver_{nullptr},
+    taskQueue_{nullptr}
 {}
 
 DHTInteractionCommand::~DHTInteractionCommand()
@@ -142,17 +145,18 @@ bool DHTInteractionCommand::execute()
   return false;
 }
 
-void DHTInteractionCommand::setMessageDispatcher(const std::shared_ptr<DHTMessageDispatcher>& dispatcher)
+void DHTInteractionCommand::setMessageDispatcher
+(DHTMessageDispatcher* dispatcher)
 {
   dispatcher_ = dispatcher;
 }
 
-void DHTInteractionCommand::setMessageReceiver(const std::shared_ptr<DHTMessageReceiver>& receiver)
+void DHTInteractionCommand::setMessageReceiver(DHTMessageReceiver* receiver)
 {
   receiver_ = receiver;
 }
 
-void DHTInteractionCommand::setTaskQueue(const std::shared_ptr<DHTTaskQueue>& taskQueue)
+void DHTInteractionCommand::setTaskQueue(DHTTaskQueue* taskQueue)
 {
   taskQueue_ = taskQueue;
 }

+ 6 - 6
src/DHTInteractionCommand.h

@@ -52,9 +52,9 @@ class UDPTrackerClient;
 class DHTInteractionCommand:public Command {
 private:
   DownloadEngine* e_;
-  std::shared_ptr<DHTMessageDispatcher> dispatcher_;
-  std::shared_ptr<DHTMessageReceiver> receiver_;
-  std::shared_ptr<DHTTaskQueue> taskQueue_;
+  DHTMessageDispatcher* dispatcher_;
+  DHTMessageReceiver* receiver_;
+  DHTTaskQueue* taskQueue_;
   std::shared_ptr<SocketCore> readCheckSocket_;
   std::shared_ptr<DHTConnection> connection_;
   std::shared_ptr<UDPTrackerClient> udpTrackerClient_;
@@ -69,11 +69,11 @@ public:
 
   void disableReadCheckSocket(const std::shared_ptr<SocketCore>& socket);
 
-  void setMessageDispatcher(const std::shared_ptr<DHTMessageDispatcher>& dispatcher);
+  void setMessageDispatcher(DHTMessageDispatcher* dispatcher);
 
-  void setMessageReceiver(const std::shared_ptr<DHTMessageReceiver>& receiver);
+  void setMessageReceiver(DHTMessageReceiver* receiver);
 
-  void setTaskQueue(const std::shared_ptr<DHTTaskQueue>& taskQueue);
+  void setTaskQueue(DHTTaskQueue* taskQueue);
 
   void setConnection(const std::shared_ptr<DHTConnection>& connection);
 

+ 5 - 3
src/DHTMessageReceiver.cc

@@ -58,7 +58,9 @@ namespace aria2 {
 
 DHTMessageReceiver::DHTMessageReceiver
 (const std::shared_ptr<DHTMessageTracker>& tracker)
-  : tracker_{tracker}
+  : tracker_{tracker},
+    factory_{nullptr},
+    routingTable_{nullptr}
 {}
 
 std::unique_ptr<DHTMessage> DHTMessageReceiver::receiveMessage
@@ -146,12 +148,12 @@ void DHTMessageReceiver::setConnection(const std::shared_ptr<DHTConnection>& con
   connection_ = connection;
 }
 
-void DHTMessageReceiver::setMessageFactory(const std::shared_ptr<DHTMessageFactory>& factory)
+void DHTMessageReceiver::setMessageFactory(DHTMessageFactory* factory)
 {
   factory_ = factory;
 }
 
-void DHTMessageReceiver::setRoutingTable(const std::shared_ptr<DHTRoutingTable>& routingTable)
+void DHTMessageReceiver::setRoutingTable(DHTRoutingTable* routingTable)
 {
   routingTable_ = routingTable;
 }

+ 4 - 4
src/DHTMessageReceiver.h

@@ -55,9 +55,9 @@ private:
 
   std::shared_ptr<DHTConnection> connection_;
 
-  std::shared_ptr<DHTMessageFactory> factory_;
+  DHTMessageFactory* factory_;
 
-  std::shared_ptr<DHTRoutingTable> routingTable_;
+  DHTRoutingTable* routingTable_;
 
   std::unique_ptr<DHTUnknownMessage>
   handleUnknownMessage(const unsigned char* data, size_t length,
@@ -85,9 +85,9 @@ public:
 
   void setConnection(const std::shared_ptr<DHTConnection>& connection);
 
-  void setMessageFactory(const std::shared_ptr<DHTMessageFactory>& factory);
+  void setMessageFactory(DHTMessageFactory* factory);
 
-  void setRoutingTable(const std::shared_ptr<DHTRoutingTable>& routingTable);
+  void setRoutingTable(DHTRoutingTable* routingTable);
 };
 
 } // namespace aria2

+ 3 - 3
src/DHTMessageTracker.cc

@@ -52,7 +52,8 @@
 namespace aria2 {
 
 DHTMessageTracker::DHTMessageTracker()
-  : factory_{nullptr}
+  : routingTable_{nullptr},
+    factory_{nullptr}
 {}
 
 void DHTMessageTracker::addMessage
@@ -173,8 +174,7 @@ size_t DHTMessageTracker::countEntry() const
   return entries_.size();
 }
 
-void DHTMessageTracker::setRoutingTable
-(const std::shared_ptr<DHTRoutingTable>& routingTable)
+void DHTMessageTracker::setRoutingTable(DHTRoutingTable* routingTable)
 {
   routingTable_ = routingTable;
 }

+ 2 - 2
src/DHTMessageTracker.h

@@ -57,7 +57,7 @@ class DHTMessageTracker {
 private:
   std::deque<std::unique_ptr<DHTMessageTrackerEntry>> entries_;
 
-  std::shared_ptr<DHTRoutingTable> routingTable_;
+  DHTRoutingTable* routingTable_;
 
   DHTMessageFactory* factory_;
 public:
@@ -83,7 +83,7 @@ public:
 
   size_t countEntry() const;
 
-  void setRoutingTable(const std::shared_ptr<DHTRoutingTable>& routingTable);
+  void setRoutingTable(DHTRoutingTable* routingTable);
 
   void setMessageFactory(DHTMessageFactory* factory);
 };

+ 3 - 2
src/DHTPeerAnnounceCommand.cc

@@ -45,7 +45,8 @@ namespace aria2 {
 
 DHTPeerAnnounceCommand::DHTPeerAnnounceCommand
 (cuid_t cuid, DownloadEngine* e, time_t interval)
-  : TimeBasedCommand(cuid, e, interval)
+  : TimeBasedCommand{cuid, e, interval},
+    peerAnnounceStorage_{nullptr}
 {}
 
 DHTPeerAnnounceCommand::~DHTPeerAnnounceCommand() {}
@@ -68,7 +69,7 @@ void DHTPeerAnnounceCommand::process()
 }
 
 void DHTPeerAnnounceCommand::setPeerAnnounceStorage
-(const std::shared_ptr<DHTPeerAnnounceStorage>& storage)
+(DHTPeerAnnounceStorage* storage)
 {
   peerAnnounceStorage_ = storage;
 }

+ 2 - 2
src/DHTPeerAnnounceCommand.h

@@ -45,7 +45,7 @@ class DHTPeerAnnounceStorage;
 
 class DHTPeerAnnounceCommand:public TimeBasedCommand {
 private:
-  std::shared_ptr<DHTPeerAnnounceStorage> peerAnnounceStorage_;
+  DHTPeerAnnounceStorage* peerAnnounceStorage_;
 public:
   DHTPeerAnnounceCommand(cuid_t cuid, DownloadEngine* e, time_t interval);
 
@@ -55,7 +55,7 @@ public:
 
   virtual void process();
 
-  void setPeerAnnounceStorage(const std::shared_ptr<DHTPeerAnnounceStorage>& storage);
+  void setPeerAnnounceStorage(DHTPeerAnnounceStorage* storage);
 };
 
 } // namespace aria2

+ 6 - 5
src/DHTPeerAnnounceStorage.cc

@@ -52,9 +52,10 @@
 
 namespace aria2 {
 
-DHTPeerAnnounceStorage::DHTPeerAnnounceStorage() {}
-
-DHTPeerAnnounceStorage::~DHTPeerAnnounceStorage() {}
+DHTPeerAnnounceStorage::DHTPeerAnnounceStorage()
+  : taskQueue_{nullptr},
+    taskFactory_{nullptr}
+{}
 
 bool DHTPeerAnnounceStorage::InfoHashLess::operator()
   (const std::shared_ptr<DHTPeerAnnounceEntry>& lhs,
@@ -153,12 +154,12 @@ void DHTPeerAnnounceStorage::announcePeer()
   }
 }
 
-void DHTPeerAnnounceStorage::setTaskQueue(const std::shared_ptr<DHTTaskQueue>& taskQueue)
+void DHTPeerAnnounceStorage::setTaskQueue(DHTTaskQueue* taskQueue)
 {
   taskQueue_ = taskQueue;
 }
 
-void DHTPeerAnnounceStorage::setTaskFactory(const std::shared_ptr<DHTTaskFactory>& taskFactory)
+void DHTPeerAnnounceStorage::setTaskFactory(DHTTaskFactory* taskFactory)
 {
   taskFactory_ = taskFactory;
 }

+ 4 - 6
src/DHTPeerAnnounceStorage.h

@@ -62,14 +62,12 @@ private:
 
   std::shared_ptr<DHTPeerAnnounceEntry> getPeerAnnounceEntry(const unsigned char* infoHash);
 
-  std::shared_ptr<DHTTaskQueue> taskQueue_;
+  DHTTaskQueue* taskQueue_;
 
-  std::shared_ptr<DHTTaskFactory> taskFactory_;
+  DHTTaskFactory* taskFactory_;
 public:
   DHTPeerAnnounceStorage();
 
-  ~DHTPeerAnnounceStorage();
-
   void addPeerAnnounce(const unsigned char* infoHash,
                        const std::string& ipaddr, uint16_t port);
 
@@ -87,9 +85,9 @@ public:
   // are excluded from announce.
   void announcePeer();
 
-  void setTaskQueue(const std::shared_ptr<DHTTaskQueue>& taskQueue);
+  void setTaskQueue(DHTTaskQueue* taskQueue);
 
-  void setTaskFactory(const std::shared_ptr<DHTTaskFactory>& taskFactory);
+  void setTaskFactory(DHTTaskFactory* taskFactory);
 };
 
 } // namespace aria2

+ 8 - 8
src/DHTRegistry.h

@@ -58,21 +58,21 @@ private:
 
     std::shared_ptr<DHTNode> localNode;
 
-    std::shared_ptr<DHTRoutingTable> routingTable;
+    std::unique_ptr<DHTRoutingTable> routingTable;
 
-    std::shared_ptr<DHTTaskQueue> taskQueue;
+    std::unique_ptr<DHTTaskQueue> taskQueue;
 
-    std::shared_ptr<DHTTaskFactory> taskFactory;
+    std::unique_ptr<DHTTaskFactory> taskFactory;
 
-    std::shared_ptr<DHTPeerAnnounceStorage> peerAnnounceStorage;
+    std::unique_ptr<DHTPeerAnnounceStorage> peerAnnounceStorage;
 
-    std::shared_ptr<DHTTokenTracker> tokenTracker;
+    std::unique_ptr<DHTTokenTracker> tokenTracker;
 
-    std::shared_ptr<DHTMessageDispatcher> messageDispatcher;
+    std::unique_ptr<DHTMessageDispatcher> messageDispatcher;
 
-    std::shared_ptr<DHTMessageReceiver> messageReceiver;
+    std::unique_ptr<DHTMessageReceiver> messageReceiver;
 
-    std::shared_ptr<DHTMessageFactory> messageFactory;
+    std::unique_ptr<DHTMessageFactory> messageFactory;
 
     Data():initialized(false) {}
   };

+ 5 - 3
src/DHTRoutingTable.cc

@@ -53,7 +53,9 @@ DHTRoutingTable::DHTRoutingTable(const std::shared_ptr<DHTNode>& localNode)
   : localNode_(localNode),
     root_(new DHTBucketTreeNode
           (std::shared_ptr<DHTBucket>(new DHTBucket(localNode_)))),
-    numBucket_(1)
+    numBucket_(1),
+    taskQueue_{nullptr},
+    taskFactory_{nullptr}
 {}
 
 DHTRoutingTable::~DHTRoutingTable()
@@ -164,12 +166,12 @@ void DHTRoutingTable::getBuckets
   dht::enumerateBucket(buckets, root_);
 }
 
-void DHTRoutingTable::setTaskQueue(const std::shared_ptr<DHTTaskQueue>& taskQueue)
+void DHTRoutingTable::setTaskQueue(DHTTaskQueue* taskQueue)
 {
   taskQueue_ = taskQueue;
 }
 
-void DHTRoutingTable::setTaskFactory(const std::shared_ptr<DHTTaskFactory>& taskFactory)
+void DHTRoutingTable::setTaskFactory(DHTTaskFactory* taskFactory)
 {
   taskFactory_ = taskFactory;
 }

+ 4 - 4
src/DHTRoutingTable.h

@@ -57,9 +57,9 @@ private:
 
   int numBucket_;
 
-  std::shared_ptr<DHTTaskQueue> taskQueue_;
+  DHTTaskQueue* taskQueue_;
 
-  std::shared_ptr<DHTTaskFactory> taskFactory_;
+  DHTTaskFactory* taskFactory_;
 
   bool addNode(const std::shared_ptr<DHTNode>& node, bool good);
 public:
@@ -92,9 +92,9 @@ public:
 
   void getBuckets(std::vector<std::shared_ptr<DHTBucket> >& buckets) const;
 
-  void setTaskQueue(const std::shared_ptr<DHTTaskQueue>& taskQueue);
+  void setTaskQueue(DHTTaskQueue* taskQueue);
 
-  void setTaskFactory(const std::shared_ptr<DHTTaskFactory>& taskFactory);
+  void setTaskFactory(DHTTaskFactory* taskFactory);
 };
 
 } // namespace aria2

+ 63 - 66
src/DHTSetup.cc

@@ -111,7 +111,7 @@ std::vector<std::unique_ptr<Command>> DHTSetup::setup
     }
 
     uint16_t port;
-    std::shared_ptr<DHTConnectionImpl> connection(new DHTConnectionImpl(family));
+    auto connection = std::make_shared<DHTConnectionImpl>(family);
     {
       port = e->getBtRegistry()->getUdpPort();
       const std::string& addr =
@@ -137,27 +137,28 @@ std::vector<std::unique_ptr<Command>> DHTSetup::setup
     }
     A2_LOG_DEBUG(fmt("Initialized local node ID=%s",
                      util::toHex(localNode->getID(), DHT_ID_LENGTH).c_str()));
-    std::shared_ptr<DHTRoutingTable> routingTable(new DHTRoutingTable(localNode));
-
-    auto factory = std::make_shared<DHTMessageFactoryImpl>(family);
     auto tracker = std::make_shared<DHTMessageTracker>();
-    auto dispatcher = std::make_shared<DHTMessageDispatcherImpl>(tracker);
-    auto receiver = std::make_shared<DHTMessageReceiver>(tracker);
-    auto taskQueue = std::make_shared<DHTTaskQueueImpl>();
-    auto taskFactory = std::make_shared<DHTTaskFactoryImpl>();
-    auto peerAnnounceStorage = std::make_shared<DHTPeerAnnounceStorage>();
-    auto tokenTracker = std::make_shared<DHTTokenTracker>();
+    auto routingTable = make_unique<DHTRoutingTable>(localNode);
+    auto factory = make_unique<DHTMessageFactoryImpl>(family);
+    auto dispatcher = make_unique<DHTMessageDispatcherImpl>(tracker);
+    auto receiver = make_unique<DHTMessageReceiver>(tracker);
+    auto taskQueue = make_unique<DHTTaskQueueImpl>();
+    auto taskFactory = make_unique<DHTTaskFactoryImpl>();
+    auto peerAnnounceStorage = make_unique<DHTPeerAnnounceStorage>();
+    auto tokenTracker = make_unique<DHTTokenTracker>();
+    // For now, UDPTrackerClient was enabled along with DHT
+    auto udpTrackerClient = std::make_shared<UDPTrackerClient>();
     const time_t messageTimeout =
       e->getOption()->getAsInt(PREF_DHT_MESSAGE_TIMEOUT);
     // wiring up
-    tracker->setRoutingTable(routingTable);
+    tracker->setRoutingTable(routingTable.get());
     tracker->setMessageFactory(factory.get());
 
     dispatcher->setTimeout(messageTimeout);
 
     receiver->setConnection(connection);
-    receiver->setMessageFactory(factory);
-    receiver->setRoutingTable(routingTable);
+    receiver->setMessageFactory(factory.get());
+    receiver->setRoutingTable(routingTable.get());
 
     taskFactory->setLocalNode(localNode);
     taskFactory->setRoutingTable(routingTable.get());
@@ -166,11 +167,11 @@ std::vector<std::unique_ptr<Command>> DHTSetup::setup
     taskFactory->setTaskQueue(taskQueue.get());
     taskFactory->setTimeout(messageTimeout);
 
-    routingTable->setTaskQueue(taskQueue);
-    routingTable->setTaskFactory(taskFactory);
+    routingTable->setTaskQueue(taskQueue.get());
+    routingTable->setTaskFactory(taskFactory.get());
 
-    peerAnnounceStorage->setTaskQueue(taskQueue);
-    peerAnnounceStorage->setTaskFactory(taskFactory);
+    peerAnnounceStorage->setTaskQueue(taskQueue.get());
+    peerAnnounceStorage->setTaskFactory(taskFactory.get());
 
     factory->setRoutingTable(routingTable.get());
     factory->setConnection(connection.get());
@@ -179,43 +180,6 @@ std::vector<std::unique_ptr<Command>> DHTSetup::setup
     factory->setTokenTracker(tokenTracker.get());
     factory->setLocalNode(localNode);
 
-    // For now, UDPTrackerClient was enabled along with DHT
-    auto udpTrackerClient = std::make_shared<UDPTrackerClient>();
-    // assign them into DHTRegistry
-    if(family == AF_INET) {
-      DHTRegistry::getMutableData().localNode = localNode;
-      DHTRegistry::getMutableData().routingTable = routingTable;
-      DHTRegistry::getMutableData().taskQueue = taskQueue;
-      DHTRegistry::getMutableData().taskFactory = taskFactory;
-      DHTRegistry::getMutableData().peerAnnounceStorage = peerAnnounceStorage;
-      DHTRegistry::getMutableData().tokenTracker = tokenTracker;
-      DHTRegistry::getMutableData().messageDispatcher = dispatcher;
-      DHTRegistry::getMutableData().messageReceiver = receiver;
-      DHTRegistry::getMutableData().messageFactory = factory;
-      e->getBtRegistry()->setUDPTrackerClient(udpTrackerClient);
-    } else {
-      DHTRegistry::getMutableData6().localNode = localNode;
-      DHTRegistry::getMutableData6().routingTable = routingTable;
-      DHTRegistry::getMutableData6().taskQueue = taskQueue;
-      DHTRegistry::getMutableData6().taskFactory = taskFactory;
-      DHTRegistry::getMutableData6().peerAnnounceStorage = peerAnnounceStorage;
-      DHTRegistry::getMutableData6().tokenTracker = tokenTracker;
-      DHTRegistry::getMutableData6().messageDispatcher = dispatcher;
-      DHTRegistry::getMutableData6().messageReceiver = receiver;
-      DHTRegistry::getMutableData6().messageFactory = factory;
-    }
-    // add deserialized nodes to routing table
-    auto& desnodes = deserializer.getNodes();
-    for(auto& node : desnodes) {
-      routingTable->addNode(node);
-    }
-    if(!desnodes.empty()) {
-      auto task = std::static_pointer_cast<DHTBucketRefreshTask>
-        (taskFactory->createBucketRefreshTask());
-      task->setForceRefresh(true);
-      taskQueue->addPeriodicTask1(task);
-    }
-
     const Pref* prefEntryPointHost =
       family == AF_INET?PREF_DHT_ENTRY_POINT_HOST:PREF_DHT_ENTRY_POINT_HOST6;
     if(!e->getOption()->get(prefEntryPointHost).empty()) {
@@ -231,9 +195,9 @@ std::vector<std::unique_ptr<Command>> DHTSetup::setup
         auto command = make_unique<DHTEntryPointNameResolveCommand>
           (e->newCUID(), e, entryPoints);
         command->setBootstrapEnabled(true);
-        command->setTaskQueue(taskQueue);
-        command->setTaskFactory(taskFactory);
-        command->setRoutingTable(routingTable);
+        command->setTaskQueue(taskQueue.get());
+        command->setTaskFactory(taskFactory.get());
+        command->setRoutingTable(routingTable.get());
         command->setLocalNode(localNode);
         tempCommands.push_back(std::move(command));
       }
@@ -242,9 +206,9 @@ std::vector<std::unique_ptr<Command>> DHTSetup::setup
     }
     {
       auto command = make_unique<DHTInteractionCommand>(e->newCUID(), e);
-      command->setMessageDispatcher(dispatcher);
-      command->setMessageReceiver(receiver);
-      command->setTaskQueue(taskQueue);
+      command->setMessageDispatcher(dispatcher.get());
+      command->setMessageReceiver(receiver.get());
+      command->setTaskQueue(taskQueue.get());
       command->setReadCheckSocket(connection->getSocket());
       command->setConnection(connection);
       command->setUDPTrackerClient(udpTrackerClient);
@@ -253,33 +217,66 @@ std::vector<std::unique_ptr<Command>> DHTSetup::setup
     {
       auto command = make_unique<DHTTokenUpdateCommand>
         (e->newCUID(), e, DHT_TOKEN_UPDATE_INTERVAL);
-      command->setTokenTracker(tokenTracker);
+      command->setTokenTracker(tokenTracker.get());
       tempCommands.push_back(std::move(command));
     }
     {
       auto command = make_unique<DHTBucketRefreshCommand>
         (e->newCUID(), e, DHT_BUCKET_REFRESH_CHECK_INTERVAL);
-      command->setTaskQueue(taskQueue);
-      command->setRoutingTable(routingTable);
-      command->setTaskFactory(taskFactory);
+      command->setTaskQueue(taskQueue.get());
+      command->setRoutingTable(routingTable.get());
+      command->setTaskFactory(taskFactory.get());
       tempCommands.push_back(std::move(command));
     }
     {
       auto command = make_unique<DHTPeerAnnounceCommand>
         (e->newCUID(), e, DHT_PEER_ANNOUNCE_CHECK_INTERVAL);
-      command->setPeerAnnounceStorage(peerAnnounceStorage);
+      command->setPeerAnnounceStorage(peerAnnounceStorage.get());
       tempCommands.push_back(std::move(command));
     }
     {
       auto command = make_unique<DHTAutoSaveCommand>
         (e->newCUID(), e, family, 30*60);
       command->setLocalNode(localNode);
-      command->setRoutingTable(routingTable);
+      command->setRoutingTable(routingTable.get());
       tempCommands.push_back(std::move(command));
     }
+    // add deserialized nodes to routing table
+    auto& desnodes = deserializer.getNodes();
+    for(auto& node : desnodes) {
+      routingTable->addNode(node);
+    }
+    if(!desnodes.empty()) {
+      auto task = std::static_pointer_cast<DHTBucketRefreshTask>
+        (taskFactory->createBucketRefreshTask());
+      task->setForceRefresh(true);
+      taskQueue->addPeriodicTask1(task);
+    }
+    // assign them into DHTRegistry
     if(family == AF_INET) {
+      DHTRegistry::getMutableData().localNode = localNode;
+      DHTRegistry::getMutableData().routingTable = std::move(routingTable);
+      DHTRegistry::getMutableData().taskQueue = std::move(taskQueue);
+      DHTRegistry::getMutableData().taskFactory = std::move(taskFactory);
+      DHTRegistry::getMutableData().peerAnnounceStorage =
+        std::move(peerAnnounceStorage);
+      DHTRegistry::getMutableData().tokenTracker = std::move(tokenTracker);
+      DHTRegistry::getMutableData().messageDispatcher = std::move(dispatcher);
+      DHTRegistry::getMutableData().messageReceiver = std::move(receiver);
+      DHTRegistry::getMutableData().messageFactory = std::move(factory);
+      e->getBtRegistry()->setUDPTrackerClient(udpTrackerClient);
       DHTRegistry::setInitialized(true);
     } else {
+      DHTRegistry::getMutableData6().localNode = localNode;
+      DHTRegistry::getMutableData6().routingTable = std::move(routingTable);
+      DHTRegistry::getMutableData6().taskQueue = std::move(taskQueue);
+      DHTRegistry::getMutableData6().taskFactory = std::move(taskFactory);
+      DHTRegistry::getMutableData6().peerAnnounceStorage =
+        std::move(peerAnnounceStorage);
+      DHTRegistry::getMutableData6().tokenTracker = std::move(tokenTracker);
+      DHTRegistry::getMutableData6().messageDispatcher = std::move(dispatcher);
+      DHTRegistry::getMutableData6().messageReceiver = std::move(receiver);
+      DHTRegistry::getMutableData6().messageFactory = std::move(factory);
       DHTRegistry::setInitialized6(true);
     }
     if(e->getBtRegistry()->getUdpPort() == 0) {

+ 3 - 2
src/DHTTokenUpdateCommand.cc

@@ -45,7 +45,8 @@ namespace aria2 {
 
 DHTTokenUpdateCommand::DHTTokenUpdateCommand
 (cuid_t cuid, DownloadEngine* e, time_t interval)
-  : TimeBasedCommand(cuid, e, interval)
+  : TimeBasedCommand{cuid, e, interval},
+    tokenTracker_{nullptr}
 {}
 
 DHTTokenUpdateCommand::~DHTTokenUpdateCommand() {}
@@ -67,7 +68,7 @@ void DHTTokenUpdateCommand::process()
   }
 }
 
-void DHTTokenUpdateCommand::setTokenTracker(const std::shared_ptr<DHTTokenTracker>& tokenTracker)
+void DHTTokenUpdateCommand::setTokenTracker(DHTTokenTracker* tokenTracker)
 {
   tokenTracker_ = tokenTracker;
 }

+ 2 - 2
src/DHTTokenUpdateCommand.h

@@ -45,7 +45,7 @@ class DHTTokenTracker;
 
 class DHTTokenUpdateCommand:public TimeBasedCommand {
 private:
-  std::shared_ptr<DHTTokenTracker> tokenTracker_;
+  DHTTokenTracker* tokenTracker_;
 public:
   DHTTokenUpdateCommand(cuid_t cuid, DownloadEngine* e, time_t interval);
 
@@ -55,7 +55,7 @@ public:
 
   virtual void process();
 
-  void setTokenTracker(const std::shared_ptr<DHTTokenTracker>& tokenTracker);
+  void setTokenTracker(DHTTokenTracker* tokenTracker);
 };
 
 } // namespace aria2

+ 3 - 3
src/RequestGroup.cc

@@ -447,9 +447,9 @@ void RequestGroup::createInitialCommand
         if(!nodes.empty() && DHTRegistry::isInitialized()) {
           auto command = make_unique<DHTEntryPointNameResolveCommand>
             (e->newCUID(), e, nodes);
-          command->setTaskQueue(DHTRegistry::getData().taskQueue);
-          command->setTaskFactory(DHTRegistry::getData().taskFactory);
-          command->setRoutingTable(DHTRegistry::getData().routingTable);
+          command->setTaskQueue(DHTRegistry::getData().taskQueue.get());
+          command->setTaskFactory(DHTRegistry::getData().taskFactory.get());
+          command->setRoutingTable(DHTRegistry::getData().routingTable.get());
           command->setLocalNode(DHTRegistry::getData().localNode);
           e->addCommand(std::move(command));
         }

+ 3 - 3
test/DHTMessageTrackerTest.cc

@@ -35,8 +35,8 @@ CPPUNIT_TEST_SUITE_REGISTRATION(DHTMessageTrackerTest);
 void DHTMessageTrackerTest::testMessageArrived()
 {
   auto localNode = std::make_shared<DHTNode>();
-  auto routingTable = std::make_shared<DHTRoutingTable>(localNode);
-  auto factory = std::make_shared<MockDHTMessageFactory>();
+  auto routingTable = make_unique<DHTRoutingTable>(localNode);
+  auto factory = make_unique<MockDHTMessageFactory>();
   factory->setLocalNode(localNode);
 
   auto r1 = std::make_shared<DHTNode>();
@@ -54,7 +54,7 @@ void DHTMessageTrackerTest::testMessageArrived()
   auto m3 = make_unique<MockDHTMessage>(localNode, r3);
 
   DHTMessageTracker tracker;
-  tracker.setRoutingTable(routingTable);
+  tracker.setRoutingTable(routingTable.get());
   tracker.setMessageFactory(factory.get());
   tracker.addMessage(m1.get(), DHT_MESSAGE_TIMEOUT);
   tracker.addMessage(m2.get(), DHT_MESSAGE_TIMEOUT);

+ 19 - 19
test/DHTRoutingTableTest.cc

@@ -35,16 +35,15 @@ CPPUNIT_TEST_SUITE_REGISTRATION(DHTRoutingTableTest);
 
 void DHTRoutingTableTest::testAddNode()
 {
-  std::shared_ptr<DHTNode> localNode(new DHTNode());
+  auto localNode = std::make_shared<DHTNode>();
   DHTRoutingTable table(localNode);
-  std::shared_ptr<MockDHTTaskFactory> taskFactory(new MockDHTTaskFactory());
-  table.setTaskFactory(taskFactory);
-  std::shared_ptr<MockDHTTaskQueue> taskQueue(new MockDHTTaskQueue());
-  table.setTaskQueue(taskQueue);
+  auto taskFactory = make_unique<MockDHTTaskFactory>();
+  table.setTaskFactory(taskFactory.get());
+  auto taskQueue = make_unique<MockDHTTaskQueue>();
+  table.setTaskQueue(taskQueue.get());
   uint32_t count = 0;
   for(int i = 0; i < 100; ++i) {
-    std::shared_ptr<DHTNode> node(new DHTNode());
-    if(table.addNode(node)) {
+    if(table.addNode(std::make_shared<DHTNode>())) {
       ++count;
     }
   }
@@ -53,14 +52,14 @@ void DHTRoutingTableTest::testAddNode()
 
 void DHTRoutingTableTest::testAddNode_localNode()
 {
-  std::shared_ptr<DHTNode> localNode(new DHTNode());
+  auto localNode = std::make_shared<DHTNode>();
   DHTRoutingTable table(localNode);
-  std::shared_ptr<MockDHTTaskFactory> taskFactory(new MockDHTTaskFactory());
-  table.setTaskFactory(taskFactory);
-  std::shared_ptr<MockDHTTaskQueue> taskQueue(new MockDHTTaskQueue());
-  table.setTaskQueue(taskQueue);
+  auto taskFactory = make_unique<MockDHTTaskFactory>();
+  table.setTaskFactory(taskFactory.get());
+  auto taskQueue = make_unique<MockDHTTaskQueue>();
+  table.setTaskQueue(taskQueue.get());
 
-  std::shared_ptr<DHTNode> newNode(new DHTNode(localNode->getID()));
+  auto newNode = std::make_shared<DHTNode>(localNode->getID());
   CPPUNIT_ASSERT(!table.addNode(newNode));
 }
 
@@ -77,7 +76,7 @@ void DHTRoutingTableTest::testGetClosestKNodes()
 {
   unsigned char id[DHT_ID_LENGTH];
   createID(id, 0x81, 0);
-  std::shared_ptr<DHTNode> localNode(new DHTNode(id));
+  auto localNode = std::make_shared<DHTNode>(id);
 
   DHTRoutingTable table(localNode);
 
@@ -86,26 +85,27 @@ void DHTRoutingTableTest::testGetClosestKNodes()
   std::shared_ptr<DHTNode> nodes3[8];
   for(size_t i = 0; i < DHTBucket::K; ++i) {
     createID(id, 0xf0, i);
-    nodes1[i].reset(new DHTNode(id));
+    nodes1[i] = std::make_shared<DHTNode>(id);
     CPPUNIT_ASSERT(table.addNode(nodes1[i]));
   }
   for(size_t i = 0; i < DHTBucket::K; ++i) {
     createID(id, 0x80, i);
-    nodes2[i].reset(new DHTNode(id));
+    nodes2[i] = std::make_shared<DHTNode>(id);
     CPPUNIT_ASSERT(table.addNode(nodes2[i]));
   }
   for(size_t i = 0; i < DHTBucket::K; ++i) {
     createID(id, 0x70, i);
-    nodes3[i].reset(new DHTNode(id));
+    nodes3[i] = std::make_shared<DHTNode>(id);
     CPPUNIT_ASSERT(table.addNode(nodes3[i]));
   }
   {
     createID(id, 0x80, 0x10);
-    std::vector<std::shared_ptr<DHTNode> > nodes;
+    std::vector<std::shared_ptr<DHTNode>> nodes;
     table.getClosestKNodes(nodes, id);
     CPPUNIT_ASSERT_EQUAL((size_t)8, nodes.size());
     for(size_t i = 0; i < nodes.size(); ++i) {
-      CPPUNIT_ASSERT(memcmp(nodes2[0]->getID(), nodes[0]->getID(), DHT_ID_LENGTH) == 0);
+      CPPUNIT_ASSERT(memcmp(nodes2[0]->getID(), nodes[0]->getID(),
+                            DHT_ID_LENGTH) == 0);
     }
   }
 }