Piece.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #ifndef _D_PIECE_H_
  23. #define _D_PIECE_H_
  24. #include "BitfieldMan.h"
  25. #include "common.h"
  26. #define BLOCK_LENGTH (16*1024)
  27. class Piece {
  28. private:
  29. int index;
  30. int length;
  31. BitfieldMan* bitfield;
  32. public:
  33. Piece():index(0), length(0), bitfield(NULL) {}
  34. Piece(int index, int length):index(index), length(length) {
  35. bitfield = new BitfieldMan(BLOCK_LENGTH, length);
  36. }
  37. Piece(const Piece& piece) {
  38. index = piece.index;
  39. length = piece.length;
  40. if(piece.bitfield == NULL) {
  41. bitfield = NULL;
  42. } else {
  43. bitfield = new BitfieldMan(*piece.bitfield);
  44. }
  45. }
  46. ~Piece() {
  47. delete bitfield;
  48. }
  49. Piece& operator=(const Piece& piece) {
  50. if(this != &piece) {
  51. index = piece.index;
  52. length = piece.length;
  53. if(bitfield != NULL) {
  54. delete bitfield;
  55. }
  56. if(piece.bitfield == NULL) {
  57. bitfield = NULL;
  58. } else {
  59. bitfield = new BitfieldMan(*piece.bitfield);
  60. }
  61. }
  62. return *this;
  63. }
  64. bool operator==(const Piece& piece) const {
  65. return index == piece.index;
  66. }
  67. int getMissingUnusedBlockIndex() const;
  68. int getMissingBlockIndex() const;
  69. BlockIndexes getAllMissingBlockIndexes() const;
  70. void completeBlock(int blockIndex);
  71. void cancelBlock(int blockIndex);
  72. int countCompleteBlock() const {
  73. return bitfield->countBlock()-bitfield->countMissingBlock();
  74. }
  75. bool hasBlock(int blockIndex) const {
  76. return bitfield->isBitSet(blockIndex);
  77. }
  78. /**
  79. * Returns true if all blocks of this piece have been downloaded, otherwise
  80. * returns false.
  81. */
  82. bool pieceComplete() const;
  83. int countBlock() const { return bitfield->countBlock(); }
  84. int getBlockLength(int index) const {
  85. return bitfield->getBlockLength(index);
  86. }
  87. int getBlockLength() const { return bitfield->getBlockLength(); }
  88. int getIndex() const { return index; }
  89. void setIndex(int index) { this->index = index; }
  90. int getLength() const { return length; }
  91. void setLength(int index) { this->length = length; }
  92. const unsigned char* getBitfield() const { return bitfield->getBitfield(); }
  93. void setBitfield(const unsigned char* bitfield, int len);
  94. void clearAllBlock();
  95. void setAllBlock();
  96. static Piece nullPiece;
  97. static bool isNull(const Piece& piece) {
  98. return piece.index == 0 && piece.length == 0;
  99. }
  100. };
  101. #endif // _D_PIECE_H_