DHTAbstractNodeLookupTask.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #ifndef D_DHT_ABSTRACT_NODE_LOOKUP_TASK_H
  36. #define D_DHT_ABSTRACT_NODE_LOOKUP_TASK_H
  37. #include "DHTAbstractTask.h"
  38. #include <cstring>
  39. #include <algorithm>
  40. #include <deque>
  41. #include <vector>
  42. #include "DHTConstants.h"
  43. #include "DHTNodeLookupEntry.h"
  44. #include "DHTRoutingTable.h"
  45. #include "DHTMessageDispatcher.h"
  46. #include "DHTMessageFactory.h"
  47. #include "DHTMessage.h"
  48. #include "DHTNode.h"
  49. #include "DHTBucket.h"
  50. #include "LogFactory.h"
  51. #include "Logger.h"
  52. #include "util.h"
  53. #include "DHTIDCloser.h"
  54. #include "a2functional.h"
  55. #include "fmt.h"
  56. namespace aria2 {
  57. class DHTNode;
  58. class DHTMessage;
  59. template<class ResponseMessage>
  60. class DHTAbstractNodeLookupTask:public DHTAbstractTask {
  61. private:
  62. unsigned char targetID_[DHT_ID_LENGTH];
  63. std::deque<SharedHandle<DHTNodeLookupEntry> > entries_;
  64. size_t inFlightMessage_;
  65. template<typename Container>
  66. void toEntries
  67. (Container& entries, const std::vector<SharedHandle<DHTNode> >& nodes) const
  68. {
  69. for(std::vector<SharedHandle<DHTNode> >::const_iterator i = nodes.begin(),
  70. eoi = nodes.end(); i != eoi; ++i) {
  71. SharedHandle<DHTNodeLookupEntry> e(new DHTNodeLookupEntry(*i));
  72. entries.push_back(e);
  73. }
  74. }
  75. void sendMessage()
  76. {
  77. for(std::deque<SharedHandle<DHTNodeLookupEntry> >::iterator i =
  78. entries_.begin(), eoi = entries_.end();
  79. i != eoi && inFlightMessage_ < ALPHA; ++i) {
  80. if((*i)->used == false) {
  81. ++inFlightMessage_;
  82. (*i)->used = true;
  83. SharedHandle<DHTMessage> m = createMessage((*i)->node);
  84. SharedHandle<DHTMessageCallback> callback(createCallback());
  85. getMessageDispatcher()->addMessageToQueue(m, callback);
  86. }
  87. }
  88. }
  89. void sendMessageAndCheckFinish()
  90. {
  91. if(needsAdditionalOutgoingMessage()) {
  92. sendMessage();
  93. }
  94. if(inFlightMessage_ == 0) {
  95. A2_LOG_DEBUG(fmt("Finished node_lookup for node ID %s",
  96. util::toHex(targetID_, DHT_ID_LENGTH).c_str()));
  97. onFinish();
  98. updateBucket();
  99. setFinished(true);
  100. } else {
  101. A2_LOG_DEBUG(fmt("%lu in flight message for node ID %s",
  102. static_cast<unsigned long>(inFlightMessage_),
  103. util::toHex(targetID_, DHT_ID_LENGTH).c_str()));
  104. }
  105. }
  106. void updateBucket() {}
  107. protected:
  108. const unsigned char* getTargetID() const
  109. {
  110. return targetID_;
  111. }
  112. const std::deque<SharedHandle<DHTNodeLookupEntry> >& getEntries() const
  113. {
  114. return entries_;
  115. }
  116. virtual void getNodesFromMessage
  117. (std::vector<SharedHandle<DHTNode> >& nodes,
  118. const ResponseMessage* message) = 0;
  119. virtual void onReceivedInternal
  120. (const ResponseMessage* message) {}
  121. virtual bool needsAdditionalOutgoingMessage() { return true; }
  122. virtual void onFinish() {}
  123. virtual SharedHandle<DHTMessage> createMessage
  124. (const SharedHandle<DHTNode>& remoteNode) = 0;
  125. virtual SharedHandle<DHTMessageCallback> createCallback() = 0;
  126. public:
  127. DHTAbstractNodeLookupTask(const unsigned char* targetID):
  128. inFlightMessage_(0)
  129. {
  130. memcpy(targetID_, targetID, DHT_ID_LENGTH);
  131. }
  132. static const size_t ALPHA = 3;
  133. virtual void startup()
  134. {
  135. std::vector<SharedHandle<DHTNode> > nodes;
  136. getRoutingTable()->getClosestKNodes(nodes, targetID_);
  137. entries_.clear();
  138. toEntries(entries_, nodes);
  139. if(entries_.empty()) {
  140. setFinished(true);
  141. } else {
  142. // TODO use RTT here
  143. inFlightMessage_ = 0;
  144. sendMessage();
  145. if(inFlightMessage_ == 0) {
  146. A2_LOG_DEBUG("No message was sent in this lookup stage. Finished.");
  147. setFinished(true);
  148. }
  149. }
  150. }
  151. void onReceived(const ResponseMessage* message)
  152. {
  153. --inFlightMessage_;
  154. // Replace old Node ID with new Node ID.
  155. for(std::deque<SharedHandle<DHTNodeLookupEntry> >::iterator i =
  156. entries_.begin(), eoi = entries_.end(); i != eoi; ++i) {
  157. if((*i)->node->getIPAddress() == message->getRemoteNode()->getIPAddress()
  158. && (*i)->node->getPort() == message->getRemoteNode()->getPort()) {
  159. (*i)->node = message->getRemoteNode();
  160. }
  161. }
  162. onReceivedInternal(message);
  163. std::vector<SharedHandle<DHTNode> > nodes;
  164. getNodesFromMessage(nodes, message);
  165. std::vector<SharedHandle<DHTNodeLookupEntry> > newEntries;
  166. toEntries(newEntries, nodes);
  167. size_t count = 0;
  168. for(std::vector<SharedHandle<DHTNodeLookupEntry> >::const_iterator i =
  169. newEntries.begin(), eoi = newEntries.end(); i != eoi; ++i) {
  170. if(memcmp(getLocalNode()->getID(), (*i)->node->getID(),
  171. DHT_ID_LENGTH) != 0) {
  172. entries_.push_front(*i);
  173. ++count;
  174. A2_LOG_DEBUG(fmt("Received nodes: id=%s, ip=%s",
  175. util::toHex((*i)->node->getID(),
  176. DHT_ID_LENGTH).c_str(),
  177. (*i)->node->getIPAddress().c_str()));
  178. }
  179. }
  180. A2_LOG_DEBUG(fmt("%lu node lookup entries added.",
  181. static_cast<unsigned long>(count)));
  182. std::stable_sort(entries_.begin(), entries_.end(), DHTIDCloser(targetID_));
  183. entries_.erase
  184. (std::unique(entries_.begin(), entries_.end(),
  185. DerefEqualTo<SharedHandle<DHTNodeLookupEntry> >()),
  186. entries_.end());
  187. A2_LOG_DEBUG(fmt("%lu node lookup entries are unique.",
  188. static_cast<unsigned long>(entries_.size())));
  189. if(entries_.size() > DHTBucket::K) {
  190. entries_.erase(entries_.begin()+DHTBucket::K, entries_.end());
  191. }
  192. sendMessageAndCheckFinish();
  193. }
  194. void onTimeout(const SharedHandle<DHTNode>& node)
  195. {
  196. A2_LOG_DEBUG(fmt("node lookup message timeout for node ID=%s",
  197. util::toHex(node->getID(), DHT_ID_LENGTH).c_str()));
  198. --inFlightMessage_;
  199. for(std::deque<SharedHandle<DHTNodeLookupEntry> >::iterator i =
  200. entries_.begin(), eoi = entries_.end(); i != eoi; ++i) {
  201. if(*(*i)->node == *node) {
  202. entries_.erase(i);
  203. break;
  204. }
  205. }
  206. sendMessageAndCheckFinish();
  207. }
  208. };
  209. } // namespace aria2
  210. #endif // D_DHT_ABSTRACT_NODE_LOOKUP_TASK_H