DefaultPeerStorage.cc 6.6 KB

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