DHTPeerAnnounceStorage.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "DHTPeerAnnounceStorage.h"
  36. #include <cstring>
  37. #include <algorithm>
  38. #include "DHTPeerAnnounceEntry.h"
  39. #include "Peer.h"
  40. #include "DHTConstants.h"
  41. #include "DHTTaskQueue.h"
  42. #include "DHTTaskFactory.h"
  43. #include "DHTTask.h"
  44. #include "LogFactory.h"
  45. #include "Logger.h"
  46. #include "util.h"
  47. #include "a2functional.h"
  48. #include "wallclock.h"
  49. namespace aria2 {
  50. DHTPeerAnnounceStorage::DHTPeerAnnounceStorage():
  51. logger_(LogFactory::getInstance()) {}
  52. DHTPeerAnnounceStorage::~DHTPeerAnnounceStorage() {}
  53. namespace {
  54. class InfoHashLess
  55. {
  56. public:
  57. bool operator()(const SharedHandle<DHTPeerAnnounceEntry>& lhs,
  58. const SharedHandle<DHTPeerAnnounceEntry>& rhs)
  59. {
  60. return memcmp(lhs->getInfoHash(), rhs->getInfoHash(), DHT_ID_LENGTH) < 0;
  61. }
  62. };
  63. }
  64. SharedHandle<DHTPeerAnnounceEntry>
  65. DHTPeerAnnounceStorage::getPeerAnnounceEntry(const unsigned char* infoHash)
  66. {
  67. SharedHandle<DHTPeerAnnounceEntry> entry(new DHTPeerAnnounceEntry(infoHash));
  68. std::deque<SharedHandle<DHTPeerAnnounceEntry> >::iterator i =
  69. std::lower_bound(entries_.begin(), entries_.end(), entry, InfoHashLess());
  70. if(i != entries_.end() &&
  71. memcmp(infoHash, (*i)->getInfoHash(), DHT_ID_LENGTH) == 0) {
  72. entry = *i;
  73. } else {
  74. entries_.insert(i, entry);
  75. }
  76. return entry;
  77. }
  78. void
  79. DHTPeerAnnounceStorage::addPeerAnnounce(const unsigned char* infoHash,
  80. const std::string& ipaddr, uint16_t port)
  81. {
  82. if(logger_->debug()) {
  83. logger_->debug("Adding %s:%u to peer announce list: infoHash=%s",
  84. ipaddr.c_str(), port,
  85. util::toHex(infoHash, DHT_ID_LENGTH).c_str());
  86. }
  87. getPeerAnnounceEntry(infoHash)->addPeerAddrEntry(PeerAddrEntry(ipaddr, port));
  88. }
  89. bool DHTPeerAnnounceStorage::contains(const unsigned char* infoHash) const
  90. {
  91. SharedHandle<DHTPeerAnnounceEntry> entry(new DHTPeerAnnounceEntry(infoHash));
  92. return
  93. std::binary_search(entries_.begin(), entries_.end(), entry, InfoHashLess());
  94. }
  95. void DHTPeerAnnounceStorage::getPeers(std::vector<SharedHandle<Peer> >& peers,
  96. const unsigned char* infoHash)
  97. {
  98. SharedHandle<DHTPeerAnnounceEntry> entry(new DHTPeerAnnounceEntry(infoHash));
  99. std::deque<SharedHandle<DHTPeerAnnounceEntry> >::iterator i =
  100. std::lower_bound(entries_.begin(), entries_.end(), entry, InfoHashLess());
  101. if(i != entries_.end() &&
  102. memcmp(infoHash, (*i)->getInfoHash(), DHT_ID_LENGTH) == 0 &&
  103. !(*i)->empty()) {
  104. (*i)->getPeers(peers);
  105. }
  106. }
  107. namespace {
  108. class RemoveStalePeerAddrEntry
  109. {
  110. public:
  111. void operator()(const SharedHandle<DHTPeerAnnounceEntry>& e)
  112. {
  113. e->removeStalePeerAddrEntry(DHT_PEER_ANNOUNCE_PURGE_INTERVAL);
  114. }
  115. };
  116. }
  117. void DHTPeerAnnounceStorage::handleTimeout()
  118. {
  119. if(logger_->debug()) {
  120. logger_->debug("Now purge peer announces(%lu entries) which are timed out.",
  121. static_cast<unsigned long>(entries_.size()));
  122. }
  123. std::for_each(entries_.begin(), entries_.end(), RemoveStalePeerAddrEntry());
  124. entries_.erase(std::remove_if(entries_.begin(), entries_.end(),
  125. mem_fun_sh(&DHTPeerAnnounceEntry::empty)),
  126. entries_.end());
  127. if(logger_->debug()) {
  128. logger_->debug("Currently %lu peer announce entries",
  129. static_cast<unsigned long>(entries_.size()));
  130. }
  131. }
  132. void DHTPeerAnnounceStorage::announcePeer()
  133. {
  134. if(logger_->debug()) {
  135. logger_->debug("Now announcing peer.");
  136. }
  137. for(std::deque<SharedHandle<DHTPeerAnnounceEntry> >::iterator i =
  138. entries_.begin(), eoi = entries_.end(); i != eoi; ++i) {
  139. if((*i)->getLastUpdated().
  140. difference(global::wallclock) >= DHT_PEER_ANNOUNCE_INTERVAL) {
  141. (*i)->notifyUpdate();
  142. SharedHandle<DHTTask> task =
  143. taskFactory_->createPeerAnnounceTask((*i)->getInfoHash());
  144. taskQueue_->addPeriodicTask2(task);
  145. if(logger_->debug()) {
  146. logger_->debug("Added 1 peer announce: infoHash=%s",
  147. util::toHex((*i)->getInfoHash(), DHT_ID_LENGTH).c_str());
  148. }
  149. }
  150. }
  151. }
  152. void DHTPeerAnnounceStorage::setTaskQueue(const SharedHandle<DHTTaskQueue>& taskQueue)
  153. {
  154. taskQueue_ = taskQueue;
  155. }
  156. void DHTPeerAnnounceStorage::setTaskFactory(const SharedHandle<DHTTaskFactory>& taskFactory)
  157. {
  158. taskFactory_ = taskFactory;
  159. }
  160. } // namespace aria2