Methods Summary |
---|
protected void | addWrite(PEPieceWriteImpl write)
try{
class_mon.enter();
writes.add(write);
}finally{
class_mon.exit();
}
|
public void | addWrite(int blockNumber, java.lang.String sender, byte[] hash, boolean correct)
addWrite( new PEPieceWriteImpl( blockNumber, sender, hash, correct ));
|
public void | checkRequests()This will scan each block looking for requested blocks. For each one, it'll verify
if the PEPeer for it still exists and is still willing and able to upload data.
If not, it'll unmark the block as requested.
if ( getTimeSinceLastActivity() < 30*1000 ){
return;
}
int cleared = 0;
for (int i=0; i<nbBlocks; i++){
if (!downloaded[i] &&!dmPiece.isWritten(i)){
final String requester = requested[i];
if ( requester != null ){
if ( !manager.requestExists(
requester,
getPieceNumber(),
i *DiskManager.BLOCK_SIZE,
getBlockSize( i ))){
clearRequested(i);
cleared++;
}
}
}
}
if ( cleared > 0 ){
if (Logger.isEnabled())
Logger.log(new LogEvent(dmPiece.getManager().getTorrent(), LOGID, LogEvent.LT_WARNING,
"checkRequests(): piece #" +getPieceNumber()+" cleared " +cleared +" requests" ));
}else{
if ( fully_requested && getNbUnrequested() > 0 ){
if (Logger.isEnabled())
Logger.log(new LogEvent(dmPiece.getManager().getTorrent(), LOGID, LogEvent.LT_WARNING,
"checkRequests(): piece #" +getPieceNumber()+" reset fully requested" ));
fully_requested = false;
}
}
|
public void | clearDownloaded(int offset)This flags the block at the given offset as NOT having been downloaded
and the whole piece as not having been fully downloaded
downloaded[offset /DiskManager.BLOCK_SIZE] =false;
fully_downloaded = false;
|
public void | clearRequested(int blockNumber)This method clears the requested information for the given block
unless the block has already been downloaded, in which case the writer's
IP is recorded as a request for the block.
requested[blockNumber] =downloaded[blockNumber] ?writers[blockNumber] :null;
fully_requested = false;
|
public void | getAndMarkBlock(PEPeer peer, int index)
requested[index] = peer.getIp();
if ( getNbUnrequested() <= 0 ){
setRequested();
}
|
public int[] | getAndMarkBlocks(PEPeer peer, int nbWanted, boolean enable_request_hints)This method scans a piece for the first unrequested block. Upon finding it,
it counts how many are unrequested up to nbWanted.
The blocks are marked as requested by the PEPeer
Assumption - single threaded access to this
TODO: this should return the largest span equal or smaller than nbWanted
OR, probably a different method should do that, so this one can support 'more sequential' picking
final String ip =peer.getIp();
final boolean[] written =dmPiece.getWritten();
int blocksFound =0;
if ( enable_request_hints ){
int[] request_hint = peer.getRequestHint();
if ( request_hint != null && request_hint[0] == dmPiece.getPieceNumber()){
// try to honour the hint first
int hint_block_start = request_hint[1] / DiskManager.BLOCK_SIZE;
int hint_block_count = ( request_hint[2] + DiskManager.BLOCK_SIZE-1 ) / DiskManager.BLOCK_SIZE;
for (int i =hint_block_start; i < nbBlocks && i <hint_block_start + hint_block_count; i++)
{
while (blocksFound <nbWanted &&(i +blocksFound) <nbBlocks &&!downloaded[i +blocksFound]
&&requested[i +blocksFound] ==null &&(written ==null ||!written[i]))
{
requested[i +blocksFound] =ip;
blocksFound++;
}
if (blocksFound >0){
return new int[] {i, blocksFound};
}
}
}
}
// scan piece to find first free block
for (int i =0; i <nbBlocks; i++)
{
while (blocksFound <nbWanted &&(i +blocksFound) <nbBlocks &&!downloaded[i +blocksFound]
&&requested[i +blocksFound] ==null &&(written ==null ||!written[i]))
{
requested[i +blocksFound] =ip;
blocksFound++;
}
if (blocksFound >0)
return new int[] {i, blocksFound};
}
return new int[] {-1, 0};
|
public int | getAvailability()
return manager.getAvailability(dmPiece.getPieceNumber());
|
public int | getBlockNumber(int offset)
return offset /DiskManager.BLOCK_SIZE;
|
public int | getBlockSize(int blockNumber)
if ( blockNumber == (nbBlocks - 1)){
int length = dmPiece.getLength();
if ((length % DiskManager.BLOCK_SIZE) != 0){
return( length % DiskManager.BLOCK_SIZE );
}
}
return DiskManager.BLOCK_SIZE;
|
public long | getCreationTime()
final long now =SystemTime.getCurrentTime();
if (now >=creationTime &&creationTime >0)
return creationTime;
creationTime =now;
return now;
|
public DiskManagerPiece | getDMPiece()
return dmPiece;
|
public boolean[] | getDownloaded()
return( downloaded );
|
public long | getLastDownloadTime(long now)
if (time_last_download <=now)
return time_last_download;
return time_last_download =now;
|
public int | getLength()
return dmPiece.getLength();
|
public PEPeerManager | getManager()
return manager;
|
public int | getNbBlocks()
return nbBlocks;
|
public int | getNbRequests()
int result =0;
for (int i =0; i <nbBlocks; i++)
{
if (!downloaded[i] &&requested[i] !=null)
result++;
}
return result;
|
public int | getNbUnrequested()
int result =0;
final boolean[] written =dmPiece.getWritten();
for (int i =0; i <nbBlocks; i++ )
{
if (!downloaded[i] &&requested[i] ==null &&(written ==null ||!written[i]))
result++;
}
return result;
|
public int | getNbWritten()This support method returns how many blocks have already been
written from the dmPiece
return dmPiece.getNbWritten();
|
public int | getPieceNumber()
return dmPiece.getPieceNumber();
|
public java.util.List | getPieceWrites()
List result;
try{
class_mon.enter();
result = new ArrayList(writes);
}finally{
class_mon.exit();
}
return result;
|
public java.util.List | getPieceWrites(int blockNumber)
final List result;
try{
class_mon.enter();
result = new ArrayList(writes);
}finally{
class_mon.exit();
}
final Iterator iter = result.iterator();
while(iter.hasNext()) {
final PEPieceWriteImpl write = (PEPieceWriteImpl) iter.next();
if(write.getBlockNumber() != blockNumber)
iter.remove();
}
return result;
|
public java.util.List | getPieceWrites(PEPeer peer)
final List result;
try{
class_mon.enter();
result = new ArrayList(writes);
}finally{
class_mon.exit();
}
final Iterator iter = result.iterator();
while(iter.hasNext()) {
PEPieceWriteImpl write = (PEPieceWriteImpl) iter.next();
if(peer == null || ! peer.getIp().equals(write.getSender()))
iter.remove();
}
return result;
|
public java.util.List | getPieceWrites(java.lang.String ip)
final List result;
try{
class_mon.enter();
result = new ArrayList(writes);
}finally{
class_mon.exit();
}
final Iterator iter = result.iterator();
while(iter.hasNext()) {
final PEPieceWriteImpl write = (PEPieceWriteImpl) iter.next();
if ( !write.getSender().equals( ip )){
iter.remove();
}
}
return result;
|
public java.lang.Object | getRealTimeData()
return( real_time_data );
|
public java.lang.String | getReservedBy()
return reservedBy;
|
public int | getResumePriority()
return resumePriority;
|
public int | getSpeed()
return speed;
|
public java.lang.String | getString()
String text = "";
PiecePicker pp = manager.getPiecePicker();
text += ( isRequestable()?"reqable,":"" );
text += "req=" + getNbRequests() + ",";
text += ( isRequested()?"reqstd,":"" );
text += ( isDownloaded()?"downed,":"" );
text += ( getReservedBy()!=null?"resrv,":"" );
text += "speed=" + getSpeed() + ",";
text += ( pp==null?("pri=" + getResumePriority()):pp.getPieceString(dmPiece.getPieceNumber()));
if ( text.endsWith(",")){
text = text.substring(0,text.length()-1);
}
return( text );
|
public long | getTimeSinceLastActivity()
final long now =SystemTime.getCurrentTime();
final long lastWriteTime =getLastDownloadTime(now);
if (time_last_download >0 &&now >=time_last_download)
return now -time_last_download;
if (creationTime >0 &&now >=creationTime)
return now -creationTime;
creationTime =now;
return 0;
|
public java.lang.String[] | getWriters()
return writers;
|
public boolean[] | getWritten()This support method returns the dmPiece's written array
return dmPiece.getWritten();
|
public boolean | hasUndownloadedBlock()
for (int i =0; i <nbBlocks; i++ ){
if (!downloaded[i]){
return( true );
}
}
return( false );
|
public boolean | hasUnrequestedBlock()
final boolean[] written =dmPiece.getWritten();
for (int i =0; i <nbBlocks; i++ )
{
if (!downloaded[i] &&requested[i] ==null &&(written ==null ||!written[i]))
return true;
}
return false;
|
public boolean | isDownloaded()
return( fully_downloaded );
|
public boolean | isDownloaded(int blockNumber)Tells if a block has been downloaded
return downloaded[blockNumber];
|
public boolean | isRequestable()
return( dmPiece.isDownloadable() && !( fully_downloaded || fully_requested ));
|
public boolean | isRequested()
return( fully_requested );
|
public boolean | isRequested(int blockNumber)Tells if a block has been requested
return requested[blockNumber] !=null;
|
public boolean | isWritten()
return dmPiece.isWritten();
|
public boolean | isWritten(int block)
return dmPiece.isWritten( block );
|
public void | reDownloadBlock(int blockNumber)for a block that's already downloadedt, mark up the piece
so that the block will get downloaded again. This is used
when the piece fails hash-checking.
downloaded[blockNumber] =false;
requested[blockNumber] =null;
fully_downloaded = false;
writers[blockNumber] = null;
dmPiece.reDownloadBlock(blockNumber);
|
public void | reDownloadBlocks(java.lang.String address)finds all blocks downloaded by the given address
and marks them up for re-downloading
for (int i =0; i <writers.length; i++ )
{
final String writer =writers[i];
if (writer !=null &&writer.equals(address))
reDownloadBlock(i);
}
|
public void | reset()
dmPiece.reset();
for (int i =0; i <nbBlocks; i++)
{
requested[i] =null;
downloaded[i] =false;
writers[i] =null;
}
fully_downloaded = false;
time_last_download = 0;
reservedBy =null;
real_time_data=null;
|
public void | setDownloaded(int offset)This flags the block at the given offset as having been downloaded
If all blocks are now downloaed, sets the dmPiece as downloaded
time_last_download =SystemTime.getCurrentTime();
downloaded[offset /DiskManager.BLOCK_SIZE] =true;
for (int i =0; i <nbBlocks; i++)
{
if (!downloaded[i])
return;
}
fully_downloaded = true;
fully_requested = false;
|
public void | setLastRequestedPeerSpeed(int peerSpeed)
// Up the speed on this piece?
if (peerSpeed > speed ){
speed++;
}
|
public void | setRealTimeData(java.lang.Object o)
real_time_data = o;
|
public void | setRequestable()
fully_downloaded = false;
fully_requested = false;
dmPiece.setDownloadable();
|
public void | setRequested()
fully_requested = true;
|
public boolean | setRequested(PEPeer peer, int blockNumber)Assumption - single threaded with getAndMarkBlock
if (!downloaded[blockNumber])
{
requested[blockNumber] =peer.getIp();
return true;
}
return false;
|
public void | setReservedBy(java.lang.String peer)
reservedBy =peer;
|
public void | setResumePriority(int p)
resumePriority =p;
|
public void | setSpeed(int newSpeed)
speed =newSpeed;
|
public void | setWritten(PEPeer peer, int blockNumber)This marks a given block as having been written by the given peer
writers[blockNumber] =peer.getIp();
dmPiece.setWritten(blockNumber);
|