DefaultPeerStorage.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "DefaultPeerStorage.h"
  36. #include "LogFactory.h"
  37. #include "BtRegistry.h"
  38. extern PeerHandle nullPeer;
  39. DefaultPeerStorage::DefaultPeerStorage(BtContextHandle btContext,
  40. const Option* option):
  41. btContext(btContext),
  42. option(option),
  43. maxPeerListSize(MAX_PEER_LIST_SIZE),
  44. peerEntryIdCounter(0),
  45. btRuntime(BT_RUNTIME(btContext))
  46. {
  47. logger = LogFactory::getInstance();
  48. }
  49. DefaultPeerStorage::~DefaultPeerStorage() {}
  50. bool DefaultPeerStorage::addPeer(const PeerHandle& peer) {
  51. Peers::iterator itr = find(peers.begin(), peers.end(), peer);
  52. if(itr == peers.end()) {
  53. if(peers.size() >= (size_t)maxPeerListSize) {
  54. deleteUnusedPeer(peers.size()-maxPeerListSize+1);
  55. }
  56. ++peerEntryIdCounter;
  57. peer->entryId = peerEntryIdCounter;
  58. peers.push_back(peer);
  59. return true;
  60. } else {
  61. const PeerHandle& peer = *itr;
  62. if(peer->error >= MAX_PEER_ERROR || peer->cuid != 0) {
  63. return false;
  64. } else {
  65. *itr = peer;
  66. return true;
  67. }
  68. }
  69. }
  70. void DefaultPeerStorage::addPeer(const Peers& peers) {
  71. for(Peers::const_iterator itr = peers.begin();
  72. itr != peers.end(); itr++) {
  73. const PeerHandle& peer = *itr;
  74. if(addPeer(peer)) {
  75. logger->debug("Adding peer %s:%d",
  76. peer->ipaddr.c_str(), peer->port);
  77. }
  78. }
  79. }
  80. const Peers& DefaultPeerStorage::getPeers() {
  81. return peers;
  82. }
  83. class FindFinePeer {
  84. public:
  85. bool operator()(const PeerHandle& peer) const {
  86. return peer->cuid == 0 && peer->error < MAX_PEER_ERROR;
  87. }
  88. };
  89. PeerHandle DefaultPeerStorage::getUnusedPeer() {
  90. Peers::const_iterator itr = find_if(peers.begin(), peers.end(),
  91. FindFinePeer());
  92. if(itr == peers.end()) {
  93. return nullPeer;
  94. } else {
  95. return *itr;
  96. }
  97. }
  98. class FindPeer {
  99. private:
  100. string ipaddr;
  101. int port;
  102. public:
  103. FindPeer(const string& ipaddr, int port):ipaddr(ipaddr), port(port) {}
  104. bool operator()(const PeerHandle& peer) const {
  105. return ipaddr == peer->ipaddr && port == peer->port;
  106. }
  107. };
  108. PeerHandle DefaultPeerStorage::getPeer(const string& ipaddr,
  109. int port) const {
  110. Peers::const_iterator itr = find_if(peers.begin(), peers.end(),
  111. FindPeer(ipaddr, port));
  112. if(itr == peers.end()) {
  113. return nullPeer;
  114. } else {
  115. return *itr;
  116. }
  117. }
  118. int DefaultPeerStorage::countPeer() const {
  119. return peers.size();
  120. }
  121. bool DefaultPeerStorage::isPeerAvailable() {
  122. return getUnusedPeer() != nullPeer;
  123. }
  124. Peers DefaultPeerStorage::getActivePeers() {
  125. Peers activePeers;
  126. for(Peers::iterator itr = peers.begin(); itr != peers.end(); itr++) {
  127. PeerHandle& peer = *itr;
  128. if(peer->isActive()) {
  129. activePeers.push_back(peer);
  130. }
  131. }
  132. return activePeers;
  133. }
  134. TransferStat DefaultPeerStorage::calculateStat() {
  135. TransferStat stat;
  136. Peers activePeers = getActivePeers();
  137. for(Peers::iterator itr = activePeers.begin();
  138. itr != activePeers.end(); itr++) {
  139. PeerHandle& peer = *itr;
  140. stat.downloadSpeed += peer->calculateDownloadSpeed();
  141. stat.uploadSpeed += peer->calculateUploadSpeed();
  142. stat.sessionDownloadLength += peer->getSessionDownloadLength();
  143. stat.sessionUploadLength += peer->getSessionUploadLength();
  144. }
  145. return stat;
  146. }
  147. void DefaultPeerStorage::deleteUnusedPeer(int delSize) {
  148. for(Peers::iterator itr = peers.begin();
  149. itr != peers.end() && delSize > 0;) {
  150. const PeerHandle& p = *itr;
  151. if(p->cuid == 0) {
  152. itr = peers.erase(itr);
  153. delSize--;
  154. } else {
  155. itr++;
  156. }
  157. }
  158. }