From 75fb574803e7b290aaae7a9d3a379bcc2de9a614 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 21 Jul 2015 15:44:24 +0200 Subject: [client] Continue refactoring Upload and Download Task --- .../openslx/bwlp/sat/database/mappers/DbImage.java | 2 +- .../openslx/bwlp/sat/fileserv/ActiveUpload.java | 2 + .../org/openslx/bwlp/sat/fileserv/ChunkList.java | 113 --------------------- .../org/openslx/bwlp/sat/fileserv/FileChunk.java | 77 -------------- .../org/openslx/bwlp/sat/fileserv/FileServer.java | 1 + .../java/org/openslx/bwlp/sat/util/Constants.java | 2 +- 6 files changed, 5 insertions(+), 192 deletions(-) delete mode 100644 dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/ChunkList.java delete mode 100644 dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/FileChunk.java (limited to 'dozentenmodulserver/src/main/java') diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java index 6894c85f..adf0f26c 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java @@ -11,7 +11,6 @@ import org.openslx.bwlp.sat.database.Database; import org.openslx.bwlp.sat.database.MysqlConnection; import org.openslx.bwlp.sat.database.MysqlStatement; import org.openslx.bwlp.sat.database.Paginator; -import org.openslx.bwlp.sat.fileserv.ChunkList; import org.openslx.bwlp.thrift.iface.ImageBaseWrite; import org.openslx.bwlp.thrift.iface.ImageDetailsRead; import org.openslx.bwlp.thrift.iface.ImagePermissions; @@ -21,6 +20,7 @@ import org.openslx.bwlp.thrift.iface.ImageVersionWrite; import org.openslx.bwlp.thrift.iface.ShareMode; import org.openslx.bwlp.thrift.iface.TNotFoundException; import org.openslx.bwlp.thrift.iface.UserInfo; +import org.openslx.filetransfer.util.ChunkList; public class DbImage { diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/ActiveUpload.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/ActiveUpload.java index da15656d..08d0d30f 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/ActiveUpload.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/ActiveUpload.java @@ -24,6 +24,8 @@ import org.openslx.filetransfer.DataReceivedCallback; import org.openslx.filetransfer.Downloader; import org.openslx.filetransfer.FileRange; import org.openslx.filetransfer.WantRangeCallback; +import org.openslx.filetransfer.util.ChunkList; +import org.openslx.filetransfer.util.FileChunk; public class ActiveUpload { private static final Logger LOGGER = Logger.getLogger(ActiveUpload.class); diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/ChunkList.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/ChunkList.java deleted file mode 100644 index 385a6484..00000000 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/ChunkList.java +++ /dev/null @@ -1,113 +0,0 @@ -package org.openslx.bwlp.sat.fileserv; - -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; - -import org.apache.log4j.Logger; - -public class ChunkList { - - private static final Logger LOGGER = Logger.getLogger(ChunkList.class); - - /** - * Chunks that are missing from the file - */ - private final List missingChunks = new LinkedList<>(); - - /** - * Chunks that are currently being uploaded or hash-checked - */ - private final List pendingChunks = new LinkedList<>(); - - private final List 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 sha1Sums) { - FileChunk.createChunkList(missingChunks, fileSize, sha1Sums); - statusArray = ByteBuffer.allocate(missingChunks.size()); - } - - /** - * Get a missing chunk, marking it pending. - * - * @return chunk marked as missing - */ - public synchronized FileChunk getMissing() { - 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() { - byte[] array = statusArray.array(); - //Arrays.fill(array, (byte)0); - for (FileChunk c : missingChunks) { - array[c.getChunkIndex()] = 1; - } - for (FileChunk c : pendingChunks) { - array[c.getChunkIndex()] = 2; - } - for (FileChunk c : completeChunks) { - array[c.getChunkIndex()] = 0; - } - return statusArray; - } - - /** - * Get completed chunks as list - * - * @return List containing all successfully transfered chunks - */ - public synchronized List getCompleted() { - return new ArrayList<>(completeChunks); - } - - /** - * Mark a chunk currently transferring as successfully transfered. - * - * @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!"); - return; - } - completeChunks.add(c); - } - - /** - * Mark a chunk currently transferring or being hash checked as failed - * transfer. This increases its fail count and re-adds it to the list of - * missing chunks. - * - * @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!"); - return -1; - } - // Add as first element so it will be re-transmitted immediately - missingChunks.add(0, c); - return c.incFailed(); - } - - public synchronized boolean isComplete() { - return missingChunks.isEmpty() && pendingChunks.isEmpty(); - } - -} diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/FileChunk.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/FileChunk.java deleted file mode 100644 index b322e65d..00000000 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/FileChunk.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.openslx.bwlp.sat.fileserv; - -import java.nio.ByteBuffer; -import java.util.Collection; -import java.util.List; - -import org.openslx.filetransfer.FileRange; - -public class FileChunk { - - public static final int CHUNK_SIZE_MIB = 16; - public static final int CHUNK_SIZE = CHUNK_SIZE_MIB * (1024 * 1024); - - public final FileRange range; - public final byte[] sha1sum; - private int failCount = 0; - - public FileChunk(long startOffset, long endOffset, byte[] sha1sum) { - this.range = new FileRange(startOffset, endOffset); - this.sha1sum = sha1sum; - } - - /** - * Signal that transferring this chunk seems to have failed (checksum - * mismatch). - * - * @return Number of times the transfer failed now - */ - public synchronized int incFailed() { - return ++failCount; - } - - public int getChunkIndex() { - return (int)(range.startOffset / CHUNK_SIZE); - } - - @Override - public String toString() { - return "[Chunk " + getChunkIndex() + " (" + range.startOffset + "-" + range.endOffset + "), fails: " + failCount + "]"; - } - - // - - public static int fileSizeToChunkCount(long fileSize) { - return (int) ((fileSize + CHUNK_SIZE - 1) / CHUNK_SIZE); - } - - public static void createChunkList(Collection list, long fileSize, List sha1Sums) { - if (fileSize < 0) - throw new IllegalArgumentException("fileSize cannot be negative"); - if (!list.isEmpty()) - throw new IllegalArgumentException("Passed list is not empty"); - long chunkCount = fileSizeToChunkCount(fileSize); - if (sha1Sums != null) { - if (sha1Sums.size() != chunkCount) - throw new IllegalArgumentException( - "Passed a sha1sum list, but hash count in list doesn't match expected chunk count"); - long offset = 0; - for (ByteBuffer sha1sum : sha1Sums) { // Do this as we don't know how efficient List.get(index) is... - long end = offset + CHUNK_SIZE; - if (end > fileSize) - end = fileSize; - list.add(new FileChunk(offset, end, sha1sum.array())); - offset = end; - } - return; - } - long offset = 0; - while (offset < fileSize) { // ...otherwise we could share this code - long end = offset + CHUNK_SIZE; - if (end > fileSize) - end = fileSize; - list.add(new FileChunk(offset, end, null)); - offset = end; - } - } -} diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/FileServer.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/FileServer.java index 93089b5a..3abc5f98 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/FileServer.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/FileServer.java @@ -68,6 +68,7 @@ public class FileServer implements IncomingEvent { ActiveUpload upload = uploads.get(token); if (upload == null) { LOGGER.warn("Unknown token " + token); + downloader.cancel(); return; } upload.addConnection(downloader, transferPool); diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/util/Constants.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/util/Constants.java index 8216fd86..ad5ea0e2 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/util/Constants.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/util/Constants.java @@ -1,6 +1,6 @@ package org.openslx.bwlp.sat.util; -import org.openslx.bwlp.sat.fileserv.FileChunk; +import org.openslx.filetransfer.util.FileChunk; public class Constants { public static final String INCOMPLETE_UPLOAD_SUFFIX = ".part"; -- cgit v1.2.3-55-g7522