DefaultPeerStorage.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. DefaultPeerStorage::DefaultPeerStorage(BtContextHandle btContext,
  39. const Option* option):
  40. btContext(btContext),
  41. option(option),
  42. maxPeerListSize(MAX_PEER_LIST_SIZE),
  43. btRuntime(BT_RUNTIME(btContext)),
  44. removedPeerSessionDownloadLength(0),
  45. removedPeerSessionUploadLength(0)
  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. peers.push_back(peer);
  57. return true;
  58. } else {
  59. const PeerHandle& peer = *itr;
  60. if(!peer->isGood() || peer->cuid != 0) {
  61. return false;
  62. } else {
  63. *itr = peer;
  64. return true;
  65. }
  66. }
  67. }
  68. void DefaultPeerStorage::addPeer(const Peers& peers) {
  69. for(Peers::const_iterator itr = peers.begin();
  70. itr != peers.end(); itr++) {
  71. const PeerHandle& peer = *itr;
  72. if(addPeer(peer)) {
  73. logger->debug("Adding peer %s:%d",
  74. peer->ipaddr.c_str(), peer->port);
  75. }
  76. }
  77. }
  78. const Peers& DefaultPeerStorage::getPeers() {
  79. return peers;
  80. }
  81. class FindFinePeer {
  82. public:
  83. bool operator()(const PeerHandle& peer) const {
  84. return peer->cuid == 0 && peer->isGood();
  85. }
  86. };
  87. PeerHandle DefaultPeerStorage::getUnusedPeer() {
  88. Peers::const_iterator itr = find_if(peers.begin(), peers.end(),
  89. FindFinePeer());
  90. if(itr == peers.end()) {
  91. return 0;
  92. } else {
  93. return *itr;
  94. }
  95. }
  96. class FindPeer {
  97. private:
  98. string ipaddr;
  99. int port;
  100. public:
  101. FindPeer(const string& ipaddr, int port):ipaddr(ipaddr), port(port) {}
  102. bool operator()(const PeerHandle& peer) const {
  103. return ipaddr == peer->ipaddr && port == peer->port;
  104. }
  105. };
  106. PeerHandle DefaultPeerStorage::getPeer(const string& ipaddr,
  107. int port) const {
  108. Peers::const_iterator itr = find_if(peers.begin(), peers.end(),
  109. FindPeer(ipaddr, port));
  110. if(itr == peers.end()) {
  111. return 0;
  112. } else {
  113. return *itr;
  114. }
  115. }
  116. int DefaultPeerStorage::countPeer() const {
  117. return peers.size();
  118. }
  119. bool DefaultPeerStorage::isPeerAvailable() {
  120. return !getUnusedPeer().isNull();
  121. }
  122. Peers DefaultPeerStorage::getActivePeers() {
  123. Peers activePeers;
  124. for(Peers::iterator itr = peers.begin(); itr != peers.end(); itr++) {
  125. PeerHandle& peer = *itr;
  126. if(peer->isActive()) {
  127. activePeers.push_back(peer);
  128. }
  129. }
  130. return activePeers;
  131. }
  132. TransferStat DefaultPeerStorage::calculateStat() {
  133. TransferStat stat;
  134. for(Peers::iterator itr = peers.begin(); itr != peers.end(); itr++) {
  135. PeerHandle& peer = *itr;
  136. if(peer->isActive()) {
  137. stat.downloadSpeed += peer->calculateDownloadSpeed();
  138. stat.uploadSpeed += peer->calculateUploadSpeed();
  139. }
  140. stat.sessionDownloadLength += peer->getSessionDownloadLength();
  141. stat.sessionUploadLength += peer->getSessionUploadLength();
  142. }
  143. stat.sessionDownloadLength += removedPeerSessionDownloadLength;
  144. stat.sessionUploadLength += removedPeerSessionUploadLength;
  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. // Update removedPeerSession******Length
  153. removedPeerSessionDownloadLength += p->getSessionDownloadLength();
  154. removedPeerSessionUploadLength += p->getSessionUploadLength();
  155. itr = peers.erase(itr);
  156. delSize--;
  157. } else {
  158. itr++;
  159. }
  160. }
  161. }