DefaultPeerStorage.cc 6.6 KB

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