Piece.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. bool Piece::operator<(const Piece& piece) const
  78. {
  79. return index < piece.index;
  80. }
  81. void Piece::completeBlock(size_t blockIndex) {
  82. bitfield->setBit(blockIndex);
  83. bitfield->unsetUseBit(blockIndex);
  84. }
  85. void Piece::clearAllBlock() {
  86. bitfield->clearAllBit();
  87. bitfield->clearAllUseBit();
  88. }
  89. void Piece::setAllBlock() {
  90. bitfield->setAllBit();
  91. }
  92. bool Piece::pieceComplete() const {
  93. return bitfield->isAllBitSet();
  94. }
  95. size_t Piece::countBlock() const
  96. {
  97. return bitfield->countBlock();
  98. }
  99. size_t Piece::getBlockLength(size_t index) const
  100. {
  101. return bitfield->getBlockLength(index);
  102. }
  103. size_t Piece::getBlockLength() const
  104. {
  105. return bitfield->getBlockLength();
  106. }
  107. const unsigned char* Piece::getBitfield() const
  108. {
  109. return bitfield->getBitfield();
  110. }
  111. size_t Piece::getBitfieldLength() const
  112. {
  113. return bitfield->getBitfieldLength();
  114. }
  115. bool Piece::isBlockUsed(size_t index) const
  116. {
  117. return bitfield->isUseBitSet(index);
  118. }
  119. void Piece::cancelBlock(size_t blockIndex) {
  120. bitfield->unsetUseBit(blockIndex);
  121. }
  122. size_t Piece::countCompleteBlock() const
  123. {
  124. return bitfield->countBlock()-bitfield->countMissingBlock();
  125. }
  126. bool Piece::hasBlock(size_t blockIndex) const
  127. {
  128. return bitfield->isBitSet(blockIndex);
  129. }
  130. bool Piece::getMissingUnusedBlockIndex(size_t& index) const
  131. {
  132. if(bitfield->getFirstMissingUnusedIndex(index)) {
  133. bitfield->setUseBit(index);
  134. return true;
  135. } else {
  136. return false;
  137. }
  138. }
  139. bool Piece::getMissingBlockIndex(size_t& index) const
  140. {
  141. if(bitfield->getMissingIndex(index)) {
  142. bitfield->setUseBit(index);
  143. return true;
  144. } else {
  145. return false;
  146. }
  147. }
  148. bool Piece::getFirstMissingBlockIndexWithoutLock(size_t& index) const
  149. {
  150. return bitfield->getFirstMissingIndex(index);
  151. }
  152. bool Piece::getAllMissingBlockIndexes(std::deque<size_t>& indexes) const {
  153. return bitfield->getAllMissingIndexes(indexes);
  154. }
  155. std::string Piece::toString() const {
  156. return "piece: index="+Util::itos(index)+", length="+Util::itos(length);
  157. }
  158. void Piece::reconfigure(size_t length)
  159. {
  160. delete bitfield;
  161. this->length = length;
  162. bitfield =
  163. BitfieldManFactory::getFactoryInstance()->createBitfieldMan(_blockLength, length);
  164. }
  165. void Piece::setBitfield(const unsigned char* bitfield, size_t len)
  166. {
  167. this->bitfield->setBitfield(bitfield, len);
  168. }
  169. size_t Piece::getCompletedLength()
  170. {
  171. return bitfield->getCompletedLength();
  172. }
  173. } // namespace aria2