diff options
Diffstat (limited to 'src/main/java/org/openslx/filetransfer/util/OutgoingTransferBase.java')
-rw-r--r-- | src/main/java/org/openslx/filetransfer/util/OutgoingTransferBase.java | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/filetransfer/util/OutgoingTransferBase.java b/src/main/java/org/openslx/filetransfer/util/OutgoingTransferBase.java new file mode 100644 index 0000000..12deddb --- /dev/null +++ b/src/main/java/org/openslx/filetransfer/util/OutgoingTransferBase.java @@ -0,0 +1,121 @@ +package org.openslx.filetransfer.util; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; + +import org.apache.log4j.Logger; +import org.openslx.bwlp.thrift.iface.TransferInformation; +import org.openslx.filetransfer.Uploader; + +public abstract class OutgoingTransferBase extends AbstractTransfer +{ + + /* + * Constants + */ + + private static final Logger LOGGER = Logger.getLogger( OutgoingTransferBase.class ); + + private static final long INACTIVITY_TIMEOUT = TimeUnit.MINUTES.toMillis( 5 ); + + /* + * Overridable constants + */ + + protected static int MAX_CONNECTIONS_PER_TRANSFER = 2; + + /* + * Class members + */ + + /** + * Remote peer is downloading, so we have Uploaders + */ + private final List<Uploader> uploads = new ArrayList<>(); + + /** + * File being uploaded + */ + private final File sourceFile; + + private final TransferInformation transferInformation; + + public OutgoingTransferBase( String transferId, File sourceFile, int plainPort, int sslPort ) + { + super( transferId ); + this.sourceFile = sourceFile; + this.transferInformation = new TransferInformation( transferId, plainPort, sslPort ); + } + + /** + * Add another connection for this file transfer. + * + * @param connection + * @return true if the connection is accepted, false if it should be + * discarded + */ + public synchronized boolean addConnection( final Uploader connection, ExecutorService pool ) + { + synchronized ( uploads ) { + if ( uploads.size() >= MAX_CONNECTIONS_PER_TRANSFER ) + return false; + uploads.add( connection ); + } + return runConnectionInternal( connection, pool ); + } + + protected boolean runConnectionInternal( final Uploader connection, ExecutorService pool ) + { + try { + pool.execute( new Runnable() { + @Override + public void run() + { + boolean ret = connection.upload( sourceFile.getAbsolutePath() ); + synchronized ( uploads ) { + uploads.remove( connection ); + } + if ( ret && uploads.isEmpty() && potentialFinishTime.get() == 0 ) { + potentialFinishTime.set( System.currentTimeMillis() ); + } + lastActivityTime.set( System.currentTimeMillis() ); + } + } ); + } catch ( Exception e ) { + LOGGER.warn( "threadpool rejected the incoming file transfer", e ); + synchronized ( uploads ) { + uploads.remove( connection ); + } + return false; + } + return true; + } + + @Override + public TransferInformation getTransferInfo() + { + return transferInformation; + } + + @Override + public final boolean isActive() + { + return uploads.size() > 0 || lastActivityTime.get() + INACTIVITY_TIMEOUT > System.currentTimeMillis(); + } + + @Override + public void cancel() + { + // Void + } + + @Override + public final int getActiveConnectionCount() + { + return uploads.size(); + } + +} |