Piece.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "Piece.h"
  36. #include "Util.h"
  37. #include "BitfieldManFactory.h"
  38. #include "BitfieldMan.h"
  39. namespace aria2 {
  40. Piece::Piece():index(0), length(0), _blockLength(BLOCK_LENGTH), bitfield(0) {}
  41. Piece::Piece(size_t index, size_t length, size_t blockLength):index(index), length(length), _blockLength(blockLength) {
  42. bitfield =
  43. BitfieldManFactory::getFactoryInstance()->createBitfieldMan(_blockLength, length);
  44. }
  45. Piece::Piece(const Piece& piece) {
  46. index = piece.index;
  47. length = piece.length;
  48. _blockLength = piece._blockLength;
  49. if(piece.bitfield == 0) {
  50. bitfield = 0;
  51. } else {
  52. bitfield = new BitfieldMan(*piece.bitfield);
  53. }
  54. }
  55. Piece::~Piece()
  56. {
  57. delete bitfield;
  58. }
  59. Piece& Piece::operator=(const Piece& piece)
  60. {
  61. if(this != &piece) {
  62. index = piece.index;
  63. length = piece.length;
  64. delete bitfield;
  65. if(piece.bitfield) {
  66. bitfield = new BitfieldMan(*piece.bitfield);
  67. } else {
  68. bitfield = 0;
  69. }
  70. }
  71. return *this;
  72. }
  73. bool Piece::operator==(const Piece& piece) const
  74. {
  75. return index == piece.index;
  76. }
  77. void Piece::completeBlock(size_t blockIndex) {
  78. bitfield->setBit(blockIndex);
  79. bitfield->unsetUseBit(blockIndex);
  80. }
  81. void Piece::clearAllBlock() {
  82. bitfield->clearAllBit();
  83. bitfield->clearAllUseBit();
  84. }
  85. void Piece::setAllBlock() {
  86. bitfield->setAllBit();
  87. }
  88. bool Piece::pieceComplete() const {
  89. return bitfield->isAllBitSet();
  90. }
  91. size_t Piece::countBlock() const
  92. {
  93. return bitfield->countBlock();
  94. }
  95. size_t Piece::getBlockLength(size_t index) const
  96. {
  97. return bitfield->getBlockLength(index);
  98. }
  99. size_t Piece::getBlockLength() const
  100. {
  101. return bitfield->getBlockLength();
  102. }
  103. const unsigned char* Piece::getBitfield() const
  104. {
  105. return bitfield->getBitfield();
  106. }
  107. size_t Piece::getBitfieldLength() const
  108. {
  109. return bitfield->getBitfieldLength();
  110. }
  111. bool Piece::isBlockUsed(size_t index) const
  112. {
  113. return bitfield->isUseBitSet(index);
  114. }
  115. void Piece::cancelBlock(size_t blockIndex) {
  116. bitfield->unsetUseBit(blockIndex);
  117. }
  118. size_t Piece::countCompleteBlock() const
  119. {
  120. return bitfield->countBlock()-bitfield->countMissingBlock();
  121. }
  122. bool Piece::hasBlock(size_t blockIndex) const
  123. {
  124. return bitfield->isBitSet(blockIndex);
  125. }
  126. bool Piece::getMissingUnusedBlockIndex(size_t& index) const
  127. {
  128. if(bitfield->getFirstMissingUnusedIndex(index)) {
  129. bitfield->setUseBit(index);
  130. return true;
  131. } else {
  132. return false;
  133. }
  134. }
  135. bool Piece::getMissingBlockIndex(size_t& index) const
  136. {
  137. if(bitfield->getMissingIndex(index)) {
  138. bitfield->setUseBit(index);
  139. return true;
  140. } else {
  141. return false;
  142. }
  143. }
  144. bool Piece::getFirstMissingBlockIndexWithoutLock(size_t& index) const
  145. {
  146. return bitfield->getFirstMissingIndex(index);
  147. }
  148. bool Piece::getAllMissingBlockIndexes(std::deque<size_t>& indexes) const {
  149. return bitfield->getAllMissingIndexes(indexes);
  150. }
  151. std::string Piece::toString() const {
  152. return "piece: index="+Util::itos(index)+", length="+Util::itos(length);
  153. }
  154. void Piece::reconfigure(size_t length)
  155. {
  156. delete bitfield;
  157. this->length = length;
  158. bitfield =
  159. BitfieldManFactory::getFactoryInstance()->createBitfieldMan(_blockLength, length);
  160. }
  161. void Piece::setBitfield(const unsigned char* bitfield, size_t len)
  162. {
  163. this->bitfield->setBitfield(bitfield, len);
  164. }
  165. size_t Piece::getCompletedLength()
  166. {
  167. return bitfield->getCompletedLength();
  168. }
  169. } // namespace aria2