Peer.cc 4.4 KB

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