summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/filetransfer/util/ChunkList.java
diff options
context:
space:
mode:
authorSimon Rettberg2015-07-23 17:42:30 +0200
committerSimon Rettberg2015-07-23 17:42:30 +0200
commitbacb060ba31fafebd8c0f15d0b6704732a37b482 (patch)
treea20be56f5d24fe8e242cd1fbb0589826bf88dc7f /src/main/java/org/openslx/filetransfer/util/ChunkList.java
parentVm meta parser: Add virtualizer getter, change type of description (diff)
downloadmaster-sync-shared-bacb060ba31fafebd8c0f15d0b6704732a37b482.tar.gz
master-sync-shared-bacb060ba31fafebd8c0f15d0b6704732a37b482.tar.xz
master-sync-shared-bacb060ba31fafebd8c0f15d0b6704732a37b482.zip
ChunkList.getMissing() now blocks for a while if there are still pending blocks
Added HashChecker class to verify checksums of blocks
Diffstat (limited to 'src/main/java/org/openslx/filetransfer/util/ChunkList.java')
-rw-r--r--src/main/java/org/openslx/filetransfer/util/ChunkList.java70
1 files changed, 43 insertions, 27 deletions
diff --git a/src/main/java/org/openslx/filetransfer/util/ChunkList.java b/src/main/java/org/openslx/filetransfer/util/ChunkList.java
index 9154dc8..88d40cf 100644
--- a/src/main/java/org/openslx/filetransfer/util/ChunkList.java
+++ b/src/main/java/org/openslx/filetransfer/util/ChunkList.java
@@ -7,9 +7,10 @@ import java.util.List;
import org.apache.log4j.Logger;
-public class ChunkList {
+public class ChunkList
+{
- private static final Logger LOGGER = Logger.getLogger(ChunkList.class);
+ private static final Logger LOGGER = Logger.getLogger( ChunkList.class );
/**
* Chunks that are missing from the file
@@ -21,44 +22,53 @@ public class ChunkList {
*/
private final List<FileChunk> pendingChunks = new LinkedList<>();
- private final List<FileChunk> completeChunks = new ArrayList<>(100);
+ private final List<FileChunk> completeChunks = new ArrayList<>( 100 );
// 0 = complete, 1 = missing, 2 = uploading, 3 = queued for copying, 4 = copying
private final ByteBuffer statusArray;
// Do we need to keep valid chunks, or chunks that failed too many times?
- public ChunkList(long fileSize, List<ByteBuffer> sha1Sums) {
- FileChunk.createChunkList(missingChunks, fileSize, sha1Sums);
- statusArray = ByteBuffer.allocate(missingChunks.size());
+ public ChunkList( long fileSize, List<ByteBuffer> sha1Sums )
+ {
+ FileChunk.createChunkList( missingChunks, fileSize, sha1Sums );
+ statusArray = ByteBuffer.allocate( missingChunks.size() );
}
/**
* Get a missing chunk, marking it pending.
*
* @return chunk marked as missing
+ * @throws InterruptedException
*/
- public synchronized FileChunk getMissing() {
- if (missingChunks.isEmpty())
+ public synchronized FileChunk getMissing() throws InterruptedException
+ {
+ if ( missingChunks.isEmpty() && pendingChunks.isEmpty() )
return null;
- FileChunk c = missingChunks.remove(0);
- pendingChunks.add(c);
+ if ( missingChunks.isEmpty() ) {
+ this.wait( 3000 );
+ if ( missingChunks.isEmpty() )
+ return null;
+ }
+ FileChunk c = missingChunks.remove( 0 );
+ pendingChunks.add( c );
return c;
}
/**
* Get the block status as byte representation.
*/
- public synchronized ByteBuffer getStatusArray() {
+ public synchronized ByteBuffer getStatusArray()
+ {
byte[] array = statusArray.array();
//Arrays.fill(array, (byte)0);
- for (FileChunk c : missingChunks) {
+ for ( FileChunk c : missingChunks ) {
array[c.getChunkIndex()] = 1;
}
- for (FileChunk c : pendingChunks) {
+ for ( FileChunk c : pendingChunks ) {
array[c.getChunkIndex()] = 2;
}
- for (FileChunk c : completeChunks) {
+ for ( FileChunk c : completeChunks ) {
array[c.getChunkIndex()] = 0;
}
return statusArray;
@@ -69,8 +79,9 @@ public class ChunkList {
*
* @return List containing all successfully transfered chunks
*/
- public synchronized List<FileChunk> getCompleted() {
- return new ArrayList<>(completeChunks);
+ public synchronized List<FileChunk> getCompleted()
+ {
+ return new ArrayList<>( completeChunks );
}
/**
@@ -78,13 +89,15 @@ public class ChunkList {
*
* @param c The chunk in question
*/
- public synchronized void markSuccessful(FileChunk c) {
- if (!pendingChunks.remove(c)) {
- LOGGER.warn("Inconsistent state: markTransferred called for Chunk " + c.toString()
- + ", but chunk is not marked as currently transferring!");
+ public synchronized void markSuccessful( FileChunk c )
+ {
+ if ( !pendingChunks.remove( c ) ) {
+ LOGGER.warn( "Inconsistent state: markTransferred called for Chunk " + c.toString()
+ + ", but chunk is not marked as currently transferring!" );
return;
}
- completeChunks.add(c);
+ completeChunks.add( c );
+ this.notifyAll();
}
/**
@@ -95,18 +108,21 @@ public class ChunkList {
* @param c The chunk in question
* @return Number of times transfer of this chunk failed
*/
- public synchronized int markFailed(FileChunk c) {
- if (!pendingChunks.remove(c)) {
- LOGGER.warn("Inconsistent state: markTransferred called for Chunk " + c.toString()
- + ", but chunk is not marked as currently transferring!");
+ public synchronized int markFailed( FileChunk c )
+ {
+ if ( !pendingChunks.remove( c ) ) {
+ LOGGER.warn( "Inconsistent state: markTransferred called for Chunk " + c.toString()
+ + ", but chunk is not marked as currently transferring!" );
return -1;
}
// Add as first element so it will be re-transmitted immediately
- missingChunks.add(0, c);
+ missingChunks.add( 0, c );
+ this.notifyAll();
return c.incFailed();
}
- public synchronized boolean isComplete() {
+ public synchronized boolean isComplete()
+ {
return missingChunks.isEmpty() && pendingChunks.isEmpty();
}