DHTPeerAnnounceStorage.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "DHTPeerAnnounceEntry.h"
  37. #include "Peer.h"
  38. #include "BtContext.h"
  39. #include "DHTConstants.h"
  40. #include "DHTTaskQueue.h"
  41. #include "DHTTaskFactory.h"
  42. #include "DHTTask.h"
  43. #include "LogFactory.h"
  44. #include "Logger.h"
  45. #include "Util.h"
  46. #include <cstring>
  47. #include <algorithm>
  48. namespace aria2 {
  49. DHTPeerAnnounceStorage::DHTPeerAnnounceStorage():_taskQueue(0),
  50. _taskFactory(0),
  51. _logger(LogFactory::getInstance()) {}
  52. DHTPeerAnnounceStorage::~DHTPeerAnnounceStorage() {}
  53. class FindPeerAnnounceEntry {
  54. private:
  55. unsigned char _infoHash[DHT_ID_LENGTH];
  56. public:
  57. FindPeerAnnounceEntry(const unsigned char* infoHash)
  58. {
  59. memcpy(_infoHash, infoHash, DHT_ID_LENGTH);
  60. }
  61. bool operator()(const SharedHandle<DHTPeerAnnounceEntry>& entry) const
  62. {
  63. return memcmp(_infoHash, entry->getInfoHash(), DHT_ID_LENGTH) == 0;
  64. }
  65. };
  66. SharedHandle<DHTPeerAnnounceEntry>
  67. DHTPeerAnnounceStorage::getPeerAnnounceEntry(const unsigned char* infoHash)
  68. {
  69. std::deque<SharedHandle<DHTPeerAnnounceEntry> >::iterator i =
  70. std::find_if(_entries.begin(), _entries.end(), FindPeerAnnounceEntry(infoHash));
  71. SharedHandle<DHTPeerAnnounceEntry> entry = 0;
  72. if(i == _entries.end()) {
  73. entry = new DHTPeerAnnounceEntry(infoHash);
  74. _entries.push_back(entry);
  75. } else {
  76. entry = *i;
  77. }
  78. return entry;
  79. }
  80. void
  81. DHTPeerAnnounceStorage::addPeerAnnounce(const unsigned char* infoHash,
  82. const std::string& ipaddr, uint16_t port)
  83. {
  84. _logger->debug("Adding %s:%u to peer announce list: infoHash=%s",
  85. ipaddr.c_str(), port, Util::toHex(infoHash, DHT_ID_LENGTH).c_str());
  86. getPeerAnnounceEntry(infoHash)->addPeerAddrEntry(PeerAddrEntry(ipaddr, port));
  87. }
  88. // add peer announce as localhost downloading the content
  89. void DHTPeerAnnounceStorage::addPeerAnnounce(const BtContextHandle& ctx)
  90. {
  91. _logger->debug("Adding localhost to peer announce list: infoHash=%s",
  92. ctx->getInfoHashAsString().c_str());
  93. getPeerAnnounceEntry(ctx->getInfoHash())->setBtContext(ctx);
  94. }
  95. void DHTPeerAnnounceStorage::removePeerAnnounce(const BtContextHandle& ctx)
  96. {
  97. std::deque<SharedHandle<DHTPeerAnnounceEntry> >::iterator i =
  98. std::find_if(_entries.begin(), _entries.end(), FindPeerAnnounceEntry(ctx->getInfoHash()));
  99. if(i != _entries.end()) {
  100. (*i)->setBtContext(0);
  101. if((*i)->empty()) {
  102. _entries.erase(i);
  103. }
  104. }
  105. }
  106. bool DHTPeerAnnounceStorage::contains(const unsigned char* infoHash) const
  107. {
  108. return
  109. std::find_if(_entries.begin(), _entries.end(), FindPeerAnnounceEntry(infoHash)) != _entries.end();
  110. }
  111. Peers DHTPeerAnnounceStorage::getPeers(const unsigned char* infoHash)
  112. {
  113. std::deque<SharedHandle<DHTPeerAnnounceEntry> >::iterator i =
  114. std::find_if(_entries.begin(), _entries.end(), FindPeerAnnounceEntry(infoHash));
  115. if(i == _entries.end() || (*i)->empty()) {
  116. return Peers();
  117. }
  118. return (*i)->getPeers();
  119. }
  120. void DHTPeerAnnounceStorage::handleTimeout()
  121. {
  122. _logger->debug("Now purge peer announces which are timed out.");
  123. for(std::deque<SharedHandle<DHTPeerAnnounceEntry> >::iterator i = _entries.begin(); i != _entries.end();) {
  124. (*i)->removeStalePeerAddrEntry(DHT_PEER_ANNOUNCE_PURGE_INTERVAL);
  125. if((*i)->empty()) {
  126. _logger->debug("1 entry purged: infoHash=%s",
  127. Util::toHex((*i)->getInfoHash(), DHT_ID_LENGTH).c_str());
  128. i = _entries.erase(i);
  129. } else {
  130. ++i;
  131. }
  132. }
  133. }
  134. void DHTPeerAnnounceStorage::announcePeer()
  135. {
  136. _logger->debug("Now announcing peer.");
  137. for(std::deque<SharedHandle<DHTPeerAnnounceEntry> >::iterator i = _entries.begin(); i != _entries.end(); ++i) {
  138. if((*i)->getLastUpdated().elapsed(DHT_PEER_ANNOUNCE_INTERVAL)) {
  139. (*i)->notifyUpdate();
  140. SharedHandle<DHTTask> task = _taskFactory->createPeerAnnounceTask((*i)->getInfoHash());
  141. _taskQueue->addPeriodicTask2(task);
  142. _logger->debug("Added 1 peer announce: infoHash=%s",
  143. Util::toHex((*i)->getInfoHash(), DHT_ID_LENGTH).c_str());
  144. }
  145. }
  146. }
  147. void DHTPeerAnnounceStorage::setTaskQueue(const SharedHandle<DHTTaskQueue>& taskQueue)
  148. {
  149. _taskQueue = taskQueue;
  150. }
  151. void DHTPeerAnnounceStorage::setTaskFactory(const SharedHandle<DHTTaskFactory>& taskFactory)
  152. {
  153. _taskFactory = taskFactory;
  154. }
  155. } // namespace aria2