DefaultPeerStorage.cc 7.8 KB

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