diff options
author | Simon Rettberg | 2015-06-02 19:53:31 +0200 |
---|---|---|
committer | Simon Rettberg | 2015-06-02 19:53:31 +0200 |
commit | 1bc83891c68ee269727e81a13cc70da698bcc7a7 (patch) | |
tree | b052a72ad7d65864068752f71c5ed2b49a171276 /dozentenmodulserver/src/main/java/fileserv/ActiveUpload.java | |
parent | [server] Started work on the internal file server (diff) | |
download | tutor-module-1bc83891c68ee269727e81a13cc70da698bcc7a7.tar.gz tutor-module-1bc83891c68ee269727e81a13cc70da698bcc7a7.tar.xz tutor-module-1bc83891c68ee269727e81a13cc70da698bcc7a7.zip |
[server] Compiling again, still lots of stubs
Diffstat (limited to 'dozentenmodulserver/src/main/java/fileserv/ActiveUpload.java')
-rw-r--r-- | dozentenmodulserver/src/main/java/fileserv/ActiveUpload.java | 54 |
1 files changed, 44 insertions, 10 deletions
diff --git a/dozentenmodulserver/src/main/java/fileserv/ActiveUpload.java b/dozentenmodulserver/src/main/java/fileserv/ActiveUpload.java index cf73a413..256f8b8d 100644 --- a/dozentenmodulserver/src/main/java/fileserv/ActiveUpload.java +++ b/dozentenmodulserver/src/main/java/fileserv/ActiveUpload.java @@ -1,10 +1,11 @@ package fileserv; +import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; +import java.nio.ByteBuffer; import java.util.List; -import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ThreadPoolExecutor; import org.apache.log4j.Logger; @@ -12,6 +13,7 @@ import org.openslx.filetransfer.DataReceivedCallback; import org.openslx.filetransfer.Downloader; import org.openslx.filetransfer.FileRange; import org.openslx.filetransfer.WantRangeCallback; +import org.openslx.imagemaster.thrift.iface.UserInfo; public class ActiveUpload { private static final Logger LOGGER = Logger.getLogger(ActiveUpload.class); @@ -21,19 +23,28 @@ public class ActiveUpload { */ private Downloader download = null; - private final String destinationFile; + private final File destinationFile; private final RandomAccessFile outFile; - private ConcurrentLinkedQueue<FileChunk> chunks = new ConcurrentLinkedQueue<>(); + private final ChunkList chunks; - // TODO: Hashlist for verification + private final long fileSize; - public ActiveUpload(String destinationFile, long fileSize, List<byte[]> sha1Sums) + /** + * User owning this uploaded file. + */ + private final UserInfo owner; + + // TODO: Use HashList for verification + + public ActiveUpload(UserInfo owner, File destinationFile, long fileSize, List<ByteBuffer> sha1Sums) throws FileNotFoundException { this.destinationFile = destinationFile; - outFile = new RandomAccessFile(destinationFile, "rw"); - FileChunk.createChunkList(chunks, fileSize, sha1Sums); + this.outFile = new RandomAccessFile(destinationFile, "rw"); + this.chunks = new ChunkList(fileSize, sha1Sums); + this.owner = owner; + this.fileSize = fileSize; } /** @@ -45,7 +56,7 @@ public class ActiveUpload { * discarded */ public synchronized boolean addConnection(Downloader connection, ThreadPoolExecutor pool) { - if (download != null) + if (download != null || chunks.isComplete()) return false; download = connection; pool.execute(new Runnable() { @@ -55,7 +66,7 @@ public class ActiveUpload { if (!download.download(cbh, cbh) && cbh.currentChunk != null) { // If the download failed and we have a current chunk, put it back into // the queue, so it will be handled again later... - chunks.add(cbh.currentChunk); + chunks.markFailed(cbh.currentChunk); } } }); @@ -86,6 +97,27 @@ public class ActiveUpload { } /** + * Get user owning this upload. Can be null in special cases. + * + * @return instance of UserInfo for the according user. + */ + public UserInfo getOwner() { + return this.owner; + } + + public boolean isComplete() { + return chunks.isComplete() && destinationFile.length() == this.fileSize; + } + + public File getDestinationFile() { + return this.destinationFile; + } + + public long getSize() { + return this.fileSize; + } + + /** * Callback class for an instance of the Downloader, which supplies * the Downloader with wanted file ranges, and handles incoming data. */ @@ -111,11 +143,13 @@ public class ActiveUpload { // This needs to be async (own thread) so will be a little complicated } // Get next missing chunk - currentChunk = chunks.poll(); + currentChunk = chunks.getMissing(); if (currentChunk == null) return null; // No more chunks, returning null tells the Downloader we're done. return currentChunk.range; } } + // TODO: Clean up old stale uploads + } |