Peer.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "Peer.h"
  36. #include "BitfieldManFactory.h"
  37. #include "Util.h"
  38. Peer::Peer(string ipaddr, int port, int pieceLength, long long int totalLength):
  39. ipaddr(ipaddr),
  40. port(port),
  41. error(0),
  42. sessionUploadLength(0),
  43. sessionDownloadLength(0),
  44. pieceLength(pieceLength),
  45. active(false)
  46. {
  47. resetStatus();
  48. this->bitfield = BitfieldManFactory::getNewFactory()->
  49. createBitfieldMan(pieceLength, totalLength);
  50. string idSeed = ipaddr+":"+Util::itos(port);
  51. id = Util::simpleMessageDigest(idSeed);
  52. }
  53. /*
  54. Peer::Peer():entryId(0), ipaddr(""), port(0), bitfield(0),
  55. sessionUploadLength(0), sessionDownloadLength(0),
  56. pieceLength(0)
  57. {
  58. resetStatus();
  59. }
  60. */
  61. void Peer::updateBitfield(int index, int operation) {
  62. if(operation == 1) {
  63. bitfield->setBit(index);
  64. } else if(operation == 0) {
  65. bitfield->unsetBit(index);
  66. }
  67. }
  68. #define THRESHOLD 1024*1024*2
  69. bool Peer::shouldBeChoking() const {
  70. if(optUnchoking) {
  71. return false;
  72. }
  73. return chokingRequired;
  74. }
  75. bool Peer::hasPiece(int index) const {
  76. return bitfield->isBitSet(index);
  77. }
  78. bool Peer::isSeeder() const {
  79. return bitfield->isAllBitSet();
  80. }
  81. void Peer::resetStatus() {
  82. tryCount = 0;
  83. cuid = 0;
  84. amChoking = true;
  85. amInterested = false;
  86. peerChoking = true;
  87. peerInterested = false;
  88. chokingRequired = true;
  89. optUnchoking = false;
  90. snubbing = false;
  91. fastExtensionEnabled = false;
  92. latency = DEFAULT_LATENCY;
  93. fastSet.clear();
  94. peerAllowedIndexSet.clear();
  95. amAllowedIndexSet.clear();
  96. peerStat.reset();
  97. }
  98. bool Peer::isInFastSet(int index) const {
  99. return find(fastSet.begin(), fastSet.end(), index) != fastSet.end();
  100. }
  101. void Peer::addFastSetIndex(int index) {
  102. if(!isInFastSet(index)) {
  103. fastSet.push_back(index);
  104. }
  105. }
  106. bool Peer::isInPeerAllowedIndexSet(int index) const {
  107. return find(peerAllowedIndexSet.begin(), peerAllowedIndexSet.end(),
  108. index) != peerAllowedIndexSet.end();
  109. }
  110. void Peer::addPeerAllowedIndex(int index) {
  111. if(!isInPeerAllowedIndexSet(index)) {
  112. peerAllowedIndexSet.push_back(index);
  113. }
  114. }
  115. bool Peer::isInAmAllowedIndexSet(int index) const {
  116. return find(amAllowedIndexSet.begin(), amAllowedIndexSet.end(),
  117. index) != amAllowedIndexSet.end();
  118. }
  119. void Peer::addAmAllowedIndex(int index) {
  120. if(!isInAmAllowedIndexSet(index)) {
  121. amAllowedIndexSet.push_back(index);
  122. }
  123. }
  124. void Peer::setAllBitfield() {
  125. bitfield->setAllBit();
  126. }
  127. void Peer::updateLatency(int latency) {
  128. this->latency = (this->latency*20+latency*80)/200;
  129. }