DHTAbstractNodeLookupTask.h 7.2 KB

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