DHTPeerAnnounceStorage.cc 6.2 KB

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