DefaultPeerStorage.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 SharedHandle<Peer>();
  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 SharedHandle<Peer>();
  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. std::deque<SharedHandle<Peer> >& _activePeers;
  149. public:
  150. CollectActivePeer(std::deque<SharedHandle<Peer> >& activePeers):
  151. _activePeers(activePeers) {}
  152. void operator()(const SharedHandle<Peer>& peer)
  153. {
  154. if(peer->isActive()) {
  155. _activePeers.push_back(peer);
  156. }
  157. }
  158. };
  159. void DefaultPeerStorage::getActivePeers(std::deque<SharedHandle<Peer> >& activePeers)
  160. {
  161. std::for_each(peers.begin(), peers.end(), CollectActivePeer(activePeers));
  162. }
  163. class CalculateStat {
  164. private:
  165. TransferStat _stat;
  166. struct timeval _now;
  167. public:
  168. CalculateStat()
  169. {
  170. gettimeofday(&_now, 0);
  171. }
  172. void operator()(const PeerHandle& peer)
  173. {
  174. if(peer->isActive()) {
  175. _stat.downloadSpeed += peer->calculateDownloadSpeed(_now);
  176. _stat.uploadSpeed += peer->calculateUploadSpeed(_now);
  177. _stat.sessionDownloadLength += peer->getSessionDownloadLength();
  178. _stat.sessionUploadLength += peer->getSessionUploadLength();
  179. }
  180. }
  181. const TransferStat& getTransferStat() { return _stat; }
  182. };
  183. TransferStat DefaultPeerStorage::calculateStat() {
  184. TransferStat stat = std::for_each(peers.begin(), peers.end(), CalculateStat()).getTransferStat();
  185. stat.sessionDownloadLength += removedPeerSessionDownloadLength;
  186. stat.sessionUploadLength += removedPeerSessionUploadLength;
  187. stat.setAllTimeUploadLength(btRuntime->getUploadLengthAtStartup()+
  188. stat.getSessionUploadLength());
  189. return stat;
  190. }
  191. void DefaultPeerStorage::deleteUnusedPeer(size_t delSize) {
  192. Peers temp;
  193. for(Peers::reverse_iterator itr = peers.rbegin();
  194. itr != peers.rend(); ++itr) {
  195. const PeerHandle& p = *itr;
  196. if(p->unused() && delSize > 0) {
  197. // Update removedPeerSession******Length
  198. onErasingPeer(p);
  199. delSize--;
  200. } else {
  201. temp.push_front(p);
  202. }
  203. }
  204. peers = temp;
  205. }
  206. void DefaultPeerStorage::onErasingPeer(const SharedHandle<Peer>& peer) {}
  207. void DefaultPeerStorage::onReturningPeer(const SharedHandle<Peer>& peer)
  208. {
  209. if(peer->isActive()) {
  210. removedPeerSessionDownloadLength += peer->getSessionDownloadLength();
  211. removedPeerSessionUploadLength += peer->getSessionUploadLength();
  212. }
  213. }
  214. void DefaultPeerStorage::returnPeer(const PeerHandle& peer)
  215. {
  216. Peers::iterator itr = std::find(peers.begin(), peers.end(), peer);
  217. if(itr == peers.end()) {
  218. logger->debug("Cannot find peer %s:%u in PeerStorage.", peer->ipaddr.c_str(), peer->port);
  219. } else {
  220. onReturningPeer(peer);
  221. if((*itr)->port == 0) {
  222. onErasingPeer(*itr);
  223. peers.erase(itr);
  224. } else {
  225. peer->startBadCondition();
  226. peer->resetStatus();
  227. peers.erase(itr);
  228. peers.push_back(peer);
  229. }
  230. }
  231. }
  232. bool DefaultPeerStorage::chokeRoundIntervalElapsed()
  233. {
  234. const time_t CHOKE_ROUND_INTERVAL = 10;
  235. if(PIECE_STORAGE(btContext)->downloadFinished()) {
  236. return _seederStateChoke->getLastRound().elapsed(CHOKE_ROUND_INTERVAL);
  237. } else {
  238. return _leecherStateChoke->getLastRound().elapsed(CHOKE_ROUND_INTERVAL);
  239. }
  240. }
  241. void DefaultPeerStorage::executeChoke()
  242. {
  243. std::deque<SharedHandle<Peer> > activePeers;
  244. getActivePeers(activePeers);
  245. if(PIECE_STORAGE(btContext)->downloadFinished()) {
  246. return _seederStateChoke->executeChoke(activePeers);
  247. } else {
  248. return _leecherStateChoke->executeChoke(activePeers);
  249. }
  250. }
  251. } // namespace aria2