diff options
author | Simon Rettberg | 2014-09-17 16:33:51 +0200 |
---|---|---|
committer | Simon Rettberg | 2014-09-17 16:33:51 +0200 |
commit | eaa0e5dcb9e506720fff48ae1efbb07579342471 (patch) | |
tree | c97820005bd73361557bc1e1b00264dffffe34b4 | |
parent | Add deploy section (diff) | |
download | master-sync-shared-eaa0e5dcb9e506720fff48ae1efbb07579342471.tar.gz master-sync-shared-eaa0e5dcb9e506720fff48ae1efbb07579342471.tar.xz master-sync-shared-eaa0e5dcb9e506720fff48ae1efbb07579342471.zip |
Add debugging to file transfer
4 files changed, 57 insertions, 62 deletions
diff --git a/src/main/java/org/openslx/filetransfer/ClassTest.java b/src/main/java/org/openslx/filetransfer/ClassTest.java index 4550060..2a4d05c 100644 --- a/src/main/java/org/openslx/filetransfer/ClassTest.java +++ b/src/main/java/org/openslx/filetransfer/ClassTest.java @@ -34,6 +34,10 @@ import org.slf4j.LoggerFactory; public class ClassTest { + + private static String inFile; + private static String outFile; + static { // This is a temporary workaround for this annoying log4j error msg. // Initializing the logger before anything else is done. @@ -43,9 +47,14 @@ public class ClassTest public static void main( String[] args ) throws Exception { - String pathToKeyStore = - "/home/bjoern/javadev/DataTransfer/mySrvKeyStore.jks"; - char[] passphrase = "test123".toCharArray(); + if (args.length != 4) { + System.out.println("Need 4 argument: <keystore> <passphrase> <infile> <outfile>"); + System.exit(1); + } + String pathToKeyStore = args[0]; + final char[] passphrase = args[1].toCharArray(); + inFile = args[2]; + outFile = args[3]; KeyStore keystore = KeyStore.getInstance( "JKS" ); keystore.load( new FileInputStream( pathToKeyStore ), passphrase ); KeyManagerFactory kmf = KeyManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm() ); @@ -58,14 +67,7 @@ public class ClassTest Listener listener = new Listener( new Test(), context, 6789 ); listener.start(); - Thread.sleep( 5000 ); - - String pathToTrustStore = - "/home/bjoern/javadev/DataTransfer/mySrvKeyStore.jks"; - - passphrase = "test123".toCharArray(); - keystore = KeyStore.getInstance( "JKS" ); - keystore.load( new FileInputStream( pathToTrustStore ), passphrase ); + Thread.sleep( 2000 ); TrustManagerFactory tmf = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() ); tmf.init( keystore ); @@ -76,7 +78,7 @@ public class ClassTest context.init( null, trustManagers, null ); Downloader d = new Downloader( "localhost", 6789, context ); - d.setOutputFilename( "output.txt" ); + d.setOutputFilename( outFile ); d.sendToken( "xyz" ); while ( d.readMetaData() ) d.receiveBinary(); @@ -112,16 +114,15 @@ public class ClassTest u.sendFile("test.txt"); */ } -} // Implementing IncomingEvent for testing case. -class Test implements IncomingEvent +static class Test implements IncomingEvent { public void incomingUploader( Uploader uploader ) throws IOException { RandomAccessFile file; try { - file = new RandomAccessFile( new File( "test.txt" ), "r" ); + file = new RandomAccessFile( new File( inFile ), "r" ); } catch ( FileNotFoundException e ) { e.printStackTrace(); return; @@ -132,19 +133,23 @@ class Test implements IncomingEvent int diff = 0; for ( int i = 0; ( i + 254 ) < length; i += 254 ) { - uploader.sendRange( i, i + 254 ); - uploader.sendFile( "test.txt" ); + if ( !uploader.sendRange( i, i + 254 ) || !uploader.sendFile( inFile ) ) { + System.out.println("FAIL"); + return; + } diff = (int) ( length - i ); } uploader.sendRange( (int) ( length - diff ), (int)length ); - uploader.sendFile( "test.txt" ); + uploader.sendFile( inFile ); } public void incomingDownloader( Downloader downloader ) throws IOException { - downloader.setOutputFilename( "output.txt" ); + downloader.setOutputFilename( outFile ); while ( downloader.readMetaData() ) downloader.receiveBinary(); } } + +} diff --git a/src/main/java/org/openslx/filetransfer/Downloader.java b/src/main/java/org/openslx/filetransfer/Downloader.java index d9e41a5..6946173 100644 --- a/src/main/java/org/openslx/filetransfer/Downloader.java +++ b/src/main/java/org/openslx/filetransfer/Downloader.java @@ -107,14 +107,12 @@ public class Downloader extends Transfer } catch ( SocketTimeoutException ste ) { ste.printStackTrace(); sendErrorCode( "timeout" ); - log.info( "Socket timeout occured ... close connection." ); - this.close(); + this.close( "Socket timeout occured ... close connection." ); return false; } catch ( Exception e ) { e.printStackTrace(); - log.info( "Reading RANGE " + getStartOfRange() + ":" + getEndOfRange() - + " of file failed..." ); - this.close(); + this.close( "Reading RANGE " + getStartOfRange() + ":" + getEndOfRange() + + " of file from socket failed..." ); return false; } finally { if ( file != null ) { diff --git a/src/main/java/org/openslx/filetransfer/Transfer.java b/src/main/java/org/openslx/filetransfer/Transfer.java index aa7f9ed..a8c9a8a 100644 --- a/src/main/java/org/openslx/filetransfer/Transfer.java +++ b/src/main/java/org/openslx/filetransfer/Transfer.java @@ -54,17 +54,17 @@ public abstract class Transfer } try { sendKeyValuePair( "RANGE", startOffset + ":" + endOffset ); + RANGE[0] = startOffset; + RANGE[1] = endOffset; } catch ( SocketTimeoutException ste ) { ste.printStackTrace(); - log.info( "Socket timeout occured ... close connection." ); - this.close(); + this.close( "Socket timeout occured ... close connection." ); } catch ( IOException e ) { e.printStackTrace(); readMetaData(); if ( ERROR != null ) { - if ( ERROR == "timeout" ) { - log.info( "Socket timeout occured ... close connection." ); - this.close(); + if ( ERROR.equals( "timeout" ) ) { + this.close( "Remote Socket timeout occured ... close connection." ); } } log.info( "Sending RANGE in Uploader failed..." ); @@ -90,15 +90,13 @@ public abstract class Transfer sendKeyValuePair( "TOKEN", TOKEN ); } catch ( SocketTimeoutException ste ) { ste.printStackTrace(); - log.info( "Socket timeout occured ... close connection." ); - this.close(); + this.close( "Socket timeout occured ... close connection." ); } catch ( IOException e ) { e.printStackTrace(); readMetaData(); if ( ERROR != null ) { - if ( ERROR == "timeout" ) { - log.info( "Socket timeout occured ... close connection." ); - this.close(); + if ( ERROR.equals( "timeout" ) ) { + this.close( "Remote Socket timeout occured ... close connection." ); } } log.info( "Sending TOKEN in Downloader failed..." ); @@ -197,13 +195,11 @@ public abstract class Transfer // First get length. int retLengthByte; - log.debug("dataFromServer.available() : " + dataFromServer.available()); retLengthByte = dataFromServer.read( incoming, 0, 1 ); // If .read() didn't return 1, it was not able to read first byte. if ( retLengthByte != 1 ) { - log.warn( "Error occured while reading Metadata." ); log.debug( " retLenthByte was not 1! retLengthByte = " + retLengthByte); - this.close(); + this.close( "Error occured while reading Metadata." ); return false; } @@ -221,8 +217,7 @@ public abstract class Transfer while ( hasRead < length ) { int ret = dataFromServer.read( incoming, hasRead, length - hasRead ); if ( ret == -1 ) { - log.warn( "Error occured while reading Metadata." ); - this.close(); + this.close( "Error occured while reading Metadata." ); return false; } hasRead += ret; @@ -237,8 +232,7 @@ public abstract class Transfer } if ( splitted[0].equals( "TOKEN" ) ) { if ( TOKEN != null ) { - log.warn( "Received a token when a token is already set!" ); - this.close(); + this.close( "Received a token when a token is already set!" ); return false; } TOKEN = splitted[1]; @@ -246,7 +240,7 @@ public abstract class Transfer } else if ( splitted[0].equals( "RANGE" ) ) { if ( !parseRange( splitted[1] ) ) { - this.close(); + this.close( "Could not parse RANGE token" ); return false; } log.debug( "RANGE: '" + splitted[1] + "'" ); @@ -259,12 +253,11 @@ public abstract class Transfer } catch ( SocketTimeoutException ste ) { ste.printStackTrace(); sendErrorCode( "timeout" ); - log.info( "Socket Timeout occured in Downloader." ); - this.close(); + this.close( "Socket Timeout occured in readMetaData." ); return false; } catch ( Exception e ) { e.printStackTrace(); - this.close(); + this.close( e.toString() ); return false; } return true; @@ -289,7 +282,7 @@ public abstract class Transfer sendKeyValuePair( "ERROR", errString ); } catch ( IOException e ) { e.printStackTrace(); - this.close(); + this.close( e.toString() ); return false; } return true; @@ -300,8 +293,10 @@ public abstract class Transfer * Method for closing connection, if download has finished. * */ - public void close() + public void close( String error ) { + if ( error != null ) + log.info( error ); try { if ( satelliteSocket != null ) this.satelliteSocket.close(); diff --git a/src/main/java/org/openslx/filetransfer/Uploader.java b/src/main/java/org/openslx/filetransfer/Uploader.java index 90d9ba2..3deb272 100644 --- a/src/main/java/org/openslx/filetransfer/Uploader.java +++ b/src/main/java/org/openslx/filetransfer/Uploader.java @@ -64,14 +64,14 @@ public class Uploader extends Transfer */ public boolean sendFile( String filename ) { + if ( getStartOfRange() == -1 ) { + this.close( "sendFile called when no range is set" ); + return false; + } + RandomAccessFile file = null; try { file = new RandomAccessFile( new File( filename ), "r" ); - - if ( getStartOfRange() == -1 ) { - this.close(); - return false; - } file.seek( getStartOfRange() ); byte[] data = new byte[ 64000 ]; @@ -81,9 +81,8 @@ public class Uploader extends Transfer while ( hasRead < length ) { int ret = file.read( data, 0, Math.min( length - hasRead, data.length ) ); if ( ret == -1 ) { - log.warn( "Error occured in Uploader.sendFile()," + this.close( "Error occured in Uploader.sendFile()," + " while reading from File to send." ); - this.close(); return false; } hasRead += ret; @@ -92,25 +91,23 @@ public class Uploader extends Transfer } catch ( SocketTimeoutException ste ) { ste.printStackTrace(); sendErrorCode( "timeout" ); - log.warn( "Socket timeout occured ... close connection." ); - this.close(); + this.close( "Socket timeout occured ... close connection." ); return false; } catch ( IOException ioe ) { ioe.printStackTrace(); readMetaData(); if ( ERROR != null ) { - if ( ERROR == "timeout" ) { - log.warn( "Socket timeout occured ... close connection." ); - this.close(); + if ( ERROR.equals( "timeout" ) ) { + this.close( "Remote Socket timeout occured ... close connection." ); + return false; } } - log.warn( "Sending RANGE " + getStartOfRange() + ":" + getEndOfRange() + " of File " + this.close( "Sending RANGE " + getStartOfRange() + ":" + getEndOfRange() + " of File " + filename + " failed..." ); - this.close(); return false; } catch ( Exception e ) { e.printStackTrace(); - this.close(); + this.close( e.toString() ); return false; } finally { if ( file != null ) { |