summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/filetransfer/Listener.java
diff options
context:
space:
mode:
authorSimon Rettberg2015-06-05 11:25:50 +0200
committerSimon Rettberg2015-06-05 11:25:50 +0200
commit693392fe6c0022e7ec5060192ee322c7753b0d90 (patch)
tree855245927778c6b069714909929684d1da7d449f /src/main/java/org/openslx/filetransfer/Listener.java
parentCleanup thrift shandling stuff (diff)
downloadmaster-sync-shared-693392fe6c0022e7ec5060192ee322c7753b0d90.tar.gz
master-sync-shared-693392fe6c0022e7ec5060192ee322c7753b0d90.tar.xz
master-sync-shared-693392fe6c0022e7ec5060192ee322c7753b0d90.zip
Changes for Dozmod v1.1
Diffstat (limited to 'src/main/java/org/openslx/filetransfer/Listener.java')
-rw-r--r--src/main/java/org/openslx/filetransfer/Listener.java158
1 files changed, 106 insertions, 52 deletions
diff --git a/src/main/java/org/openslx/filetransfer/Listener.java b/src/main/java/org/openslx/filetransfer/Listener.java
index 699c61d..e4e99e9 100644
--- a/src/main/java/org/openslx/filetransfer/Listener.java
+++ b/src/main/java/org/openslx/filetransfer/Listener.java
@@ -1,29 +1,35 @@
package org.openslx.filetransfer;
import java.io.IOException;
+import java.net.ServerSocket;
+import java.net.Socket;
import javax.net.ssl.SSLContext;
-import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
-import javax.net.ssl.SSLSocket;
import org.apache.log4j.Logger;
-public class Listener extends Thread
+public class Listener
{
- private IncomingEvent incomingEvent;
- private SSLContext context;
- private int port;
- final private int U = 85; // hex - code 'U' = 85.
- final private int D = 68; // hex - code 'D' = 68.
+ private final IncomingEvent incomingEvent;
+ private final SSLContext context;
+ private final int port;
+ private ServerSocket listenSocket = null;
+ private Thread acceptThread = null;
+ private static final byte U = 85; // hex - code 'U' = 85.
+ private static final byte D = 68; // hex - code 'D' = 68.
private static Logger log = Logger.getLogger( Listener.class );
/***********************************************************************/
/**
- * Constructor for class Listener, which gets an instance of IncomingEvent.
+ * File transfer listener. This is the active side, opening a port and
+ * waiting for incoming connections.
*
- * @param e
+ * @param e the event handler for incoming connections
+ * @param context the SSL context used for encryption; if null, unencrypted connections will be
+ * used
+ * @param port port to listen on
*/
public Listener( IncomingEvent e, SSLContext context, int port )
{
@@ -38,48 +44,74 @@ public class Listener extends Thread
* connection, and start Downloader or Uploader.
*
*/
- private void listen()
+ private boolean listen()
{
- SSLServerSocket welcomeSocket = null;
try {
- SSLServerSocketFactory sslServerSocketFactory = context.getServerSocketFactory();
- welcomeSocket = (SSLServerSocket)sslServerSocketFactory.createServerSocket( this.port );
+ if ( this.context == null ) {
+ listenSocket = new ServerSocket( this.port );
+ } else {
+ SSLServerSocketFactory sslServerSocketFactory = context.getServerSocketFactory();
+ listenSocket = sslServerSocketFactory.createServerSocket( this.port );
+ }
+ } catch ( Exception e ) {
+ log.error( "Cannot listen on port " + this.port );
+ return false;
+ }
+ return true;
+ }
- 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
+ private void run()
+ {
+ final Listener instance = this;
+ acceptThread = new Thread() {
+ @Override
+ public void run()
+ {
+ try {
+ while ( !isInterrupted() ) {
+ Socket connectionSocket = null;
+ try {
+ connectionSocket = listenSocket.accept();
+ connectionSocket.setSoTimeout( 2000 ); // 2 second timeout enough? Maybe even use a small thread pool for handling accepted connections
- byte[] b = new byte[ 1 ];
- int length = connectionSocket.getInputStream().read( b );
+ byte[] b = new byte[ 1 ];
+ int length = connectionSocket.getInputStream().read( b );
- log.debug( "Length (Listener): " + length );
+ connectionSocket.setSoTimeout( 10000 );
- if ( b[0] == U ) {
- log.debug( "recognized U --> starting Downloader" );
- // --> start Downloader(socket).
- Downloader d = new Downloader( connectionSocket );
- incomingEvent.incomingDownloader( d );
- }
- else if ( b[0] == D ) {
- log.debug( "recognized D --> starting Uploader" );
- // --> start Uploader(socket).
- Uploader u = new Uploader( connectionSocket );
- incomingEvent.incomingUploader( u );
- }
- else {
- log.debug( "Got invalid option ... close connection" );
- connectionSocket.close();
+ log.debug( "Length (Listener): " + length );
+
+ if ( b[0] == U ) {
+ log.debug( "recognized U --> starting Downloader" );
+ // --> start Downloader(socket).
+ Downloader d = new Downloader( connectionSocket );
+ incomingEvent.incomingUploadRequest( d );
+ }
+ else if ( b[0] == D ) {
+ log.debug( "recognized D --> starting Uploader" );
+ // --> start Uploader(socket).
+ Uploader u = new Uploader( connectionSocket );
+ incomingEvent.incomingDownloadRequest( u );
+ }
+ else {
+ log.debug( "Got invalid option ... close connection" );
+ connectionSocket.close();
+ }
+ } catch ( IOException e ) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ } finally {
+ synchronized ( instance ) {
+ Transfer.safeClose( listenSocket );
+ listenSocket = null;
+ }
}
}
- } catch ( Exception e ) {
- e.printStackTrace(); // same as writing to System.err.println(e.toString).
- } finally {
- try {
- welcomeSocket.close();
- } catch ( IOException e ) {
- // Nothing we can do
- }
- }
+ };
+ acceptThread.start();
+ log.info( "Starting to accept " + ( this.context == null ? "UNENCRYPTED" : "encrypted" ) + " connections on port " + this.port );
}
public int getPort()
@@ -87,14 +119,36 @@ public class Listener extends Thread
return this.port;
}
- @Override
- public void run()
+ /**
+ * Check whether this listener is running.
+ *
+ * @return true if this instance is currently listening for connections and runs the accept loop.
+ */
+ public synchronized boolean isRunning()
{
- try {
- this.listen();
- } catch ( Exception e ) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
+ return acceptThread != null && acceptThread.isAlive() && listenSocket != null && !listenSocket.isClosed();
+ }
+
+ /**
+ * Check whether this listener was started.
+ *
+ * @return true if this instance was started before, but might have been stopped already.
+ */
+ public synchronized boolean wasStarted()
+ {
+ return acceptThread != null;
+ }
+
+ /**
+ * Start this listener.
+ *
+ * @return true if the port could be openened and the accepting thread was started
+ */
+ public synchronized boolean start()
+ {
+ if ( !this.listen() )
+ return false;
+ this.run();
+ return true;
}
}