DHTSetup.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "DHTSetup.h"
  36. #include "LogFactory.h"
  37. #include "Logger.h"
  38. #include "Util.h"
  39. #include "File.h"
  40. #include "DHTNode.h"
  41. #include "DHTConnectionImpl.h"
  42. #include "DHTRoutingTable.h"
  43. #include "DHTMessageFactoryImpl.h"
  44. #include "DHTMessageTracker.h"
  45. #include "DHTMessageDispatcherImpl.h"
  46. #include "DHTMessageReceiver.h"
  47. #include "DHTTaskQueueImpl.h"
  48. #include "DHTTaskFactoryImpl.h"
  49. #include "DHTPeerAnnounceStorage.h"
  50. #include "DHTTokenTracker.h"
  51. #include "DHTInteractionCommand.h"
  52. #include "DHTTokenUpdateCommand.h"
  53. #include "DHTBucketRefreshCommand.h"
  54. #include "DHTPeerAnnounceCommand.h"
  55. #include "DHTEntryPointNameResolveCommand.h"
  56. #include "DHTAutoSaveCommand.h"
  57. #include "DHTTask.h"
  58. #include "DHTRoutingTableDeserializer.h"
  59. #include "DHTRegistry.h"
  60. #include "DHTBucketRefreshTask.h"
  61. #include "DHTMessageCallback.h"
  62. #include "CUIDCounter.h"
  63. #include "prefs.h"
  64. #include "Option.h"
  65. #include "SocketCore.h"
  66. #include "DlAbortEx.h"
  67. #include "RecoverableException.h"
  68. #include "a2functional.h"
  69. #include <fstream>
  70. #include <algorithm>
  71. namespace aria2 {
  72. size_t DHTSetup::_initialized = 0;
  73. DHTSetup::DHTSetup():_logger(LogFactory::getInstance()) {}
  74. DHTSetup::~DHTSetup() {}
  75. void DHTSetup::setup(std::deque<Command*>& commands,
  76. DownloadEngine* e, const Option* option)
  77. {
  78. if(_initialized) {
  79. return;
  80. }
  81. std::deque<Command*> tempCommands;
  82. try {
  83. // load routing table and localnode id here
  84. SharedHandle<DHTNode> localNode;
  85. DHTRoutingTableDeserializer deserializer;
  86. std::string dhtFile = option->get(PREF_DHT_FILE_PATH);
  87. if(File(dhtFile).isFile()) {
  88. try {
  89. std::ifstream in(dhtFile.c_str());
  90. in.exceptions(std::ios::failbit);
  91. deserializer.deserialize(in);
  92. localNode = deserializer.getLocalNode();
  93. } catch(RecoverableException& e) {
  94. _logger->error("Exception caught while loading DHT routing table from %s",
  95. e, dhtFile.c_str());
  96. }
  97. }
  98. if(localNode.isNull()) {
  99. localNode.reset(new DHTNode());
  100. }
  101. SharedHandle<DHTConnectionImpl> connection(new DHTConnectionImpl());
  102. {
  103. IntSequence seq = Util::parseIntRange(option->get(PREF_DHT_LISTEN_PORT));
  104. uint16_t port;
  105. if(!connection->bind(port, seq)) {
  106. throw DlAbortEx("Error occurred while binding port for DHT");
  107. }
  108. localNode->setPort(port);
  109. }
  110. _logger->debug("Initialized local node ID=%s",
  111. Util::toHex(localNode->getID(), DHT_ID_LENGTH).c_str());
  112. SharedHandle<DHTRoutingTable> routingTable(new DHTRoutingTable(localNode));
  113. SharedHandle<DHTMessageFactoryImpl> factory(new DHTMessageFactoryImpl());
  114. SharedHandle<DHTMessageTracker> tracker(new DHTMessageTracker());
  115. SharedHandle<DHTMessageDispatcherImpl> dispatcher(new DHTMessageDispatcherImpl(tracker));
  116. SharedHandle<DHTMessageReceiver> receiver(new DHTMessageReceiver(tracker));
  117. SharedHandle<DHTTaskQueue> taskQueue(new DHTTaskQueueImpl());
  118. SharedHandle<DHTTaskFactoryImpl> taskFactory(new DHTTaskFactoryImpl());
  119. SharedHandle<DHTPeerAnnounceStorage> peerAnnounceStorage(new DHTPeerAnnounceStorage());
  120. SharedHandle<DHTTokenTracker> tokenTracker(new DHTTokenTracker());
  121. // wiring up
  122. tracker->setRoutingTable(routingTable);
  123. tracker->setMessageFactory(factory);
  124. receiver->setConnection(connection);
  125. receiver->setMessageFactory(factory);
  126. receiver->setRoutingTable(routingTable);
  127. taskFactory->setLocalNode(localNode);
  128. taskFactory->setRoutingTable(routingTable);
  129. taskFactory->setMessageDispatcher(dispatcher);
  130. taskFactory->setMessageFactory(factory);
  131. taskFactory->setTaskQueue(taskQueue);
  132. routingTable->setTaskQueue(taskQueue);
  133. routingTable->setTaskFactory(taskFactory);
  134. peerAnnounceStorage->setTaskQueue(taskQueue);
  135. peerAnnounceStorage->setTaskFactory(taskFactory);
  136. factory->setRoutingTable(routingTable);
  137. factory->setConnection(connection);
  138. factory->setMessageDispatcher(dispatcher);
  139. factory->setPeerAnnounceStorage(peerAnnounceStorage);
  140. factory->setTokenTracker(tokenTracker);
  141. factory->setLocalNode(localNode);
  142. // assign them into DHTRegistry
  143. DHTRegistry::_localNode = localNode;
  144. DHTRegistry::_routingTable = routingTable;
  145. DHTRegistry::_taskQueue = taskQueue;
  146. DHTRegistry::_taskFactory = taskFactory;
  147. DHTRegistry::_peerAnnounceStorage = peerAnnounceStorage;
  148. DHTRegistry::_tokenTracker = tokenTracker;
  149. DHTRegistry::_messageDispatcher = dispatcher;
  150. DHTRegistry::_messageReceiver = receiver;
  151. DHTRegistry::_messageFactory = factory;
  152. // add deserialized nodes to routing table
  153. const std::deque<SharedHandle<DHTNode> >& desnodes = deserializer.getNodes();
  154. for(std::deque<SharedHandle<DHTNode> >::const_iterator i = desnodes.begin(); i != desnodes.end(); ++i) {
  155. routingTable->addNode(*i);
  156. }
  157. if(!desnodes.empty() && deserializer.getSerializedTime().elapsed(DHT_BUCKET_REFRESH_INTERVAL)) {
  158. SharedHandle<DHTBucketRefreshTask> task
  159. (dynamic_pointer_cast<DHTBucketRefreshTask>(taskFactory->createBucketRefreshTask()));
  160. task->setForceRefresh(true);
  161. taskQueue->addPeriodicTask1(task);
  162. }
  163. if(!option->get(PREF_DHT_ENTRY_POINT_HOST).empty()) {
  164. {
  165. std::pair<std::string, uint16_t> addr(option->get(PREF_DHT_ENTRY_POINT_HOST),
  166. option->getAsInt(PREF_DHT_ENTRY_POINT_PORT));
  167. std::deque<std::pair<std::string, uint16_t> > entryPoints;
  168. entryPoints.push_back(addr);
  169. DHTEntryPointNameResolveCommand* command = new DHTEntryPointNameResolveCommand(CUIDCounterSingletonHolder::instance()->newID(), e, entryPoints);
  170. command->setBootstrapEnabled(true);
  171. command->setTaskQueue(taskQueue);
  172. command->setTaskFactory(taskFactory);
  173. command->setRoutingTable(routingTable);
  174. command->setLocalNode(localNode);
  175. tempCommands.push_back(command);
  176. }
  177. } else {
  178. _logger->info("No DHT entry point specified.");
  179. }
  180. {
  181. DHTInteractionCommand* command = new DHTInteractionCommand(CUIDCounterSingletonHolder::instance()->newID(), e);
  182. command->setMessageDispatcher(dispatcher);
  183. command->setMessageReceiver(receiver);
  184. command->setTaskQueue(taskQueue);
  185. command->setReadCheckSocket(connection->getSocket());
  186. tempCommands.push_back(command);
  187. }
  188. {
  189. DHTTokenUpdateCommand* command = new DHTTokenUpdateCommand(CUIDCounterSingletonHolder::instance()->newID(), e, DHT_TOKEN_UPDATE_INTERVAL);
  190. command->setTokenTracker(tokenTracker);
  191. tempCommands.push_back(command);
  192. }
  193. {
  194. DHTBucketRefreshCommand* command = new DHTBucketRefreshCommand(CUIDCounterSingletonHolder::instance()->newID(), e, DHT_BUCKET_REFRESH_CHECK_INTERVAL);
  195. command->setTaskQueue(taskQueue);
  196. command->setRoutingTable(routingTable);
  197. command->setTaskFactory(taskFactory);
  198. tempCommands.push_back(command);
  199. }
  200. {
  201. DHTPeerAnnounceCommand* command = new DHTPeerAnnounceCommand(CUIDCounterSingletonHolder::instance()->newID(), e, DHT_PEER_ANNOUNCE_CHECK_INTERVAL);
  202. command->setPeerAnnounceStorage(peerAnnounceStorage);
  203. tempCommands.push_back(command);
  204. }
  205. {
  206. DHTAutoSaveCommand* command = new DHTAutoSaveCommand(CUIDCounterSingletonHolder::instance()->newID(), e, 30*60);
  207. command->setLocalNode(localNode);
  208. command->setRoutingTable(routingTable);
  209. tempCommands.push_back(command);
  210. }
  211. _initialized = true;
  212. commands.insert(commands.end(), tempCommands.begin(), tempCommands.end());
  213. } catch(RecoverableException& e) {
  214. _logger->error("Exception caught while initializing DHT functionality. DHT is disabled.", e);
  215. DHTRegistry::clear();
  216. std::for_each(tempCommands.begin(), tempCommands.end(), Deleter());
  217. }
  218. }
  219. bool DHTSetup::initialized()
  220. {
  221. return _initialized;
  222. }
  223. } // namespace aria2