summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/filetransfer/Listener.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/openslx/filetransfer/Listener.java')
-rw-r--r--src/main/java/org/openslx/filetransfer/Listener.java38
1 files changed, 24 insertions, 14 deletions
diff --git a/src/main/java/org/openslx/filetransfer/Listener.java b/src/main/java/org/openslx/filetransfer/Listener.java
index a0fc172..fc990fc 100644
--- a/src/main/java/org/openslx/filetransfer/Listener.java
+++ b/src/main/java/org/openslx/filetransfer/Listener.java
@@ -3,6 +3,7 @@ package org.openslx.filetransfer;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
+import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
@@ -11,9 +12,11 @@ import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLException;
import javax.net.ssl.SSLServerSocketFactory;
-import org.apache.log4j.Logger;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
import org.openslx.util.PrioThreadFactory;
public class Listener
@@ -25,11 +28,11 @@ public class Listener
private Thread acceptThread = null;
private final int readTimeoutMs;
private final ExecutorService processingPool = new ThreadPoolExecutor( 0, 8, 5, TimeUnit.MINUTES, new SynchronousQueue<Runnable>(),
- new PrioThreadFactory( "BFTP-Init" ) );
+ new PrioThreadFactory( "BFTP-BS" ) );
- private static final byte CONNECTING_PEER_WANTS_TO_UPLOAD = 85; // hex - code 'U' = 85.
- private static final byte CONNECTING_PEER_WANTS_TO_DOWNLOAD = 68; // hex - code 'D' = 68.
- private static Logger log = Logger.getLogger( Listener.class );
+ private static final byte CONNECTING_PEER_WANTS_TO_UPLOAD = 85; // ASCII 'U' = 85.
+ private static final byte CONNECTING_PEER_WANTS_TO_DOWNLOAD = 68; // ASCII 'D' = 68.
+ private static Logger log = LogManager.getLogger( Listener.class );
/***********************************************************************/
/**
@@ -67,8 +70,10 @@ public class Listener
SSLServerSocketFactory sslServerSocketFactory = context.getServerSocketFactory();
listenSocket = sslServerSocketFactory.createServerSocket();
}
+ listenSocket.setSoTimeout( 5000 );
listenSocket.setReuseAddress( true );
listenSocket.bind( new InetSocketAddress( this.port ) );
+ listenSocket.setSoTimeout( 0 );
} catch ( Exception e ) {
log.error( "Cannot listen on port " + this.port, e );
listenSocket = null;
@@ -82,16 +87,16 @@ public class Listener
if ( acceptThread != null )
return;
final Listener instance = this;
- acceptThread = new Thread( "BFTP-Listen-" + this.port ) {
+ acceptThread = new Thread( "BFTP:" + this.port ) {
@Override
public void run()
{
try {
// Run accept loop in own thread
while ( !isInterrupted() ) {
- Socket acceptedSocket = null;
+ final Socket connection;
try {
- acceptedSocket = listenSocket.accept();
+ connection = listenSocket.accept();
} catch ( SocketTimeoutException e ) {
continue;
} catch ( Exception e ) {
@@ -105,14 +110,12 @@ public class Listener
continue;
}
// Handle each accepted connection in a thread pool
- final Socket connection = acceptedSocket;
Runnable handler = new Runnable() {
@Override
public void run()
{
-
try {
- // Give initial byte signalling mode of operation 5 secs to arrive
+ // Give initial byte signaling mode of operation 5 secs to arrive
connection.setSoTimeout( 5000 );
byte[] b = new byte[ 1 ];
@@ -127,25 +130,32 @@ public class Listener
if ( b[0] == CONNECTING_PEER_WANTS_TO_UPLOAD ) {
// --> start Downloader(socket).
Downloader d = new Downloader( connection );
+ // Will take care of connection cleanup
incomingEvent.incomingUploadRequest( d );
} else if ( b[0] == CONNECTING_PEER_WANTS_TO_DOWNLOAD ) {
// --> start Uploader(socket).
Uploader u = new Uploader( connection );
+ // Will take care of connection cleanup
incomingEvent.incomingDownloadRequest( u );
} else {
- log.debug( "Got invalid init-byte ... close connection" );
+ log.debug( "Got invalid init-byte ... closing connection" );
Transfer.safeClose( connection );
}
+ } catch ( SSLException e ) {
+ Transfer.safeClose( connection );
+ log.warn( "SSL error when acceping client " + connection.getInetAddress().getHostAddress() );
+ } catch ( SocketException e ) {
+ // No reason to log, probably - connection where client did nothing after connecting.
} catch ( Exception e ) {
- log.warn( "Error accepting client", e );
Transfer.safeClose( connection );
+ log.warn( "Error handling client", e );
}
}
};
try {
processingPool.execute( handler );
} catch ( RejectedExecutionException e ) {
- Transfer.safeClose( acceptedSocket );
+ Transfer.safeClose( connection );
}
}
} finally {