summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/FileServer.java
diff options
context:
space:
mode:
authorSimon Rettberg2015-06-11 18:40:49 +0200
committerSimon Rettberg2015-06-11 18:40:49 +0200
commite0005ceecfd9281230c4add7575b18ee88307774 (patch)
treea73bbcfc213df478c701aac120ae2b7c6e52bb1b /dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/FileServer.java
parent[server] db stuff, new interface, ... (diff)
downloadtutor-module-e0005ceecfd9281230c4add7575b18ee88307774.tar.gz
tutor-module-e0005ceecfd9281230c4add7575b18ee88307774.tar.xz
tutor-module-e0005ceecfd9281230c4add7575b18ee88307774.zip
[server] On mah way (lots of restructuring, some early db classes, sql dump of current schema)
Diffstat (limited to 'dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/FileServer.java')
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/FileServer.java106
1 files changed, 106 insertions, 0 deletions
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
new file mode 100644
index 00000000..c357c292
--- /dev/null
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/FileServer.java
@@ -0,0 +1,106 @@
+package org.openslx.bwlp.sat.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.bwlp.sat.util.Constants;
+import org.openslx.bwlp.sat.util.Formatter;
+import org.openslx.bwlp.thrift.iface.TTransferRejectedException;
+import org.openslx.bwlp.thrift.iface.UserInfo;
+import org.openslx.filetransfer.Downloader;
+import org.openslx.filetransfer.IncomingEvent;
+import org.openslx.filetransfer.Listener;
+import org.openslx.filetransfer.Uploader;
+
+public class FileServer implements IncomingEvent {
+
+ /**
+ * Listener for incoming unencrypted connections
+ */
+ private Listener plainListener = new Listener(this, null, 9092); // TODO: Config
+
+ /**
+ * All currently running uploads, indexed by token
+ */
+ 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
+ return ret;
+ }
+
+ @Override
+ public void incomingDownloadRequest(Uploader uploader) throws IOException {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void incomingUploadRequest(Downloader downloader) throws IOException {
+ // TODO Auto-generated method stub
+
+ }
+
+ /**
+ * 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 TTransferRejectedException, 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 TTransferRejectedException("Server busy. Too many running uploads.");
+ File destinationFile = null;
+ do {
+ destinationFile = Formatter.getTempImageName();
+ } while (destinationFile.exists());
+ // TODO: Pass image
+ ActiveUpload upload = new ActiveUpload(owner, null, 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
+ }
+
+}