<?php
/**
* Describes a torrent piece
*
* @author Charalampos Pournaris
*/
class TorrentPiece {
/**
* The offset that this piece starts in a file
*
* @var integer
*/
private $start_offset = 0;
/**
* The offset that this piece ends in a file
*
* @var integer
*/
private $end_offset = 0;
/**
* Piece id (incremental index)
*
* @var integer
*/
private $piece_id = 0;
/**
* If it's a shared piece, remaining bytes to complete a piece
*
* @var integer
*/
private $remaining = 0;
/**
* This piece length size in bytes
*
* @var integer
*/
private $piece_length = 0;
/**
* It is a full piece
*
* @var boolean
*/
private $is_full = false;
/**
* It is a partial piece
*
* @var boolean
*/
private $is_shared = false;
/**
* Piece size used to create the torrent file
*
* @var integer
*/
public static $piece_size = 0;
/**
* Create a new torrent piece object
*
* @param integer $piece_id
* @param integer $start_offset
* @param integer $end_offset
* @param boolean $is_full
* @param boolean $is_shared
*/
public function __construct($piece_id, $start_offset, $end_offset, $is_full=false, $is_shared=false) {
$this->start_offset = $start_offset;
$this->end_offset = $end_offset;
$this->piece_id = $piece_id;
$this->is_full = $is_full;
$this->is_shared = $is_shared;
$this->piece_length = $end_offset - $start_offset;
$this->remaining = self::$piece_size - $this->piece_length;
}
/**
* Returns if this is a full piece
*
* @return boolean
*/
public function isFullPiece() {
return $this->is_full;
}
/**
* Returns if this a partial piece
*
* @return boolean
*/
public function isPartialPiece() {
return $this->is_shared;
}
/**
* Set the number of bytes remaining to complete a piece
*
* @param integer $remaining
*/
public function setRemaining($remaining) {
$this->remaining = $remaining;
}
/**
* Returns this piece's id
*
* @return integer
*/
public function getPieceId() {
return $this->piece_id;
}
/**
* Return the start offset
*
* @return integer
*/
public function getStartOffset() {
return $this->start_offset;
}
/**
* Return the end offset
*
* @return integer
*/
public function getEndOffset() {
return $this->end_offset;
}
/**
* Get the number of bytes remaining to complete a piece
*
* @return integer
*/
public function getRemaining() {
return $this->remaining;
}
/**
* Get this piece's length size in bytes
*
* @return integer
*/
public function getPieceLength() {
return $this->piece_length;
}
}
?>