Piece.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. Piece::Piece():index(0), length(0), _blockLength(BLOCK_LENGTH), bitfield(0) {}
  39. Piece::Piece(int32_t index, int32_t length, int32_t blockLength):index(index), length(length), _blockLength(blockLength) {
  40. bitfield =
  41. BitfieldManFactory::getFactoryInstance()->createBitfieldMan(_blockLength, length);
  42. }
  43. Piece::Piece(const Piece& piece) {
  44. index = piece.index;
  45. length = piece.length;
  46. _blockLength = piece._blockLength;
  47. if(piece.bitfield == 0) {
  48. bitfield = 0;
  49. } else {
  50. bitfield = new BitfieldMan(*piece.bitfield);
  51. }
  52. }
  53. void Piece::completeBlock(int32_t blockIndex) {
  54. bitfield->setBit(blockIndex);
  55. bitfield->unsetUseBit(blockIndex);
  56. removeSubPiece(blockIndex);
  57. }
  58. void Piece::clearAllBlock() {
  59. bitfield->clearAllBit();
  60. bitfield->clearAllUseBit();
  61. }
  62. void Piece::setAllBlock() {
  63. bitfield->setAllBit();
  64. }
  65. bool Piece::pieceComplete() const {
  66. return bitfield->isAllBitSet();
  67. }
  68. void Piece::cancelBlock(int32_t blockIndex) {
  69. bitfield->unsetUseBit(blockIndex);
  70. }
  71. int32_t Piece::getMissingUnusedBlockIndex() const {
  72. int32_t blockIndex = bitfield->getFirstMissingUnusedIndex();
  73. if(blockIndex == -1) {
  74. return blockIndex;
  75. }
  76. bitfield->setUseBit(blockIndex);
  77. return blockIndex;
  78. }
  79. int32_t Piece::getMissingBlockIndex() const {
  80. int32_t blockIndex = bitfield->getMissingIndex();
  81. if(blockIndex == -1) {
  82. return blockIndex;
  83. }
  84. bitfield->setUseBit(blockIndex);
  85. return blockIndex;
  86. }
  87. int32_t Piece::getFirstMissingBlockIndexWithoutLock() const
  88. {
  89. return bitfield->getFirstMissingIndex();
  90. }
  91. BlockIndexes Piece::getAllMissingBlockIndexes() const {
  92. return bitfield->getAllMissingIndexes();
  93. }
  94. string Piece::toString() const {
  95. return "piece: index="+Util::itos(index)+", length="+Util::itos(length);
  96. }
  97. void Piece::reconfigure(int32_t length)
  98. {
  99. delete bitfield;
  100. this->length = length;
  101. bitfield =
  102. BitfieldManFactory::getFactoryInstance()->createBitfieldMan(_blockLength, length);
  103. }
  104. void Piece::setBitfield(const unsigned char* bitfield, int32_t len)
  105. {
  106. this->bitfield->setBitfield(bitfield, len);
  107. }
  108. void Piece::addSubPiece(const PieceHandle& subPiece)
  109. {
  110. _subPieces.push_back(subPiece);
  111. }
  112. PieceHandle Piece::getSubPiece(int32_t blockIndex)
  113. {
  114. Pieces::iterator itr = getSubPieceIterator(blockIndex);
  115. if(itr == _subPieces.end()) {
  116. return 0;
  117. } else {
  118. return *itr;
  119. }
  120. }
  121. void Piece::removeSubPiece(int32_t blockIndex)
  122. {
  123. Pieces::iterator itr = getSubPieceIterator(blockIndex);
  124. if(itr != _subPieces.end()) {
  125. _subPieces.erase(itr);
  126. }
  127. }
  128. Pieces::iterator Piece::getSubPieceIterator(int32_t blockIndex)
  129. {
  130. for(Pieces::iterator itr = _subPieces.begin(); itr != _subPieces.end(); ++itr) {
  131. if((*itr)->getIndex() == blockIndex) {
  132. return itr;
  133. }
  134. }
  135. return _subPieces.end();
  136. }
  137. bool Piece::isRangeComplete(int32_t offset, int32_t length)
  138. {
  139. int32_t startIndex = START_INDEX(offset, _blockLength);
  140. int32_t endIndex = END_INDEX(offset, length, _blockLength);
  141. if(countBlock() <= endIndex) {
  142. endIndex = countBlock()-1;
  143. }
  144. if(startIndex+1 < endIndex) {
  145. if(!bitfield->isBitRangeSet(startIndex+1, endIndex-1)) {
  146. return false;
  147. }
  148. }
  149. if(startIndex == endIndex) {
  150. if(hasBlock(startIndex)) {
  151. return true;
  152. }
  153. PieceHandle subPiece = getSubPiece(startIndex);
  154. if(subPiece.isNull()) {
  155. return false;
  156. }
  157. return subPiece->isRangeComplete(offset, length);
  158. } else {
  159. if(!hasBlock(startIndex)) {
  160. PieceHandle subPiece = getSubPiece(startIndex);
  161. if(subPiece.isNull()) {
  162. return false;
  163. }
  164. if(!subPiece->isRangeComplete(offset-startIndex*_blockLength, length)) {
  165. return false;
  166. }
  167. }
  168. if(!hasBlock(endIndex)) {
  169. PieceHandle subPiece = getSubPiece(endIndex);
  170. if(subPiece.isNull()) {
  171. return false;
  172. }
  173. if(!subPiece->isRangeComplete(0, offset+length-endIndex*_blockLength)) {
  174. return false;
  175. }
  176. }
  177. return true;
  178. }
  179. }
  180. int32_t Piece::getCompletedLength()
  181. {
  182. int32_t length = 0;
  183. for(int32_t i = 0; i < countBlock(); ++i) {
  184. if(hasBlock(i)) {
  185. length += getBlockLength(i);
  186. } else {
  187. PieceHandle subPiece = getSubPiece(i);
  188. if(!subPiece.isNull()) {
  189. length += subPiece->getCompletedLength();
  190. }
  191. }
  192. }
  193. return length;
  194. }