DHTPeerAnnounceStorage.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #include "fmt.h"
  50. namespace aria2 {
  51. DHTPeerAnnounceStorage::DHTPeerAnnounceStorage()
  52. : taskQueue_{nullptr}, taskFactory_{nullptr}
  53. {
  54. }
  55. bool DHTPeerAnnounceStorage::InfoHashLess::
  56. operator()(const std::shared_ptr<DHTPeerAnnounceEntry>& lhs,
  57. const std::shared_ptr<DHTPeerAnnounceEntry>& rhs)
  58. {
  59. return memcmp(lhs->getInfoHash(), rhs->getInfoHash(), DHT_ID_LENGTH) < 0;
  60. }
  61. std::shared_ptr<DHTPeerAnnounceEntry>
  62. DHTPeerAnnounceStorage::getPeerAnnounceEntry(const unsigned char* infoHash)
  63. {
  64. auto entry = std::make_shared<DHTPeerAnnounceEntry>(infoHash);
  65. auto i = entries_.lower_bound(entry);
  66. if (i != entries_.end() &&
  67. memcmp(infoHash, (*i)->getInfoHash(), DHT_ID_LENGTH) == 0) {
  68. entry = *i;
  69. }
  70. else {
  71. entries_.insert(i, entry);
  72. }
  73. return entry;
  74. }
  75. void DHTPeerAnnounceStorage::addPeerAnnounce(const unsigned char* infoHash,
  76. const std::string& ipaddr,
  77. uint16_t port)
  78. {
  79. A2_LOG_DEBUG(fmt("Adding %s:%u to peer announce list: infoHash=%s",
  80. ipaddr.c_str(), port,
  81. util::toHex(infoHash, DHT_ID_LENGTH).c_str()));
  82. getPeerAnnounceEntry(infoHash)->addPeerAddrEntry(PeerAddrEntry(ipaddr, port));
  83. }
  84. bool DHTPeerAnnounceStorage::contains(const unsigned char* infoHash) const
  85. {
  86. auto entry = std::make_shared<DHTPeerAnnounceEntry>(infoHash);
  87. return std::binary_search(entries_.begin(), entries_.end(), entry,
  88. InfoHashLess());
  89. }
  90. void DHTPeerAnnounceStorage::getPeers(std::vector<std::shared_ptr<Peer>>& peers,
  91. const unsigned char* infoHash)
  92. {
  93. auto entry = std::make_shared<DHTPeerAnnounceEntry>(infoHash);
  94. auto i = entries_.find(entry);
  95. if (i != entries_.end()) {
  96. (*i)->getPeers(peers);
  97. }
  98. }
  99. void DHTPeerAnnounceStorage::handleTimeout()
  100. {
  101. A2_LOG_DEBUG(fmt("Now purge peer announces(%lu entries) which are timed out.",
  102. static_cast<unsigned long>(entries_.size())));
  103. std::for_each(std::begin(entries_), std::end(entries_),
  104. [](const std::shared_ptr<DHTPeerAnnounceEntry>& e) {
  105. e->removeStalePeerAddrEntry(DHT_PEER_ANNOUNCE_PURGE_INTERVAL);
  106. });
  107. for (auto i = std::begin(entries_); i != std::end(entries_);) {
  108. if ((*i)->empty()) {
  109. entries_.erase(i++);
  110. }
  111. else {
  112. ++i;
  113. }
  114. }
  115. A2_LOG_DEBUG(fmt("Currently %lu peer announce entries",
  116. static_cast<unsigned long>(entries_.size())));
  117. }
  118. void DHTPeerAnnounceStorage::announcePeer()
  119. {
  120. A2_LOG_DEBUG("Now announcing peer.");
  121. for (auto& e : entries_) {
  122. if (e->getLastUpdated().difference(global::wallclock()) <
  123. DHT_PEER_ANNOUNCE_INTERVAL) {
  124. continue;
  125. }
  126. e->notifyUpdate();
  127. auto task = taskFactory_->createPeerAnnounceTask(e->getInfoHash());
  128. taskQueue_->addPeriodicTask2(task);
  129. A2_LOG_DEBUG(fmt("Added 1 peer announce: infoHash=%s",
  130. util::toHex(e->getInfoHash(), DHT_ID_LENGTH).c_str()));
  131. }
  132. }
  133. void DHTPeerAnnounceStorage::setTaskQueue(DHTTaskQueue* taskQueue)
  134. {
  135. taskQueue_ = taskQueue;
  136. }
  137. void DHTPeerAnnounceStorage::setTaskFactory(DHTTaskFactory* taskFactory)
  138. {
  139. taskFactory_ = taskFactory;
  140. }
  141. } // namespace aria2