blob: 31c219aac2126c77237b274a0fea8f53d8f6d8de (
plain) (
tree)
|
|
package org.openslx.bwlp.sat.fileserv;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import org.apache.log4j.Logger;
import org.openslx.bwlp.sat.util.Constants;
import org.openslx.filetransfer.Uploader;
public class OutgoingDataTransfer extends AbstractTransfer {
private static final Logger LOGGER = Logger.getLogger(OutgoingDataTransfer.class);
/**
* Remote peer is downloading, so we have Uploaders
*/
private List<Uploader> uploads = new ArrayList<>();
private final File sourceFile;
private boolean isCanceled = false;
public OutgoingDataTransfer(String uuid, File file) {
super(uuid);
this.sourceFile = file;
}
/**
* Called periodically if this is a transfer from the master server, so we
* can make sure the transfer is running.
*/
public void heartBeat(ExecutorService pool) {
// TODO
}
/**
* 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) {
if (isCanceled)
return false;
potentialFinishTime.set(0);
synchronized (uploads) {
if (uploads.size() > Constants.MAX_CONNECTIONS_PER_TRANSFER)
return false;
uploads.add(connection);
}
try {
pool.execute(new Runnable() {
@Override
public void run() {
potentialFinishTime.set(0);
boolean ret = connection.upload(sourceFile.getAbsolutePath());
synchronized (uploads) {
uploads.remove(connection);
}
if (ret && uploads.isEmpty()) {
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 synchronized void cancel() {
isCanceled = true;
synchronized (uploads) {
for (Uploader u : uploads) {
u.cancel();
}
}
}
@Override
public boolean isActive() {
return !isCanceled;
}
@Override
public int getActiveConnectionCount() {
return uploads.size();
}
}
|