Piece.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. ~Piece() {
  39. delete bitfield;
  40. }
  41. Piece& operator=(const Piece& piece);
  42. bool operator==(const Piece& piece) const;
  43. int getMissingUnusedBlockIndex() const;
  44. int getMissingBlockIndex() const;
  45. BlockIndexes getAllMissingBlockIndexes() const;
  46. void completeBlock(int blockIndex);
  47. void cancelBlock(int blockIndex);
  48. int countCompleteBlock() const {
  49. return bitfield->countBlock()-bitfield->countMissingBlock();
  50. }
  51. bool hasBlock(int blockIndex) const {
  52. return bitfield->isBitSet(blockIndex);
  53. }
  54. bool pieceComplete() const;
  55. int countBlock() const { return bitfield->countBlock(); }
  56. int getBlockLength(int index) const {
  57. return bitfield->getBlockLength(index);
  58. }
  59. int getBlockLength() const { return bitfield->getBlockLength(); }
  60. int getIndex() const { return index; }
  61. void setIndex(int index) { this->index = index; }
  62. int getLength() const { return length; }
  63. void setLength(int index) { this->length = length; }
  64. const unsigned char* getBitfield() const { return bitfield->getBitfield(); }
  65. void setBitfield(const unsigned char* bitfield, int len);
  66. void clearAllBlock();
  67. void setAllBlock();
  68. static Piece nullPiece;
  69. static bool isNull(const Piece& piece) {
  70. return piece.index == 0 && piece.length == 0;
  71. }
  72. };
  73. #endif // _D_PIECE_H_