blob: 7180fe3d4d67cc5f6d81a97500e743f1f35affa8 (
plain) (
tree)
|
|
package org.openslx.bwlp.sat.fileserv;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;
import org.apache.log4j.Logger;
import org.openslx.bwlp.sat.util.Constants;
import org.openslx.bwlp.thrift.iface.TransferState;
import org.openslx.filetransfer.Uploader;
public class ActiveDownload {
private static final Logger LOGGER = Logger.getLogger(ActiveDownload.class);
/**
* How many concurrent connections per upload
*/
private static final int MAX_CONNECTIONS = Math.max(Constants.MAX_DOWNLOADS / 4, 1);
/**
* Self reference for inner classes.
*/
private final ActiveDownload activeDownload = this;
/**
* This is a download, so we have uploaders
*/
private List<Uploader> uploads = new ArrayList<>();
private final File sourceFile;
private final long fileSize;
private final String transferId;
/**
* TransferState of this upload
*/
private TransferState state = TransferState.IDLE;
public ActiveDownload(String uuid, File file) {
this.transferId = uuid;
this.sourceFile = file;
this.fileSize = file.length();
}
/**
* Add another connection for this file transfer. Currently only one
* connection is allowed, but this might change in the future.
*
* @param connection
* @return true if the connection is accepted, false if it should be
* discarded
*/
public synchronized boolean addConnection(final Uploader connection, ThreadPoolExecutor pool) {
synchronized (uploads) {
if (uploads.size() > MAX_CONNECTIONS)
return false;
uploads.add(connection);
}
try {
pool.execute(new Runnable() {
@Override
public void run() {
connection.upload(sourceFile.getAbsolutePath());
}
});
} catch (Exception e) {
LOGGER.warn("threadpool rejected the incoming file transfer", e);
return false;
}
return true;
}
public boolean isComplete() {
// TODO Auto-generated method stub
return false;
}
public String getId() {
return transferId;
}
}
|