Peer.cc 4.3 KB

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