DefaultPeerStorage.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 "Logger.h"
  38. #include "BtRegistry.h"
  39. #include "message.h"
  40. #include "a2time.h"
  41. #include "Peer.h"
  42. #include "BtContext.h"
  43. #include "BtRuntime.h"
  44. #include "BtSeederStateChoke.h"
  45. #include "BtLeecherStateChoke.h"
  46. #include "PieceStorage.h"
  47. #include <algorithm>
  48. namespace aria2 {
  49. DefaultPeerStorage::DefaultPeerStorage(const BtContextHandle& btContext,
  50. const Option* option):
  51. btContext(btContext),
  52. option(option),
  53. maxPeerListSize(MAX_PEER_LIST_SIZE),
  54. btRuntime(BT_RUNTIME(btContext)),
  55. removedPeerSessionDownloadLength(0),
  56. removedPeerSessionUploadLength(0),
  57. _seederStateChoke(new BtSeederStateChoke(btContext)),
  58. _leecherStateChoke(new BtLeecherStateChoke())
  59. {
  60. logger = LogFactory::getInstance();
  61. }
  62. DefaultPeerStorage::~DefaultPeerStorage()
  63. {
  64. delete _seederStateChoke;
  65. delete _leecherStateChoke;
  66. }
  67. class FindIdenticalPeer {
  68. private:
  69. PeerHandle _peer;
  70. public:
  71. FindIdenticalPeer(const PeerHandle& peer):_peer(peer) {}
  72. bool operator()(const PeerHandle& peer) const {
  73. return (_peer == peer) ||
  74. ((_peer->ipaddr == peer->ipaddr) && (_peer->port == peer->port));
  75. }
  76. };
  77. bool DefaultPeerStorage::isPeerAlreadyAdded(const PeerHandle& peer)
  78. {
  79. return std::find_if(peers.begin(), peers.end(), FindIdenticalPeer(peer)) != peers.end();
  80. }
  81. bool DefaultPeerStorage::addPeer(const PeerHandle& peer) {
  82. if(isPeerAlreadyAdded(peer)) {
  83. logger->debug("Adding %s:%u is rejected because it has been already added.", peer->ipaddr.c_str(), peer->port);
  84. return false;
  85. }
  86. if(peers.size() >= maxPeerListSize) {
  87. deleteUnusedPeer(peers.size()-maxPeerListSize+1);
  88. }
  89. peers.push_front(peer);
  90. return true;
  91. }
  92. void DefaultPeerStorage::addPeer(const Peers& peers) {
  93. for(Peers::const_iterator itr = peers.begin();
  94. itr != peers.end(); itr++) {
  95. const PeerHandle& peer = *itr;
  96. if(addPeer(peer)) {
  97. logger->debug(MSG_ADDING_PEER,
  98. peer->ipaddr.c_str(), peer->port);
  99. }
  100. }
  101. }
  102. const Peers& DefaultPeerStorage::getPeers() {
  103. return peers;
  104. }
  105. class FindFinePeer {
  106. public:
  107. bool operator()(const PeerHandle& peer) const {
  108. return peer->unused() && peer->isGood();
  109. }
  110. };
  111. PeerHandle DefaultPeerStorage::getUnusedPeer() {
  112. Peers::const_iterator itr = std::find_if(peers.begin(), peers.end(),
  113. FindFinePeer());
  114. if(itr == peers.end()) {
  115. return 0;
  116. } else {
  117. return *itr;
  118. }
  119. }
  120. class FindPeer {
  121. private:
  122. std::string ipaddr;
  123. uint16_t port;
  124. public:
  125. FindPeer(const std::string& ipaddr, uint16_t port):ipaddr(ipaddr), port(port) {}
  126. bool operator()(const PeerHandle& peer) const {
  127. return ipaddr == peer->ipaddr && port == peer->port;
  128. }
  129. };
  130. PeerHandle DefaultPeerStorage::getPeer(const std::string& ipaddr,
  131. uint16_t port) const {
  132. Peers::const_iterator itr = std::find_if(peers.begin(), peers.end(),
  133. FindPeer(ipaddr, port));
  134. if(itr == peers.end()) {
  135. return 0;
  136. } else {
  137. return *itr;
  138. }
  139. }
  140. size_t DefaultPeerStorage::countPeer() const {
  141. return peers.size();
  142. }
  143. bool DefaultPeerStorage::isPeerAvailable() {
  144. return !getUnusedPeer().isNull();
  145. }
  146. class CollectActivePeer {
  147. private:
  148. Peers _activePeers;
  149. public:
  150. void operator()(const PeerHandle& peer)
  151. {
  152. if(peer->isActive()) {
  153. _activePeers.push_back(peer);
  154. }
  155. }
  156. const Peers& getActivePeers() { return _activePeers; }
  157. };
  158. Peers DefaultPeerStorage::getActivePeers() {
  159. return std::for_each(peers.begin(), peers.end(), CollectActivePeer()).getActivePeers();
  160. }
  161. class CalculateStat {
  162. private:
  163. TransferStat _stat;
  164. struct timeval _now;
  165. public:
  166. CalculateStat()
  167. {
  168. gettimeofday(&_now, 0);
  169. }
  170. void operator()(const PeerHandle& peer)
  171. {
  172. if(peer->isActive()) {
  173. _stat.downloadSpeed += peer->calculateDownloadSpeed(_now);
  174. _stat.uploadSpeed += peer->calculateUploadSpeed(_now);
  175. _stat.sessionDownloadLength += peer->getSessionDownloadLength();
  176. _stat.sessionUploadLength += peer->getSessionUploadLength();
  177. }
  178. }
  179. const TransferStat& getTransferStat() { return _stat; }
  180. };
  181. TransferStat DefaultPeerStorage::calculateStat() {
  182. TransferStat stat = std::for_each(peers.begin(), peers.end(), CalculateStat()).getTransferStat();
  183. stat.sessionDownloadLength += removedPeerSessionDownloadLength;
  184. stat.sessionUploadLength += removedPeerSessionUploadLength;
  185. stat.setAllTimeUploadLength(btRuntime->getUploadLengthAtStartup()+
  186. stat.getSessionUploadLength());
  187. return stat;
  188. }
  189. void DefaultPeerStorage::deleteUnusedPeer(size_t delSize) {
  190. Peers temp;
  191. for(Peers::reverse_iterator itr = peers.rbegin();
  192. itr != peers.rend(); ++itr) {
  193. const PeerHandle& p = *itr;
  194. if(p->unused() && delSize > 0) {
  195. // Update removedPeerSession******Length
  196. onErasingPeer(p);
  197. delSize--;
  198. } else {
  199. temp.push_front(p);
  200. }
  201. }
  202. peers = temp;
  203. }
  204. void DefaultPeerStorage::onErasingPeer(const SharedHandle<Peer>& peer) {}
  205. void DefaultPeerStorage::onReturningPeer(const SharedHandle<Peer>& peer)
  206. {
  207. if(peer->isActive()) {
  208. removedPeerSessionDownloadLength += peer->getSessionDownloadLength();
  209. removedPeerSessionUploadLength += peer->getSessionUploadLength();
  210. }
  211. }
  212. void DefaultPeerStorage::returnPeer(const PeerHandle& peer)
  213. {
  214. Peers::iterator itr = std::find(peers.begin(), peers.end(), peer);
  215. if(itr == peers.end()) {
  216. logger->debug("Cannot find peer %s:%u in PeerStorage.", peer->ipaddr.c_str(), peer->port);
  217. } else {
  218. onReturningPeer(peer);
  219. if((*itr)->port == 0) {
  220. onErasingPeer(*itr);
  221. peers.erase(itr);
  222. } else {
  223. peer->startBadCondition();
  224. peer->resetStatus();
  225. peers.erase(itr);
  226. peers.push_back(peer);
  227. }
  228. }
  229. }
  230. bool DefaultPeerStorage::chokeRoundIntervalElapsed()
  231. {
  232. const time_t CHOKE_ROUND_INTERVAL = 10;
  233. if(PIECE_STORAGE(btContext)->downloadFinished()) {
  234. return _seederStateChoke->getLastRound().elapsed(CHOKE_ROUND_INTERVAL);
  235. } else {
  236. return _leecherStateChoke->getLastRound().elapsed(CHOKE_ROUND_INTERVAL);
  237. }
  238. }
  239. void DefaultPeerStorage::executeChoke()
  240. {
  241. if(PIECE_STORAGE(btContext)->downloadFinished()) {
  242. return _seederStateChoke->executeChoke(getActivePeers());
  243. } else {
  244. return _leecherStateChoke->executeChoke(getActivePeers());
  245. }
  246. }
  247. } // namespace aria2