DHTAbstractNodeLookupTask.h 7.4 KB

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