blob: 917611c7c701c45fd78a547de5c394743c2b0d1a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
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.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(ThreadPoolExecutor pool) {
// TODO
}
/**
* 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) {
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();
}
}
|