summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/filetransfer
diff options
context:
space:
mode:
authorBjörn Hagemeister2014-07-07 15:15:48 +0200
committerBjörn Hagemeister2014-07-07 15:15:48 +0200
commit1c11ef53265b86f6e9062c80ea1af949a2d16da3 (patch)
tree16fe0f19cd6ce0338545520de9795a75929921f8 /src/main/java/org/openslx/filetransfer
parentAdd argument to send crc sums over thrift (diff)
downloadmaster-sync-shared-1c11ef53265b86f6e9062c80ea1af949a2d16da3.tar.gz
master-sync-shared-1c11ef53265b86f6e9062c80ea1af949a2d16da3.tar.xz
master-sync-shared-1c11ef53265b86f6e9062c80ea1af949a2d16da3.zip
Implemented Exception Handling with try-catch block.
Adapted code to format scheme.
Diffstat (limited to 'src/main/java/org/openslx/filetransfer')
-rw-r--r--src/main/java/org/openslx/filetransfer/ClassTest.java112
-rw-r--r--src/main/java/org/openslx/filetransfer/Downloader.java357
-rw-r--r--src/main/java/org/openslx/filetransfer/IncomingEvent.java14
-rw-r--r--src/main/java/org/openslx/filetransfer/Listener.java105
-rw-r--r--src/main/java/org/openslx/filetransfer/Uploader.java324
5 files changed, 513 insertions, 399 deletions
diff --git a/src/main/java/org/openslx/filetransfer/ClassTest.java b/src/main/java/org/openslx/filetransfer/ClassTest.java
index 768316b..e18358a 100644
--- a/src/main/java/org/openslx/filetransfer/ClassTest.java
+++ b/src/main/java/org/openslx/filetransfer/ClassTest.java
@@ -14,7 +14,7 @@
* If master receives token "D" --> start Uploader(socket)
*/
-//TODO Björn Hagemeister. SSLConfiguration!
+// TODO Björn Hagemeister. SSLConfiguration!
package org.openslx.filetransfer;
@@ -31,59 +31,60 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
-public class ClassTest {
- public static void main(String[] args) throws Exception {
+public class ClassTest
+{
+ public static void main( String[] args ) throws Exception
+ {
String pathToKeyStore =
"/home/bjoern/javadev/DataTransfer/mySrvKeyStore.jks";
- char[] passphrase = "test123".toCharArray();
- KeyStore keystore = KeyStore.getInstance("JKS");
- keystore.load(new FileInputStream(pathToKeyStore), passphrase);
- KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
- kmf.init(keystore, passphrase);
- SSLContext context = SSLContext.getInstance("SSLv3");
- KeyManager[] keyManagers = kmf.getKeyManagers();
+ char[] passphrase = "test123".toCharArray();
+ KeyStore keystore = KeyStore.getInstance( "JKS" );
+ keystore.load( new FileInputStream( pathToKeyStore ), passphrase );
+ KeyManagerFactory kmf = KeyManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm() );
+ kmf.init( keystore, passphrase );
+ SSLContext context = SSLContext.getInstance( "SSLv3" );
+ KeyManager[] keyManagers = kmf.getKeyManagers();
- context.init(keyManagers, null, null);
+ context.init( keyManagers, null, null );
- Listener listener = new Listener(new Test(), context, 6789);
+ Listener listener = new Listener( new Test(), context, 6789 );
listener.start();
-
- Thread.sleep(5000);
-
+
+ Thread.sleep( 5000 );
+
String pathToTrustStore =
"/home/bjoern/javadev/DataTransfer/mySrvKeyStore.jks";
- passphrase = "test123".toCharArray();
- keystore = KeyStore.getInstance("JKS");
- keystore.load(new FileInputStream(pathToTrustStore), passphrase);
+ passphrase = "test123".toCharArray();
+ keystore = KeyStore.getInstance( "JKS" );
+ keystore.load( new FileInputStream( pathToTrustStore ), passphrase );
- TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
- tmf.init(keystore);
+ TrustManagerFactory tmf = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() );
+ tmf.init( keystore );
- context = SSLContext.getInstance("SSLv3");
- TrustManager[] trustManagers = tmf.getTrustManagers();
+ context = SSLContext.getInstance( "SSLv3" );
+ TrustManager[] trustManagers = tmf.getTrustManagers();
- context.init(null, trustManagers, null);
+ context.init( null, trustManagers, null );
-
- Downloader d = new Downloader("localhost", 6789, context);
- d.setOutputFilename("output.txt");
- d.sendToken("xyz");
- while (d.readMetaData())
+ Downloader d = new Downloader( "localhost", 6789, context );
+ d.setOutputFilename( "output.txt" );
+ d.sendToken( "xyz" );
+ while ( d.readMetaData() )
d.readBinary();
-
+
/*
String pathToKeyStore =
"/home/bjoern/javadev/DataTransfer/mySrvKeyStore.jks";
- char[] passphrase = "test123".toCharArray();
- KeyStore keystore = KeyStore.getInstance("JKS");
- keystore.load(new FileInputStream(pathToKeyStore), passphrase);
- KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
- kmf.init(keystore, passphrase);
- SSLContext context = SSLContext.getInstance("SSLv3");
- KeyManager[] keyManagers = kmf.getKeyManagers();
+ char[] passphrase = "test123".toCharArray();
+ KeyStore keystore = KeyStore.getInstance("JKS");
+ keystore.load(new FileInputStream(pathToKeyStore), passphrase);
+ KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+ kmf.init(keystore, passphrase);
+ SSLContext context = SSLContext.getInstance("SSLv3");
+ KeyManager[] keyManagers = kmf.getKeyManagers();
- context.init(keyManagers, null, null);
+ context.init(keyManagers, null, null);
Uploader u = new Uploader("localhost", 6789, context);
u.sendToken("xyz");
@@ -106,33 +107,36 @@ public class ClassTest {
}
// Implementing IncomingEvent for testing case.
-class Test implements IncomingEvent {
- public void incomingUploader(Uploader uploader) throws IOException {
+class Test implements IncomingEvent
+{
+ public void incomingUploader( Uploader uploader ) throws IOException
+ {
RandomAccessFile file;
try {
- file = new RandomAccessFile(new File("test.txt"), "r");
- } catch (FileNotFoundException e) {
+ file = new RandomAccessFile( new File( "test.txt" ), "r" );
+ } catch ( FileNotFoundException e ) {
e.printStackTrace();
return;
}
-
+
long length = file.length();
file.close();
-
+
int diff = 0;
- for (int i = 0; (i + 5) < length; i += 5) {
- uploader.sendRange(i, i + 5);
- uploader.sendFile("test.txt");
- diff = (int) (length - i);
+ for ( int i = 0; ( i + 5 ) < length; i += 5 ) {
+ uploader.sendRange( i, i + 5 );
+ uploader.sendFile( "test.txt" );
+ diff = (int) ( length - i );
}
-
- uploader.sendRange((int)(length - diff), (int)length);
- uploader.sendFile("test.txt");
+
+ uploader.sendRange( (int) ( length - diff ), (int)length );
+ uploader.sendFile( "test.txt" );
}
-
- public void incomingDownloader(Downloader downloader) throws IOException {
- downloader.setOutputFilename("output.txt");
- while (downloader.readMetaData())
+
+ public void incomingDownloader( Downloader downloader ) throws IOException
+ {
+ downloader.setOutputFilename( "output.txt" );
+ while ( downloader.readMetaData() )
downloader.readBinary();
}
}
diff --git a/src/main/java/org/openslx/filetransfer/Downloader.java b/src/main/java/org/openslx/filetransfer/Downloader.java
index 6fe0f1a..1bbb1dc 100644
--- a/src/main/java/org/openslx/filetransfer/Downloader.java
+++ b/src/main/java/org/openslx/filetransfer/Downloader.java
@@ -15,7 +15,8 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
-public class Downloader {
+public class Downloader
+{
// Some instance variables.
private SSLSocketFactory sslSocketFactory;
private SSLSocket satelliteSocket;
@@ -25,268 +26,314 @@ public class Downloader {
private String RANGE = null;
private String outputFilename = null;
private String ERROR = null;
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Constructor for satellite downloader.
* Tries to connect to specific ip and port and sending type of action.
+ *
* @param ip
* @param port
- * @throws IOException
- * @throws KeyStoreException
- * @throws CertificateException
- * @throws NoSuchAlgorithmException
- * @throws KeyManagementException
+ * @throws IOException
+ * @throws KeyStoreException
+ * @throws CertificateException
+ * @throws NoSuchAlgorithmException
+ * @throws KeyManagementException
*/
- public Downloader(String ip, int port, SSLContext context) throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, KeyManagementException {
- // TODO: Remove old code, that's why we have git.. ;)
- /*
- char[] passphrase = "test123".toCharArray();
- KeyStore keystore = KeyStore.getInstance("JKS");
- keystore.load(new FileInputStream(pathToTrustStore), passphrase);
-
- TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
- tmf.init(keystore);
-
- SSLContext context = SSLContext.getInstance("SSLv3");
- TrustManager[] trustManagers = tmf.getTrustManagers();
-
- context.init(null, trustManagers, null);
- */
-
- // create socket.
- sslSocketFactory = context.getSocketFactory();
-
- satelliteSocket = (SSLSocket) sslSocketFactory.createSocket(ip, port);
-
- dataToServer = new DataOutputStream(satelliteSocket.getOutputStream());
- dataToServer.writeByte('D');
- dataFromServer = new DataInputStream(satelliteSocket.getInputStream());
+ public Downloader( String ip, int port, SSLContext context )
+ {
+ try {
+ // TODO: Remove old code, that's why we have git.. ;)
+ // create socket.
+ sslSocketFactory = context.getSocketFactory();
+
+ satelliteSocket = (SSLSocket)sslSocketFactory.createSocket( ip, port );
+
+ dataToServer = new DataOutputStream( satelliteSocket.getOutputStream() );
+ dataToServer.writeByte( 'D' );
+ dataFromServer = new DataInputStream( satelliteSocket.getInputStream() );
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
}
- /***********************************************************************//**
+ /***********************************************************************/
+ /**
* Constructor for master downloader.
* Given parameter is the socket over which the transfer is going.
+ *
* @param socket
- * @throws IOException
*/
- public Downloader(SSLSocket socket) throws IOException {
- satelliteSocket = socket;
- dataToServer = new DataOutputStream(satelliteSocket.getOutputStream());
- dataFromServer = new DataInputStream(satelliteSocket.getInputStream());
+ public Downloader( SSLSocket socket )
+ {
+ try {
+ satelliteSocket = socket;
+ dataToServer = new DataOutputStream( satelliteSocket.getOutputStream() );
+ dataFromServer = new DataInputStream( satelliteSocket.getInputStream() );
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
}
- /***********************************************************************//**
+ /***********************************************************************/
+ /**
* Method for setting outputFilename.
+ *
* @param filename
*/
- public void setOutputFilename(String filename)
+ public void setOutputFilename( String filename )
{
outputFilename = filename;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Method for getting outputFilename.
- * @return Return outputFilename or null.
+ *
*/
public String getOutputFilename()
{
- if (outputFilename != null)
- return outputFilename;
- return null;
+ return outputFilename;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Method for sending token for identification from satellite to master.
+ *
* @param t
- * @throws IOException
*/
- public void sendToken(String token) throws IOException {
- TOKEN = token;
- String sendToken = "TOKEN=" + TOKEN;
- byte[] data = sendToken.getBytes(StandardCharsets.UTF_8);
- dataToServer.writeByte(data.length);
- dataToServer.write(data);
+ public Boolean sendToken( String token )
+ {
+ try {
+ TOKEN = token;
+ String sendToken = "TOKEN=" + TOKEN;
+ byte[] data = sendToken.getBytes( StandardCharsets.UTF_8 );
+ dataToServer.writeByte( data.length );
+ dataToServer.write( data );
+ } catch (IOException e) {
+ e.printStackTrace();
+ return false;
+ }
+ return true;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Method to send range of the file, which should be uploaded.
* Helpful for knowing how much was already uploaded if
* connection aborts.
+ *
* @param a
* @param b
- * @throws IOException
*/
- public void sendRange(int a, int b) throws IOException {
- RANGE = a + ":" + b;
- String sendRange = "RANGE=" + RANGE;
- byte[] data = sendRange.getBytes(StandardCharsets.UTF_8);
- dataToServer.writeByte(data.length);
- dataToServer.write(data);
+ public Boolean sendRange( int a, int b )
+ {
+ try {
+ RANGE = a + ":" + b;
+ String sendRange = "RANGE=" + RANGE;
+ byte[] data = sendRange.getBytes( StandardCharsets.UTF_8 );
+ dataToServer.writeByte( data.length );
+ dataToServer.write( data );
+ } catch (IOException e) {
+ e.printStackTrace();
+ return false;
+ }
+ return true;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Method for reading incoming token for identification.
- * @throws IOException
+ *
*/
- public String getToken() {
+ public String getToken()
+ {
return TOKEN;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Method for reading range of file, which is downloaded.
* Helpful for knowing how much is already downloaded if connection aborts.
*/
- public String getRange() {
+ public String getRange()
+ {
return RANGE;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Getter for beginning of RANGE.
+ *
* @return
*/
- public int getStartOfRange() {
- if (RANGE != null) {
- String[] splitted = RANGE.split(":");
- return Integer.parseInt(splitted[0]);
+ public int getStartOfRange()
+ {
+ if ( RANGE != null ) {
+ String[] splitted = RANGE.split( ":" );
+ return Integer.parseInt( splitted[0] );
}
return -1;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Getter for end of RANGE.
+ *
* @return
*/
- public int getEndOfRange() {
- if (RANGE != null) {
- String[] splitted = RANGE.split(":");
- return Integer.parseInt(splitted[1]);
+ public int getEndOfRange()
+ {
+ if ( RANGE != null ) {
+ String[] splitted = RANGE.split( ":" );
+ return Integer.parseInt( splitted[1] );
}
return -1;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Method for returning difference of current Range.
+ *
* @return
*/
- public int getDiffOfRange() {
- int diff = Math.abs(getEndOfRange() - getStartOfRange());
+ public int getDiffOfRange()
+ {
+ int diff = Math.abs( getEndOfRange() - getStartOfRange() );
return diff;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Method for reading MetaData, like TOKEN and FileRange.
* Split incoming bytes after first '=' and store value to specific
* variable.
- * @throws IOException
+ *
*/
- public Boolean readMetaData() throws IOException {
+ public Boolean readMetaData()
+ {
try {
- while (true) {
- byte[] incoming = new byte[255];
-
+ while ( true ) {
+ byte[] incoming = new byte[ 255 ];
+
// First get length.
- dataFromServer.read(incoming, 0, 1);
+ dataFromServer.read( incoming, 0, 1 );
int length = incoming[0];
- System.out.println("length: " + length);
-
- if (length == 0)
+ System.out.println( "length: " + length );
+
+ if ( length == 0 )
break;
-
+
/**
- * Read the next available bytes and split by '=' for
- * getting TOKEN or RANGE.
+ * Read the next available bytes and split by '=' for
+ * getting TOKEN or RANGE.
*/
int hasRead = 0;
- while (hasRead < length) {
- int ret = dataFromServer.read(incoming, hasRead, length - hasRead);
- if (ret == -1) {
- System.out.println("Error occured while reading Metadata.");
+ while ( hasRead < length ) {
+ int ret = dataFromServer.read( incoming, hasRead, length - hasRead );
+ if ( ret == -1 ) {
+ System.out.println( "Error occured while reading Metadata." );
return false;
}
hasRead += ret;
}
-
- String data = new String(incoming, 0, length, "UTF-8");
+
+ String data = new String( incoming, 0, length, "UTF-8" );
// System.out.println(data);
-
- String[] splitted = data.split("=");
+
+ String[] splitted = data.split( "=" );
// System.out.println("splitted[0]: " + splitted[0]);
// System.out.println("splitted[1]: " + splitted[1]);
- if (splitted[0] != null && splitted[0].equals("TOKEN")) {
- if (splitted[1] != null)
+ if ( splitted[0] != null && splitted[0].equals( "TOKEN" ) ) {
+ if ( splitted[1] != null )
TOKEN = splitted[1];
- System.out.println("TOKEN: " + TOKEN);
+ System.out.println( "TOKEN: " + TOKEN );
}
- else if (splitted[0].equals("RANGE")) {
- if (splitted[1] != null)
+ else if ( splitted[0].equals( "RANGE" ) ) {
+ if ( splitted[1] != null )
RANGE = splitted[1];
- System.out.println("RANGE: '" + RANGE + "'");
+ System.out.println( "RANGE: '" + RANGE + "'" );
}
- else if (splitted[0].equals("ERROR")) {
- if (splitted[1] != null)
+ else if ( splitted[0].equals( "ERROR" ) ) {
+ if ( splitted[1] != null )
ERROR = splitted[1];
- System.err.println("ERROR: " + ERROR);
+ System.err.println( "ERROR: " + ERROR );
this.close();
return false;
}
}
- } catch (Exception e) {
+ } catch ( Exception e ) {
e.printStackTrace();
return false;
}
return true;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Method for reading Binary. Reading the current Range of incoming binary.
- * @throws IOException
+ *
*/
- public Boolean readBinary() throws IOException {
- int length = getDiffOfRange();
- byte[] incoming = new byte[4000];
-
- int hasRead = 0;
- while (hasRead < length) {
- int ret = dataFromServer.read(incoming, hasRead, length - hasRead);
- if (ret == -1) {
- System.out.println("Error occured in Downloader.readBinary(),"
- + " while reading binary.");
- return false;
- }
- hasRead += ret;
- }
-
- RandomAccessFile file;
+ public Boolean readBinary()
+ {
try {
- file = new RandomAccessFile(new File(outputFilename), "rw");
- file.seek(getStartOfRange());
- file.write(incoming, 0, length);
+ int length = getDiffOfRange();
+ byte[] incoming = new byte[ 4000 ];
+
+ int hasRead = 0;
+ while ( hasRead < length ) {
+ int ret = dataFromServer.read( incoming, hasRead, length - hasRead );
+ if ( ret == -1 ) {
+ System.out.println( "Error occured in Downloader.readBinary(),"
+ + " while reading binary." );
+ return false;
+ }
+ hasRead += ret;
+ }
+
+ RandomAccessFile file;
+ file = new RandomAccessFile( new File( outputFilename ), "rw" );
+ file.seek( getStartOfRange() );
+ file.write( incoming, 0, length );
file.close();
-
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Method for sending error Code to server. For example in case of wrong
* token, send code for wrong token.
- * @throws IOException
+ *
*/
- public void sendErrorCode(String errString) throws IOException {
- String sendError = "ERROR=" + errString;
- byte[] data = sendError.getBytes(StandardCharsets.UTF_8);
- dataToServer.writeByte(data.length);
- dataToServer.write(data);
+ public Boolean sendErrorCode( String errString )
+ {
+ try {
+ String sendError = "ERROR=" + errString;
+ byte[] data = sendError.getBytes( StandardCharsets.UTF_8 );
+ dataToServer.writeByte( data.length );
+ dataToServer.write( data );
+ } catch (IOException e) {
+ e.printStackTrace();
+ return false;
+ }
+ return true;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Method for closing connection, if download has finished.
- * @throws IOException
+ *
*/
- public void close() throws IOException {
- this.satelliteSocket.close();
+ public void close()
+ {
+ try {
+ this.satelliteSocket.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
}
}
diff --git a/src/main/java/org/openslx/filetransfer/IncomingEvent.java b/src/main/java/org/openslx/filetransfer/IncomingEvent.java
index a1e8b9e..fc5dc63 100644
--- a/src/main/java/org/openslx/filetransfer/IncomingEvent.java
+++ b/src/main/java/org/openslx/filetransfer/IncomingEvent.java
@@ -2,12 +2,16 @@ package org.openslx.filetransfer;
import java.io.IOException;
-/***************************************************************************//**
+/***************************************************************************/
+/**
* IncomingEvent interface for handling what should happen with incoming
- * uploader or downloader in Listener. Must be implemented outside.
+ * uploader or downloader in Listener. Must be implemented outside.
+ *
* @author bjoern
*/
-public interface IncomingEvent {
- void incomingUploader(Uploader uploader) throws IOException;
- void incomingDownloader(Downloader downloader) throws IOException;
+public interface IncomingEvent
+{
+ void incomingUploader( Uploader uploader ) throws IOException;
+
+ void incomingDownloader( Downloader downloader ) throws IOException;
}
diff --git a/src/main/java/org/openslx/filetransfer/Listener.java b/src/main/java/org/openslx/filetransfer/Listener.java
index eaa8d4b..ec3372f 100644
--- a/src/main/java/org/openslx/filetransfer/Listener.java
+++ b/src/main/java/org/openslx/filetransfer/Listener.java
@@ -7,7 +7,8 @@ import javax.net.ssl.SSLSocket;
// TODO: (all files) apply formatting using strg+shift+f *after* importing scheme from ./extras/
-public class Listener extends Thread {
+public class Listener extends Thread
+{
private IncomingEvent incomingEvent;
/*
private static String pathToKeyStore =
@@ -16,79 +17,77 @@ public class Listener extends Thread {
private SSLContext context;
private int port;
-
- /***********************************************************************//**
+ /***********************************************************************/
+ /**
* Constructor for class Listener, which gets an instance of IncomingEvent.
+ *
* @param e
*/
- public Listener(IncomingEvent e, SSLContext context, int port) {
+ public Listener( IncomingEvent e, SSLContext context, int port )
+ {
this.incomingEvent = e;
this.context = context;
this.port = port;
- /*
- char[] passphrase = "test123".toCharArray();
- KeyStore keystore = KeyStore.getInstance("JKS");
- keystore.load(new FileInputStream(pathToKeyStore), passphrase);
- KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
- kmf.init(keystore, passphrase);
- context = SSLContext.getInstance("SSLv3");
- KeyManager[] keyManagers = kmf.getKeyManagers();
-
- context.init(keyManagers, null, null);
- */
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Method listen, should run from Master Server. Listen for incoming
* connection, and start Downloader or Uploader.
+ *
* @throws Exception
*/
- private void listen() throws Exception { // TODO: Handle exceptions in function to keep going
- SSLServerSocketFactory sslServerSocketFactory = context.getServerSocketFactory();
- SSLServerSocket welcomeSocket =
- (SSLServerSocket) sslServerSocketFactory.createServerSocket(this.port);
-
- while (!isInterrupted()) {
- SSLSocket connectionSocket = (SSLSocket) welcomeSocket.accept();
- connectionSocket.setSoTimeout(2000); // 2 second timeout enough? Maybe even use a small thread pool for handling accepted connections
- // TODO: Handle SocketTimeoutException for all reads and writes in Downloader and Uploader
-
- byte[] b = new byte[1];
- int length = connectionSocket.getInputStream().read(b);
-
- System.out.println(length);
-
- // Ascii - Code: 'U' = 85 ; 'D' = 68. TODO: byte constant as class member
- if (b[0] == 85) {
- System.out.println("U erkannt --> Downloader starten"); // TODO: Use Logger (see masterserver code for example)
- // --> start Downloader(socket).
- Downloader d = new Downloader(connectionSocket);
- incomingEvent.incomingDownloader(d);
- }
- else if (b[0] == 68) {
- System.out.println("D erkannt --> Uploader starten");
- // --> start Uploader(socket).
- Uploader u = new Uploader(connectionSocket);
- incomingEvent.incomingUploader(u);
-
- }
- else {
- System.out.println("Müll empfangen");
- connectionSocket.close();
- }
+ private void listen()
+ {
+ try {
+ SSLServerSocketFactory sslServerSocketFactory = context.getServerSocketFactory();
+ SSLServerSocket welcomeSocket =
+ (SSLServerSocket)sslServerSocketFactory.createServerSocket( this.port );
+
+ while ( !isInterrupted() ) {
+ SSLSocket connectionSocket = (SSLSocket)welcomeSocket.accept();
+ connectionSocket.setSoTimeout( 2000 ); // 2 second timeout enough? Maybe even use a small thread pool for handling accepted connections
+ // TODO: Handle SocketTimeoutException for all reads and writes in Downloader and Uploader
+
+ byte[] b = new byte[ 1 ];
+ int length = connectionSocket.getInputStream().read( b );
+
+ System.out.println( length );
+
+ // Ascii - Code: 'U' = 85 ; 'D' = 68. TODO: byte constant as class member
+ if ( b[0] == 85 ) {
+ System.out.println( "U erkannt --> Downloader starten" ); // TODO: Use Logger (see masterserver code for example)
+ // --> start Downloader(socket).
+ Downloader d = new Downloader( connectionSocket );
+ incomingEvent.incomingDownloader( d );
+ }
+ else if ( b[0] == 68 ) {
+ System.out.println( "D erkannt --> Uploader starten" );
+ // --> start Uploader(socket).
+ Uploader u = new Uploader( connectionSocket );
+ incomingEvent.incomingUploader( u );
+ }
+ else {
+ System.out.println( "Müll empfangen" );
+ connectionSocket.close();
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace(); // same as writing to System.err.println(e.toString).
}
}
-
+
public int getPort()
{
return this.port;
}
@Override
- public void run() {
+ public void run()
+ {
try {
this.listen();
- } catch (Exception e) {
+ } catch ( Exception e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
diff --git a/src/main/java/org/openslx/filetransfer/Uploader.java b/src/main/java/org/openslx/filetransfer/Uploader.java
index befa9bf..5aaa7ef 100644
--- a/src/main/java/org/openslx/filetransfer/Uploader.java
+++ b/src/main/java/org/openslx/filetransfer/Uploader.java
@@ -16,7 +16,8 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
-public class Uploader {
+public class Uploader
+{
// Some member variables.
private SSLSocketFactory sslSocketFactory;
private SSLSocket satelliteSocket;
@@ -25,244 +26,303 @@ public class Uploader {
private String TOKEN = null;
private String RANGE = null;
private String ERROR = null;
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Constructor for satellite uploader.
* Tries to connect to specific ip and port and sending type of action.
+ *
* @param ip
* @param port
- * @throws IOException
- * @throws KeyStoreException
- * @throws CertificateException
- * @throws NoSuchAlgorithmException
- * @throws KeyManagementException
- * @throws UnknownHostException
+ * @throws IOException
+ * @throws KeyStoreException
+ * @throws CertificateException
+ * @throws NoSuchAlgorithmException
+ * @throws KeyManagementException
+ * @throws UnknownHostException
*/
- public Uploader(String ip, int port, SSLContext context) throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, KeyManagementException {
- sslSocketFactory = context.getSocketFactory();
-
- satelliteSocket = (SSLSocket) sslSocketFactory.createSocket(ip, port);
-
- dataToServer = new DataOutputStream(satelliteSocket.getOutputStream());
- dataToServer.writeByte('U');
- dataFromServer = new DataInputStream(satelliteSocket.getInputStream());
+ public Uploader( String ip, int port, SSLContext context )
+ {
+ try {
+ sslSocketFactory = context.getSocketFactory();
+
+ satelliteSocket = (SSLSocket)sslSocketFactory.createSocket( ip, port );
+
+ dataToServer = new DataOutputStream( satelliteSocket.getOutputStream() );
+ dataToServer.writeByte( 'U' );
+ dataFromServer = new DataInputStream( satelliteSocket.getInputStream() );
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Constructor for master uploader.
* Sends back the socket for datatransfer.
- * @throws IOException
+ *
+ * @throws IOException
*/
- public Uploader(SSLSocket socket) throws IOException {
- satelliteSocket = socket;
- dataToServer = new DataOutputStream(satelliteSocket.getOutputStream());
- dataFromServer = new DataInputStream(satelliteSocket.getInputStream());
+ public Uploader( SSLSocket socket )
+ {
+ try {
+ satelliteSocket = socket;
+ dataToServer = new DataOutputStream( satelliteSocket.getOutputStream() );
+ dataFromServer = new DataInputStream( satelliteSocket.getInputStream() );
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Method for sending token from satellite to master.
* Needfull for getting to know what should happens over connection.
+ *
* @param t
*/
- public void sendToken(String token) throws IOException {
- TOKEN = token;
- String sendToken = "TOKEN=" + TOKEN;
- byte[] data = sendToken.getBytes(StandardCharsets.UTF_8);
- dataToServer.writeByte(data.length);
- dataToServer.write(data);
+ public Boolean sendToken( String token )
+ {
+ try {
+ TOKEN = token;
+ String sendToken = "TOKEN=" + TOKEN;
+ byte[] data = sendToken.getBytes( StandardCharsets.UTF_8 );
+ dataToServer.writeByte( data.length );
+ dataToServer.write( data );
+ } catch (IOException e) {
+ e.printStackTrace();
+ return false;
+ }
+ return true;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Getter for TOKEN.
*/
- public String getToken() {
- if (TOKEN != null)
- return TOKEN;
- return null;
+ public String getToken()
+ {
+ return TOKEN;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Method to send range of the file, which should be uploaded.
* Helpful for knowing how much was already uploaded if
* connection aborts.
+ *
* @param a
* @param b
- * @throws IOException
*/
- public void sendRange(int a, int b) throws IOException {
- RANGE = a + ":" + b;
- String sendRange = "RANGE=" + RANGE;
- byte[] data = sendRange.getBytes(StandardCharsets.UTF_8);
- dataToServer.writeByte(data.length);
- dataToServer.write(data);
- dataToServer.writeByte(0);
+ public Boolean sendRange( int a, int b )
+ {
+ try {
+ RANGE = a + ":" + b;
+ String sendRange = "RANGE=" + RANGE;
+ byte[] data = sendRange.getBytes( StandardCharsets.UTF_8 );
+ dataToServer.writeByte( data.length );
+ dataToServer.write( data );
+ dataToServer.writeByte( 0 );
+ } catch (IOException e) {
+ e.printStackTrace();
+ return false;
+ }
+ return true;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Getter for RANGE.
+ *
* @return
*/
- public String getRange() {
- if (RANGE != null)
- return RANGE;
- return null;
+ public String getRange()
+ {
+ return RANGE;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Getter for beginning of RANGE.
+ *
* @return
*/
- public int getStartOfRange() {
- if (RANGE != null) {
- String[] splitted = RANGE.split(":");
- return Integer.parseInt(splitted[0]);
+ public int getStartOfRange()
+ {
+ if ( RANGE != null ) {
+ String[] splitted = RANGE.split( ":" );
+ return Integer.parseInt( splitted[0] );
}
return -1;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Getter for end of RANGE.
+ *
* @return
*/
- public int getEndOfRange() {
- if (RANGE != null) {
- String[] splitted = RANGE.split(":");
- return Integer.parseInt(splitted[1]);
+ public int getEndOfRange()
+ {
+ if ( RANGE != null ) {
+ String[] splitted = RANGE.split( ":" );
+ return Integer.parseInt( splitted[1] );
}
return -1;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Method for returning difference of current Range.
+ *
* @return
*/
- public int getDiffOfRange() {
- if (getStartOfRange() == -1 || getEndOfRange() == -1) {
+ public int getDiffOfRange()
+ {
+ if ( getStartOfRange() == -1 || getEndOfRange() == -1 ) {
return -1;
}
- int diff = Math.abs(getEndOfRange() - getStartOfRange());
+ int diff = Math.abs( getEndOfRange() - getStartOfRange() );
return diff;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Method for reading MetaData, like TOKEN and FileRange.
* Split incoming bytes after first '=' and store value to specific
* variable.
- * @throws IOException
+ *
*/
- public Boolean readMetaData() {
+ public Boolean readMetaData()
+ {
try {
- while (true) {
- byte[] incoming = new byte[255];
-
+ while ( true ) {
+ byte[] incoming = new byte[ 255 ];
+
// First get length.
- dataFromServer.read(incoming, 0, 1);
+ dataFromServer.read( incoming, 0, 1 );
int length = incoming[0];
// System.out.println("length: " + length);
-
- if (length == 0) // Stop if 0 was read.
+
+ if ( length == 0 ) // Stop if 0 was read.
break;
-
+
/**
- * Read the next available bytes and split by '=' for
- * getting TOKEN or RANGE.
+ * Read the next available bytes and split by '=' for
+ * getting TOKEN or RANGE.
*/
int hasRead = 0;
- while (hasRead < length) {
- int ret = dataFromServer.read(incoming, hasRead, length - hasRead);
- if (ret == -1) {
- System.out.println("Error in reading Metadata occured!");
+ while ( hasRead < length ) {
+ int ret = dataFromServer.read( incoming, hasRead, length - hasRead );
+ if ( ret == -1 ) {
+ System.out.println( "Error in reading Metadata occured!" );
return false;
}
hasRead += ret;
}
- String data = new String(incoming, "UTF-8");
+ String data = new String( incoming, "UTF-8" );
// System.out.println(data);
-
- String[] splitted = data.split("=");
+
+ String[] splitted = data.split( "=" );
// System.out.println("splitted[0]: " + splitted[0]);
// System.out.println("splitted[1]: " + splitted[1]);
- if (splitted[0] != null && splitted[0].equals("TOKEN")) {
- if (splitted[1] != null)
+ if ( splitted[0] != null && splitted[0].equals( "TOKEN" ) ) {
+ if ( splitted[1] != null )
TOKEN = splitted[1];
- System.out.println("TOKEN: " + TOKEN);
+ System.out.println( "TOKEN: " + TOKEN );
}
- else if (splitted[0].equals("RANGE")) {
- if (splitted[1] != null)
+ else if ( splitted[0].equals( "RANGE" ) ) {
+ if ( splitted[1] != null )
RANGE = splitted[1];
- System.out.println("RANGE: " + RANGE);
+ System.out.println( "RANGE: " + RANGE );
}
- else if (splitted[0].equals("ERROR")) {
- if (splitted[1] != null)
+ else if ( splitted[0].equals( "ERROR" ) ) {
+ if ( splitted[1] != null )
ERROR = splitted[1];
- System.err.println("ERROR: " + ERROR);
+ System.err.println( "ERROR: " + ERROR );
this.close();
return false;
}
}
- } catch (IOException e) {
+ } catch ( IOException e ) {
e.printStackTrace();
return false;
}
return true;
}
- /***********************************************************************//**
+ /***********************************************************************/
+ /**
* Method for sending File with filename.
+ *
* @param filename
- * @throws IOException
*/
- public Boolean sendFile(String filename) {
+ public Boolean sendFile( String filename )
+ {
try {
- RandomAccessFile file = new RandomAccessFile(new File(filename), "r");
-
- if (getStartOfRange() == -1) {
+ RandomAccessFile file = new RandomAccessFile( new File( filename ), "r" );
+
+ if ( getStartOfRange() == -1 ) {
file.close();
return false;
}
- file.seek(getStartOfRange());
-
- byte[] data = new byte[255];
+ file.seek( getStartOfRange() );
+
+ byte[] data = new byte[ 255 ];
int hasRead = 0;
int length = getDiffOfRange();
- System.out.println("diff of Range: " + length);
- while (hasRead < length) {
- int ret = file.read(data, hasRead, length - hasRead);
- if (ret == -1) {
- System.out.println("Error occured in Uploader.sendFile(),"
- + " while reading from File to send.");
+ System.out.println( "diff of Range: " + length );
+ while ( hasRead < length ) {
+ int ret = file.read( data, hasRead, length - hasRead );
+ if ( ret == -1 ) {
+ System.out.println( "Error occured in Uploader.sendFile(),"
+ + " while reading from File to send." );
file.close();
return false;
}
hasRead += ret;
}
file.close();
-
- dataToServer.write(data, 0, length);
- } catch (Exception e) {
+
+ dataToServer.write( data, 0, length );
+ } catch ( Exception e ) {
e.printStackTrace();
return false;
}
return true;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Method for sending error Code to server. For example in case of wrong
* token, send code for wrong token.
- * @throws IOException
+ *
*/
- public void sendErrorCode(String errString) throws IOException {
- String sendError = "ERROR=" + errString;
- byte[] data = sendError.getBytes(StandardCharsets.UTF_8);
- dataToServer.writeByte(data.length);
- dataToServer.write(data);
+ public Boolean sendErrorCode( String errString )
+ {
+ try {
+ String sendError = "ERROR=" + errString;
+ byte[] data = sendError.getBytes( StandardCharsets.UTF_8 );
+ dataToServer.writeByte( data.length );
+ dataToServer.write( data );
+ } catch (IOException e) {
+ e.printStackTrace();
+ return false;
+ }
+ return true;
}
-
- /***********************************************************************//**
+
+ /***********************************************************************/
+ /**
* Method for closing connection, if download has finished.
- * @throws IOException
+ *
*/
- public void close() throws IOException {
- this.satelliteSocket.close();
+ public void close()
+ {
+ try {
+ this.satelliteSocket.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
}
}