/** The temp directory. */
private String tempDirectory;
+ /** The current connection. */
+ private Connection connection;
+
+ /** Whether the insert is cancelled. */
+ private volatile boolean cancelled = false;
+
/**
* Adds a listener to the list of registered listeners.
*
* Starts the insert.
*/
public void start() {
+ cancelled = false;
fileScanner = new FileScanner(project);
fileScanner.addFileScannerListener(this);
new Thread(fileScanner).start();
}
/**
+ * Stops the current insert.
+ */
+ public void stop() {
+ cancelled = true;
+ synchronized (lockObject) {
+ if (connection != null) {
+ connection.disconnect();
+ }
+ }
+ }
+
+ /**
* Creates an input stream that delivers the given file, replacing edition
* tokens in the file’s content, if necessary.
*
List<String> files = fileScanner.getFiles();
/* create connection to node */
- Connection connection = freenetInterface.getConnection("project-insert-" + random + counter++);
+ synchronized (lockObject) {
+ connection = freenetInterface.getConnection("project-insert-" + random + counter++);
+ }
connection.setTempDirectory(tempDirectory);
boolean connected = false;
Throwable cause = null;
cause = e1;
}
- if (!connected) {
- fireProjectInsertFinished(false, cause);
+ if (!connected || cancelled) {
+ fireProjectInsertFinished(false, cancelled ? new AbortedException() : cause);
return;
}
boolean success = false;
boolean finished = false;
boolean disconnected = false;
- while (!finished) {
+ while (!finished && !cancelled) {
Message message = client.readMessage();
finished = (message == null) || (disconnected = client.isDisconnected());
if (firstMessage) {
project.setEdition(newEdition);
project.setLastInsertionTime(System.currentTimeMillis());
}
- fireProjectInsertFinished(success, disconnected ? new IOException("Connection terminated") : null);
+ fireProjectInsertFinished(success, cancelled ? new AbortedException() : (disconnected ? new IOException("Connection terminated") : null));
}
//