DefaultPeerStorage.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #include "message.h"
  39. #include "a2time.h"
  40. DefaultPeerStorage::DefaultPeerStorage(BtContextHandle btContext,
  41. const Option* option):
  42. btContext(btContext),
  43. option(option),
  44. maxPeerListSize(MAX_PEER_LIST_SIZE),
  45. btRuntime(BT_RUNTIME(btContext)),
  46. removedPeerSessionDownloadLength(0),
  47. removedPeerSessionUploadLength(0)
  48. {
  49. logger = LogFactory::getInstance();
  50. }
  51. DefaultPeerStorage::~DefaultPeerStorage() {}
  52. bool DefaultPeerStorage::isPeerAlreadyAdded(const PeerHandle& peer)
  53. {
  54. return find(peers.begin(), peers.end(), peer) != peers.end();
  55. }
  56. bool DefaultPeerStorage::addPeer(const PeerHandle& peer) {
  57. if(isPeerAlreadyAdded(peer)) {
  58. logger->debug("Adding %s:%u is rejected because it has been already added.", peer->ipaddr.c_str(), peer->port);
  59. return false;
  60. }
  61. if(peers.size() >= (size_t)maxPeerListSize) {
  62. deleteUnusedPeer(peers.size()-maxPeerListSize+1);
  63. }
  64. peers.push_front(peer);
  65. return true;
  66. }
  67. void DefaultPeerStorage::addPeer(const Peers& peers) {
  68. for(Peers::const_iterator itr = peers.begin();
  69. itr != peers.end(); itr++) {
  70. const PeerHandle& peer = *itr;
  71. if(addPeer(peer)) {
  72. logger->debug(MSG_ADDING_PEER,
  73. peer->ipaddr.c_str(), peer->port);
  74. }
  75. }
  76. }
  77. const Peers& DefaultPeerStorage::getPeers() {
  78. return peers;
  79. }
  80. class FindFinePeer {
  81. public:
  82. bool operator()(const PeerHandle& peer) const {
  83. return peer->cuid == 0 && peer->isGood();
  84. }
  85. };
  86. PeerHandle DefaultPeerStorage::getUnusedPeer() {
  87. Peers::const_iterator itr = find_if(peers.begin(), peers.end(),
  88. FindFinePeer());
  89. if(itr == peers.end()) {
  90. return 0;
  91. } else {
  92. return *itr;
  93. }
  94. }
  95. class FindPeer {
  96. private:
  97. string ipaddr;
  98. int32_t port;
  99. public:
  100. FindPeer(const string& ipaddr, int32_t port):ipaddr(ipaddr), port(port) {}
  101. bool operator()(const PeerHandle& peer) const {
  102. return ipaddr == peer->ipaddr && port == peer->port;
  103. }
  104. };
  105. PeerHandle DefaultPeerStorage::getPeer(const string& ipaddr,
  106. int32_t port) const {
  107. Peers::const_iterator itr = find_if(peers.begin(), peers.end(),
  108. FindPeer(ipaddr, port));
  109. if(itr == peers.end()) {
  110. return 0;
  111. } else {
  112. return *itr;
  113. }
  114. }
  115. int32_t DefaultPeerStorage::countPeer() const {
  116. return peers.size();
  117. }
  118. bool DefaultPeerStorage::isPeerAvailable() {
  119. return !getUnusedPeer().isNull();
  120. }
  121. class CollectActivePeer {
  122. private:
  123. Peers _activePeers;
  124. public:
  125. void operator()(const PeerHandle& peer)
  126. {
  127. if(peer->isActive()) {
  128. _activePeers.push_back(peer);
  129. }
  130. }
  131. const Peers& getActivePeers() { return _activePeers; }
  132. };
  133. Peers DefaultPeerStorage::getActivePeers() {
  134. return for_each(peers.begin(), peers.end(), CollectActivePeer()).getActivePeers();
  135. }
  136. class CalculateStat {
  137. private:
  138. TransferStat _stat;
  139. struct timeval _now;
  140. public:
  141. CalculateStat()
  142. {
  143. gettimeofday(&_now, 0);
  144. }
  145. void operator()(const PeerHandle& peer)
  146. {
  147. if(peer->isActive()) {
  148. _stat.downloadSpeed += peer->calculateDownloadSpeed(_now);
  149. _stat.uploadSpeed += peer->calculateUploadSpeed(_now);
  150. }
  151. _stat.sessionDownloadLength += peer->getSessionDownloadLength();
  152. _stat.sessionUploadLength += peer->getSessionUploadLength();
  153. }
  154. const TransferStat& getTransferStat() { return _stat; }
  155. };
  156. TransferStat DefaultPeerStorage::calculateStat() {
  157. TransferStat stat = for_each(peers.begin(), peers.end(), CalculateStat()).getTransferStat();
  158. stat.sessionDownloadLength += removedPeerSessionDownloadLength;
  159. stat.sessionUploadLength += removedPeerSessionUploadLength;
  160. stat.setAllTimeUploadLength(btRuntime->getUploadLengthAtStartup()+
  161. stat.getSessionUploadLength());
  162. return stat;
  163. }
  164. void DefaultPeerStorage::deleteUnusedPeer(int32_t delSize) {
  165. Peers temp;
  166. for(Peers::reverse_iterator itr = peers.rbegin();
  167. itr != peers.rend(); ++itr) {
  168. const PeerHandle& p = *itr;
  169. if(p->cuid == 0 && delSize > 0) {
  170. // Update removedPeerSession******Length
  171. onErasingPeer(p);
  172. delSize--;
  173. } else {
  174. temp.push_front(p);
  175. }
  176. }
  177. peers = temp;
  178. }
  179. void DefaultPeerStorage::onErasingPeer(const PeerHandle& peer)
  180. {
  181. removedPeerSessionDownloadLength += peer->getSessionDownloadLength();
  182. removedPeerSessionUploadLength += peer->getSessionUploadLength();
  183. }
  184. void DefaultPeerStorage::returnPeer(const PeerHandle& peer)
  185. {
  186. Peers::iterator itr = find(peers.begin(), peers.end(), peer);
  187. if(itr == peers.end()) {
  188. logger->debug("Cannot find peer %s:%u in PeerStorage.", peer->ipaddr.c_str(), peer->port);
  189. } else {
  190. if((*itr)->port == 0) {
  191. onErasingPeer(*itr);
  192. peers.erase(itr);
  193. } else {
  194. peer->startBadCondition();
  195. peer->resetStatus();
  196. peers.erase(itr);
  197. peers.push_back(peer);
  198. }
  199. }
  200. }