summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/serverconnection/ConnectionHandler.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/serverconnection/ConnectionHandler.java')
-rw-r--r--src/main/java/org/openslx/imagemaster/serverconnection/ConnectionHandler.java66
1 files changed, 55 insertions, 11 deletions
diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/ConnectionHandler.java b/src/main/java/org/openslx/imagemaster/serverconnection/ConnectionHandler.java
index 498e058..1a2ff0b 100644
--- a/src/main/java/org/openslx/imagemaster/serverconnection/ConnectionHandler.java
+++ b/src/main/java/org/openslx/imagemaster/serverconnection/ConnectionHandler.java
@@ -1,5 +1,6 @@
package org.openslx.imagemaster.serverconnection;
+import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
@@ -24,8 +25,6 @@ import org.openslx.filetransfer.IncomingEvent;
import org.openslx.filetransfer.Listener;
import org.openslx.filetransfer.Uploader;
import org.openslx.imagemaster.Globals;
-import org.openslx.imagemaster.db.DbImage;
-import org.openslx.imagemaster.util.Tuple;
/**
* Class to handle all incoming and outgoing connections.
@@ -38,7 +37,7 @@ public class ConnectionHandler implements IncomingEvent
* Key: token,
* Value: Tuple of the listener and the filepath.
*/
- private static Map<String, Tuple<Thread, String>> activeListeners = new HashMap<>();
+ private static Map<String, ConnectionData> activeListeners = new HashMap<>();
private static List<Integer> possiblePorts = new LinkedList<>();
private static IncomingEvent eventHandler = new ConnectionHandler();
@@ -68,29 +67,43 @@ public class ConnectionHandler implements IncomingEvent
sslContext.init(keyManagers, null, null);
} catch (FileNotFoundException e) {
log.error( "Could not find keystore." );
+ System.exit( 2 );
} catch ( KeyStoreException e ) {
log.error( "KeyStore implemenation not supported." );
+ System.exit( 2 );
} catch ( NoSuchAlgorithmException e ) {
log.error( "Could not find such Algorithm" );
+ System.exit( 2 );
} catch ( CertificateException e ) {
log.error( "Certificate unvalid." );
+ System.exit( 2 );
} catch ( IOException e ) {
log.error( "Could not read keyfile" );
+ System.exit( 2 );
} catch ( UnrecoverableKeyException e ) {
log.error( "Key in keystore is not valid" );
+ System.exit( 2 );
} catch ( KeyManagementException e ) {
log.error( "Context initialization failed." );
+ System.exit( 2 );
}
}
- public static void addConnection(String token, String filepath)
+ /**
+ * Add a new connection with a unique token.
+ * Tp up- or download the file in filepath.
+ * @param token The unique token
+ * @param filepath The file to up- or download
+ * @param type True if upload or false if download
+ */
+ public static void addConnection(String token, String filepath, boolean type)
{
int port = possiblePorts.remove( 0 );
Listener listener = new Listener( eventHandler, sslContext, port );
listener.start();
- activeListeners.put( token, new Tuple<Thread, String>(listener, filepath) );
+ activeListeners.put( token, new ConnectionData(filepath, type, listener) );
}
public static boolean hasConnection( String token )
@@ -100,7 +113,7 @@ public class ConnectionHandler implements IncomingEvent
public static void removeConnection( String token )
{
- Listener l = (Listener)activeListeners.remove( token ).x;
+ Listener l = activeListeners.remove( token ).listenerThread;
l.interrupt();
possiblePorts.add(l.getPort()); // add port back to possible's list
}
@@ -111,7 +124,29 @@ public class ConnectionHandler implements IncomingEvent
@Override
public void incomingUploader( Uploader uploader ) throws IOException
{
- // TODO: Handle incoming uploads (client download requests)
+ // try to read meta data
+ while ( uploader.readMetaData() ) {
+ String token = uploader.getToken();
+ // check token to identify the client
+ if ( !activeListeners.containsKey( token )) {
+ uploader.sendErrorCode( "Token not accepted." );
+ uploader.close();
+ return;
+ }
+
+ // check if he was a downloading client
+ if ( activeListeners.get( token ).type == ConnectionData.UPLOADING ) {
+ uploader.sendErrorCode( "You can not download, if you are uploading." );
+ uploader.close();
+ return;
+ }
+ // TODO: check which range needs to be sent and send this range
+ long length = ( new File( activeListeners.get( token ).filepath ) ).length();
+
+ uploader.sendRange(0, (int)length);
+ uploader.sendFile( activeListeners.get( token ).filepath );
+ }
+ uploader.close();
}
/**
@@ -121,15 +156,24 @@ public class ConnectionHandler implements IncomingEvent
public void incomingDownloader( Downloader downloader ) throws IOException
{
// try to read meta data
- while (downloader.readMetaData()) {
+ while ( downloader.readMetaData() ) {
// check token to identify the client
String token = downloader.getToken();
- if (!activeListeners.containsKey( token )) {
+ if ( !activeListeners.containsKey( token ) ) {
+ downloader.sendErrorCode( "Token not accepted." );
+ downloader.close();
+ return;
+ }
+ // check if he was a uploading client
+ if ( activeListeners.get( token ).type == ConnectionData.DOWNLOADING ) {
+ downloader.sendErrorCode( "You can not upload, if you are downloading." );
+ downloader.close();
return;
}
- downloader.setOutputFilename( activeListeners.get( token ).y );
+
+ downloader.setOutputFilename( activeListeners.get( token ).filepath );
downloader.readBinary();
- downloader.close();
}
+ downloader.close();
}
}