DefaultPeerStorage.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_front(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. bool DefaultPeerStorage::addIncomingPeer(const PeerHandle& peer)
  69. {
  70. incomingPeers.push_back(peer);
  71. return true;
  72. }
  73. void DefaultPeerStorage::addPeer(const Peers& peers) {
  74. for(Peers::const_iterator itr = peers.begin();
  75. itr != peers.end(); itr++) {
  76. const PeerHandle& peer = *itr;
  77. if(addPeer(peer)) {
  78. logger->debug("Adding peer %s:%d",
  79. peer->ipaddr.c_str(), peer->port);
  80. }
  81. }
  82. }
  83. const Peers& DefaultPeerStorage::getPeers() {
  84. return peers;
  85. }
  86. class FindFinePeer {
  87. public:
  88. bool operator()(const PeerHandle& peer) const {
  89. return peer->cuid == 0 && peer->isGood();
  90. }
  91. };
  92. PeerHandle DefaultPeerStorage::getUnusedPeer() {
  93. Peers::const_iterator itr = find_if(peers.begin(), peers.end(),
  94. FindFinePeer());
  95. if(itr == peers.end()) {
  96. return 0;
  97. } else {
  98. return *itr;
  99. }
  100. }
  101. class FindPeer {
  102. private:
  103. string ipaddr;
  104. int port;
  105. public:
  106. FindPeer(const string& ipaddr, int port):ipaddr(ipaddr), port(port) {}
  107. bool operator()(const PeerHandle& peer) const {
  108. return ipaddr == peer->ipaddr && port == peer->port;
  109. }
  110. };
  111. PeerHandle DefaultPeerStorage::getPeer(const string& ipaddr,
  112. int port) const {
  113. Peers::const_iterator itr = find_if(peers.begin(), peers.end(),
  114. FindPeer(ipaddr, port));
  115. if(itr == peers.end()) {
  116. return 0;
  117. } else {
  118. return *itr;
  119. }
  120. }
  121. int32_t DefaultPeerStorage::countPeer() const {
  122. return peers.size();
  123. }
  124. bool DefaultPeerStorage::isPeerAvailable() {
  125. return !getUnusedPeer().isNull();
  126. }
  127. class CollectActivePeer {
  128. private:
  129. Peers _activePeers;
  130. public:
  131. void operator()(const PeerHandle& peer)
  132. {
  133. if(peer->isActive()) {
  134. _activePeers.push_back(peer);
  135. }
  136. }
  137. const Peers& getActivePeers() { return _activePeers; }
  138. };
  139. Peers DefaultPeerStorage::getActivePeers() {
  140. CollectActivePeer funcObj;
  141. funcObj = for_each(peers.begin(), peers.end(), funcObj);
  142. funcObj = for_each(incomingPeers.begin(), incomingPeers.end(), funcObj);
  143. return funcObj.getActivePeers();
  144. }
  145. class CalculateStat {
  146. private:
  147. TransferStat _stat;
  148. struct timeval _now;
  149. public:
  150. CalculateStat()
  151. {
  152. gettimeofday(&_now, 0);
  153. }
  154. void operator()(const PeerHandle& peer)
  155. {
  156. if(peer->isActive()) {
  157. _stat.downloadSpeed += peer->calculateDownloadSpeed(_now);
  158. _stat.uploadSpeed += peer->calculateUploadSpeed(_now);
  159. }
  160. _stat.sessionDownloadLength += peer->getSessionDownloadLength();
  161. _stat.sessionUploadLength += peer->getSessionUploadLength();
  162. }
  163. const TransferStat& getTransferStat() { return _stat; }
  164. };
  165. TransferStat DefaultPeerStorage::calculateStat() {
  166. CalculateStat calStat;
  167. calStat = for_each(peers.begin(), peers.end(), calStat);
  168. calStat = for_each(incomingPeers.begin(), incomingPeers.end(), calStat);
  169. TransferStat stat = calStat.getTransferStat();
  170. stat.sessionDownloadLength += removedPeerSessionDownloadLength;
  171. stat.sessionUploadLength += removedPeerSessionUploadLength;
  172. return stat;
  173. }
  174. void DefaultPeerStorage::deleteUnusedPeer(int delSize) {
  175. Peers temp;
  176. for(Peers::reverse_iterator itr = peers.rbegin();
  177. itr != peers.rend(); ++itr) {
  178. const PeerHandle& p = *itr;
  179. if(p->cuid == 0 && delSize > 0) {
  180. // Update removedPeerSession******Length
  181. onErasingPeer(p);
  182. delSize--;
  183. } else {
  184. temp.push_front(p);
  185. }
  186. }
  187. peers = temp;
  188. }
  189. void DefaultPeerStorage::onErasingPeer(const PeerHandle& peer)
  190. {
  191. removedPeerSessionDownloadLength += peer->getSessionDownloadLength();
  192. removedPeerSessionUploadLength += peer->getSessionUploadLength();
  193. }
  194. void DefaultPeerStorage::returnPeer(const PeerHandle& peer)
  195. {
  196. Peers::iterator itr = find(peers.begin(), peers.end(), peer);
  197. if(itr == peers.end()) {
  198. itr = find(incomingPeers.begin(), incomingPeers.end(), peer);
  199. if(itr == peers.end()) {
  200. // do nothing
  201. } else {
  202. // erase incoming peer because we cannot connect to it with port number
  203. // (*itr)->port. It is not the listening port.
  204. onErasingPeer(*itr);
  205. incomingPeers.erase(itr);
  206. }
  207. } else {
  208. peer->startBadCondition();
  209. peer->resetStatus();
  210. peers.erase(itr);
  211. peers.push_back(peer);
  212. }
  213. }