diff options
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/serverconnection/ConnectionHandler.java')
-rw-r--r-- | src/main/java/org/openslx/imagemaster/serverconnection/ConnectionHandler.java | 138 |
1 files changed, 73 insertions, 65 deletions
diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/ConnectionHandler.java b/src/main/java/org/openslx/imagemaster/serverconnection/ConnectionHandler.java index 0d400db..f1f8887 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/ConnectionHandler.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/ConnectionHandler.java @@ -10,10 +10,10 @@ import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; -import java.util.HashMap; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; @@ -31,42 +31,30 @@ import org.openslx.imagemaster.Globals; */ public class ConnectionHandler implements IncomingEvent { + private static Logger log = Logger.getLogger( ConnectionHandler.class ); private static SSLContext sslContext; - /** - * Key: token, - * Value: Tuple of the listener and the filepath. - */ - private static Map<String, ConnectionData> activeListeners = new HashMap<>(); - private static List<Integer> possiblePorts = new LinkedList<>(); + + private static Map<String, Connection> connections = new ConcurrentHashMap<>(); private static IncomingEvent eventHandler = new ConnectionHandler(); - // TODO: There should only ever be one Listener instance in the whole application, running on a fixed port - + + private static Listener listener; + static { - possiblePorts.add( 1234 ); - possiblePorts.add( 1235 ); - possiblePorts.add( 1236 ); - possiblePorts.add( 1237 ); - possiblePorts.add( 1238 ); - possiblePorts.add( 1239 ); - possiblePorts.add( 1240 ); - possiblePorts.add( 1241 ); - possiblePorts.add( 1242 ); - possiblePorts.add( 1243 ); - possiblePorts.add( 1244 ); - + log.debug( "Starting listener on port " + Globals.getSslSocketPort() ); try { - String pathToKeyStore = Globals.getSslKeystoreFile(); - char[] passphrase = Globals.getSslKeystorePassword().toCharArray(); - KeyStore keystore = KeyStore.getInstance("JKS"); - keystore.load(new FileInputStream(pathToKeyStore), passphrase); - KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); - kmf.init(keystore, passphrase); - sslContext = SSLContext.getInstance("SSLv3"); - KeyManager[] keyManagers = kmf.getKeyManagers(); - - sslContext.init(keyManagers, null, null); - } catch (FileNotFoundException e) { + String pathToKeyStore = Globals.getSslKeystoreFile(); + char[] passphrase = Globals.getSslKeystorePassword().toCharArray(); + KeyStore keystore = KeyStore.getInstance( "JKS" ); + keystore.load( new FileInputStream( pathToKeyStore ), passphrase ); + KeyManagerFactory kmf = KeyManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm() ); + kmf.init( keystore, passphrase ); + sslContext = SSLContext.getInstance( "SSLv3" ); + KeyManager[] keyManagers = kmf.getKeyManagers(); + sslContext.init( keyManagers, null, null ); + listener = new Listener( eventHandler, sslContext, Globals.getSslSocketPort() ); + listener.start(); + } catch ( FileNotFoundException e ) { log.error( "Could not find keystore." ); System.exit( 2 ); } catch ( KeyStoreException e ) { @@ -89,36 +77,37 @@ public class ConnectionHandler implements IncomingEvent System.exit( 2 ); } } - + /** * Add a new connection with a unique token. - * Tp up- or download the file in filepath. + * To up- or download the file in file path. + * * @param token The unique token * @param filepath The file to up- or download * @param type True if upload or false if download - * @return The port that was used for this connection. + * @return The created connection */ - public static int addConnection(String token, String filepath, boolean type) + public static Connection addConnection( String token, String filepath, boolean type ) { - int port = possiblePorts.remove( 0 ); //TODO: handle if no ports are left - Listener listener = new Listener( eventHandler, sslContext, port ); - - listener.start(); - - activeListeners.put( token, new ConnectionData(filepath, type, listener) ); - return port; + log.debug( "Added connection (" + ( ( type ) ? "uploading" : "downloading" ) + ") with token: '" + token + "'" ); + + Connection connection = new Connection( filepath, type ); + synchronized ( connections ) { + connections.put( token, connection ); + } + return connection; } - + public static boolean hasConnection( String token ) { - return activeListeners.containsKey( token ); + return connections.containsKey( token ); } - + public static void removeConnection( String token ) { - Listener l = activeListeners.remove( token ).listenerThread; - l.interrupt(); - possiblePorts.add(l.getPort()); // add port back to possible's list + synchronized ( connections ) { + connections.remove( token ); // token is remove, so connections are rejected + } } /** @@ -130,54 +119,73 @@ public class ConnectionHandler implements IncomingEvent // try to read meta data while ( uploader.readMetaData() ) { String token = uploader.getToken(); - log.debug( "Got token :'" + token + "'"); + log.debug( "Got token :'" + token + "'" ); // check token to identify the client - if ( !activeListeners.containsKey( token )) { + if ( !connections.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." ); // TODO: Why not? + if ( connections.get( token ).type == Connection.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 ); + long length = ( new File( connections.get( token ).filepath ) ).length(); + + uploader.sendRange( 0, (int)length ); + uploader.sendFile( connections.get( token ).filepath ); } uploader.close(); } - + /** * Server is downloading - client is uploading! */ @Override public void incomingDownloader( Downloader downloader ) throws IOException { + int startOfRange = 0; + int endOfRange = 0; + int diffOfRange = 0; + String token = ""; // try to read meta data while ( downloader.readMetaData() ) { // check token to identify the client - String token = downloader.getToken(); - if ( !activeListeners.containsKey( token ) ) { + token = downloader.getToken(); + if ( !connections.containsKey( token ) ) { downloader.sendErrorCode( "Token not accepted." ); downloader.close(); return; } + + // get range + startOfRange = downloader.getStartOfRange(); + endOfRange = downloader.getEndOfRange(); + diffOfRange = downloader.getDiffOfRange(); + // check if he was a uploading client - if ( activeListeners.get( token ).type == ConnectionData.DOWNLOADING ) { + if ( connections.get( token ).type == Connection.DOWNLOADING ) { downloader.sendErrorCode( "You can not upload, if you are downloading." ); downloader.close(); return; } - - downloader.setOutputFilename( activeListeners.get( token ).filepath ); + + downloader.setOutputFilename( connections.get( token ).filepath ); downloader.readBinary(); } downloader.close(); + + // calculate and register the incoming blocks + if ( diffOfRange == 0 ) + return; + + for ( int i = startOfRange / Globals.blockSize; i < endOfRange / Globals.blockSize; i += Globals.blockSize ) { + connections.get( token ).image.addBlockToCheck( i ); + } + } } |