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/FileServer.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/FileServer.java')
-rw-r--r-- | dozentenmodulserver/src/main/java/fileserv/FileServer.java | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/dozentenmodulserver/src/main/java/fileserv/FileServer.java b/dozentenmodulserver/src/main/java/fileserv/FileServer.java index 446b982e..8322e2e9 100644 --- a/dozentenmodulserver/src/main/java/fileserv/FileServer.java +++ b/dozentenmodulserver/src/main/java/fileserv/FileServer.java @@ -1,13 +1,24 @@ package fileserv; +import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Iterator; +import java.util.List; import java.util.Map; +import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import org.openslx.filetransfer.Downloader; import org.openslx.filetransfer.IncomingEvent; import org.openslx.filetransfer.Listener; import org.openslx.filetransfer.Uploader; +import org.openslx.imagemaster.thrift.iface.UserInfo; +import org.openslx.sat.thrift.iface.TUploadRejectedException; + +import util.Constants; +import util.Formatter; public class FileServer implements IncomingEvent { @@ -21,6 +32,15 @@ public class FileServer implements IncomingEvent { */ private Map<String, ActiveUpload> uploads = new ConcurrentHashMap<>(); + private static final FileServer globalInstance = new FileServer(); + + private FileServer() { + } + + public static FileServer instance() { + return globalInstance; + } + public boolean start() { boolean ret = plainListener.start(); // TODO: Start SSL listener too @@ -39,4 +59,48 @@ public class FileServer implements IncomingEvent { } + /** + * Get an upload instance by given token. + * + * @param uploadToken + * @return + */ + public ActiveUpload getUploadByToken(String uploadToken) { + return uploads.get(uploadToken); + } + + public String createNewUserUpload(UserInfo owner, long fileSize, List<ByteBuffer> sha1Sums) + throws TUploadRejectedException, FileNotFoundException { + Iterator<ActiveUpload> it = uploads.values().iterator(); + int activeUploads = 0; + while (it.hasNext()) { + ActiveUpload upload = it.next(); + if (upload.isComplete()) { + // TODO: Check age (short timeout) and remove + continue; + } else { + // Check age (long timeout) and remove + } + activeUploads++; + } + if (activeUploads > Constants.MAX_UPLOADS) + throw new TUploadRejectedException("Server busy. Too many running uploads."); + File destinationFile = null; + do { + destinationFile = Formatter.getTempImageName(); + } while (destinationFile.exists()); + ActiveUpload upload = new ActiveUpload(owner, destinationFile, fileSize, sha1Sums); + String key = UUID.randomUUID().toString(); + uploads.put(key, upload); + return key; + } + + public int getPlainPort() { + return plainListener.getPort(); + } + + public int getSslPort() { + return 0; // TODO + } + } |