break;
}else{
try{
DiskManagerCheckRequest request = disk_manager.createCheckRequest( i, null );
request.setLowPriority( true );
checker.enqueueCheckRequest(
request,
new DiskManagerCheckRequestListener()
{
public void
checkCompleted(
DiskManagerCheckRequest request,
boolean passed )
{
if ( TEST_RECHECK_FAILURE_HANDLING && (int)(Math.random()*10) == 0 ){
disk_manager.getPiece(request.getPieceNumber()).setDone(false);
passed = false;
}
if ( !passed ){
synchronized( failed_pieces ){
failed_pieces.add( request );
}
}
complete();
}
public void
checkCancelled(
DiskManagerCheckRequest request )
{
complete();
}
public void
checkFailed(
DiskManagerCheckRequest request,
Throwable cause )
{
complete();
}
protected void
complete()
{
run_sem.release();
pending_checks_sem.release();
}
});
pending_check_num++;
}catch( Throwable e ){
Debug.printStackTrace(e);
}
}
}
}
}
while( pending_check_num > 0 ){
pending_checks_sem.reserve();
pending_check_num--;
}
if ( partialPieces != null ){
Iterator iter = partialPieces.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry key = (Map.Entry)iter.next();
int pieceNumber = Integer.parseInt((String)key.getKey());
DiskManagerPiece dm_piece = pieces[ pieceNumber ];
if ( !dm_piece.isDone()){
List blocks = (List)partialPieces.get(key.getKey());
Iterator iterBlock = blocks.iterator();
while (iterBlock.hasNext()) {
dm_piece.setWritten(((Long)iterBlock.next()).intValue());
}
}
}
}
}else{
// resume not enabled, recheck everything
for (int i = 0; i < pieces.length; i++){
check_position = i;
disk_manager.setPercentDone(((i + 1) * 1000) / disk_manager.getNbPieces() );
boolean pieceCannotExist = false;
// check if there is an underlying file for this piece, if not set it to not done
DMPieceList list = disk_manager.getPieceList(i);
for (int j=0;j<list.size();j++){
DMPieceMapEntry entry = list.get(j);
Long file_size = (Long)file_sizes.get(entry.getFile());
if ( file_size == null ){
pieceCannotExist = true;
break;
}
long expected_size = entry.getOffset() + entry.getLength();
if ( file_size.longValue() < expected_size ){
pieceCannotExist = true;
break;
}
}
if(pieceCannotExist)
{
disk_manager.getPiece(i).setDone(false);
continue;
}
run_sem.reserve();
while( ! stopped ){
if ( recheck_inst.getPermission()){
break;
}
}
if ( stopped ){
break;
}
try{
DiskManagerCheckRequest request = disk_manager.createCheckRequest( i, null );
request.setLowPriority( true );
checker.enqueueCheckRequest(
request,
new DiskManagerCheckRequestListener()
{
public void
checkCompleted(
DiskManagerCheckRequest request,
boolean passed )
{
if ( TEST_RECHECK_FAILURE_HANDLING && (int)(Math.random()*10) == 0 ){
disk_manager.getPiece(request.getPieceNumber()).setDone(false);
passed = false;
}
if ( !passed ){
synchronized( failed_pieces ){
failed_pieces.add( request );
}
}
complete();
}
public void
checkCancelled(
DiskManagerCheckRequest request )
{
complete();
}
public void
checkFailed(
DiskManagerCheckRequest request,
Throwable cause )
{
complete();
}
protected void
complete()
{
run_sem.release();
pending_checks_sem.release();
}
});
pending_check_num++;
}catch( Throwable e ){
Debug.printStackTrace(e);
}
}
while( pending_check_num > 0 ){
pending_checks_sem.reserve();
pending_check_num--;
}
}
if ( failed_pieces.size() > 0 && !TEST_RECHECK_FAILURE_HANDLING ){
byte[][] piece_hashes = disk_manager.getTorrent().getPieces();
ByteArrayHashMap<Integer> hash_map = new ByteArrayHashMap<Integer>();
for ( int i=0;i<piece_hashes.length;i++){
hash_map.put( piece_hashes[i], i );
}
for ( DiskManagerCheckRequest request: failed_pieces ){
while( ! stopped ){
if ( recheck_inst.getPermission()){
break;
}
}
if ( stopped ){
break;
}
byte[] hash = request.getHash();
if ( hash != null ){
final Integer target_index = hash_map.get( hash );
int current_index = request.getPieceNumber();
int piece_size = disk_manager.getPieceLength( current_index );
if ( target_index != null &&
target_index != current_index &&
disk_manager.getPieceLength( target_index ) == piece_size &&
!disk_manager.isDone( target_index )){
final AESemaphore sem = new AESemaphore( "PieceReorder" );
disk_manager.enqueueReadRequest(
disk_manager.createReadRequest( current_index, 0, piece_size ),
new DiskManagerReadRequestListener()
{
public void
readCompleted(
DiskManagerReadRequest request,
DirectByteBuffer data )
{
try{
disk_manager.enqueueWriteRequest(
disk_manager.createWriteRequest( target_index, 0, data, null ),
new DiskManagerWriteRequestListener()
{
public void
writeCompleted(
DiskManagerWriteRequest request )
{
try{
DiskManagerCheckRequest check_request = disk_manager.createCheckRequest( target_index, null );
check_request.setLowPriority( true );
checker.enqueueCheckRequest(
check_request,
new DiskManagerCheckRequestListener()
{