X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fde%2Ftodesbaum%2Futil%2Ffreenet%2Ffcp2%2FConnection.java;h=d8a8b2d0796fff54dacae1bbe7b486c1b7e310a8;hb=7dee6ec51d770004481ef6f4eab96e8f03fe0af9;hp=9932d2558d5177a8f768734f976db1d04c56ae61;hpb=85d5c89f25bed4fc3002eaaaa98e7ca4992fa2d6;p=jSite.git
diff --git a/src/de/todesbaum/util/freenet/fcp2/Connection.java b/src/de/todesbaum/util/freenet/fcp2/Connection.java
index 9932d25..d8a8b2d 100644
--- a/src/de/todesbaum/util/freenet/fcp2/Connection.java
+++ b/src/de/todesbaum/util/freenet/fcp2/Connection.java
@@ -1,5 +1,5 @@
/*
- * todesbaum-lib -
+ * todesbaum-lib -
* Copyright (C) 2006 David Roden
*
* This program is free software; you can redistribute it and/or modify
@@ -34,11 +34,12 @@ import java.util.List;
import de.todesbaum.util.io.Closer;
import de.todesbaum.util.io.LineInputStream;
import de.todesbaum.util.io.StreamCopier;
+import de.todesbaum.util.io.StreamCopier.ProgressListener;
import de.todesbaum.util.io.TempFileInputStream;
/**
* A physical connection to a Freenet node.
- *
+ *
* @author David Roden <droden@gmail.com>
* @version $Id$
*/
@@ -71,9 +72,12 @@ public class Connection {
/** The NodeHello message sent by the node on connect. */
protected Message nodeHello;
+ /** The temp directory to use. */
+ private String tempDirectory;
+
/**
* Creates a new connection to the specified node with the specified name.
- *
+ *
* @param node
* The node to connect to
* @param name
@@ -86,7 +90,7 @@ public class Connection {
/**
* Adds a listener that gets notified on connection events.
- *
+ *
* @param connectionListener
* The listener to add
*/
@@ -97,7 +101,7 @@ public class Connection {
/**
* Removes a listener from the list of registered listeners. Only the first
* matching listener is removed.
- *
+ *
* @param connectionListener
* The listener to remove
* @see List#remove(java.lang.Object)
@@ -108,12 +112,12 @@ public class Connection {
/**
* Notifies listeners about a received message.
- *
+ *
* @param message
* The received message
*/
protected void fireMessageReceived(Message message) {
- for (ConnectionListener connectionListener: connectionListeners) {
+ for (ConnectionListener connectionListener : connectionListeners) {
connectionListener.messageReceived(this, message);
}
}
@@ -122,14 +126,14 @@ public class Connection {
* Notifies listeners about the loss of the connection.
*/
protected void fireConnectionTerminated() {
- for (ConnectionListener connectionListener: connectionListeners) {
+ for (ConnectionListener connectionListener : connectionListeners) {
connectionListener.connectionTerminated(this);
}
}
/**
* Returns the name of the connection.
- *
+ *
* @return The name of the connection
*/
public String getName() {
@@ -137,8 +141,19 @@ public class Connection {
}
/**
+ * Sets the temp directory to use for creation of temporary files.
+ *
+ * @param tempDirectory
+ * The temp directory to use, or {@code null} to use the default
+ * temp directory
+ */
+ public void setTempDirectory(String tempDirectory) {
+ this.tempDirectory = tempDirectory;
+ }
+
+ /**
* Connects to the node.
- *
+ *
* @return true
if the connection succeeded and the node
* returned a NodeHello message
* @throws IOException
@@ -180,7 +195,7 @@ public class Connection {
/**
* Returns whether this connection is still connected to the node.
- *
+ *
* @return true
if this connection is still valid,
* false
otherwise
*/
@@ -190,7 +205,7 @@ public class Connection {
/**
* Returns the NodeHello message the node sent on connection.
- *
+ *
* @return The NodeHello message of the node
*/
public Message getNodeHello() {
@@ -201,34 +216,15 @@ public class Connection {
* Disconnects from the node.
*/
public void disconnect() {
- if (nodeWriter != null) {
- try {
- nodeWriter.close();
- } catch (IOException ioe1) {
- }
- nodeWriter = null;
- }
- if (nodeOutputStream != null) {
- try {
- nodeOutputStream.close();
- } catch (IOException ioe1) {
- }
- nodeOutputStream = null;
- }
- if (nodeInputStream != null) {
- try {
- nodeInputStream.close();
- } catch (IOException ioe1) {
- }
- nodeInputStream = null;
- }
- if (nodeSocket != null) {
- try {
- nodeSocket.close();
- } catch (IOException ioe1) {
- }
- nodeSocket = null;
- }
+ Closer.close(nodeWriter);
+ nodeWriter = null;
+ Closer.close(nodeOutputStream);
+ nodeOutputStream = null;
+ Closer.close(nodeInputStream);
+ nodeInputStream = null;
+ nodeInputStream = null;
+ Closer.close(nodeSocket);
+ nodeSocket = null;
synchronized (this) {
notify();
}
@@ -237,7 +233,7 @@ public class Connection {
/**
* Executes the specified command.
- *
+ *
* @param command
* The command to execute
* @throws IllegalStateException
@@ -246,6 +242,22 @@ public class Connection {
* if an I/O error occurs
*/
public synchronized void execute(Command command) throws IllegalStateException, IOException {
+ execute(command, null);
+ }
+
+ /**
+ * Executes the specified command.
+ *
+ * @param command
+ * The command to execute
+ * @param progressListener
+ * A progress listener for a payload transfer
+ * @throws IllegalStateException
+ * if the connection is not connected
+ * @throws IOException
+ * if an I/O error occurs
+ */
+ public synchronized void execute(Command command, ProgressListener progressListener) throws IllegalStateException, IOException {
if (nodeSocket == null) {
throw new IllegalStateException("connection is not connected");
}
@@ -257,7 +269,7 @@ public class Connection {
InputStream payloadInputStream = null;
try {
payloadInputStream = command.getPayload();
- StreamCopier.copy(payloadInputStream, nodeOutputStream, command.getPayloadLength());
+ StreamCopier.copy(payloadInputStream, nodeOutputStream, command.getPayloadLength(), progressListener);
} finally {
Closer.close(payloadInputStream);
}
@@ -269,7 +281,7 @@ public class Connection {
* The reader thread for this connection. This is essentially a thread that
* reads lines from the node, creates messages from them and notifies
* listeners about the messages.
- *
+ *
* @author David Roden <droden@gmail.com>
* @version $Id$
*/
@@ -281,7 +293,7 @@ public class Connection {
/**
* Creates a new reader that reads from the specified input stream.
- *
+ *
* @param nodeInputStream
* The input stream to read from
*/
@@ -293,6 +305,7 @@ public class Connection {
* Main loop of the reader. Lines are read and converted into
* {@link Message} objects.
*/
+ @SuppressWarnings("synthetic-access")
public void run() {
LineInputStream nodeReader = null;
try {
@@ -313,7 +326,7 @@ public class Connection {
/* need to read message from stream now */
File tempFile = null;
try {
- tempFile = File.createTempFile("fcpv2", "data");
+ tempFile = File.createTempFile("fcpv2", "data", (tempDirectory != null) ? new File(tempDirectory) : null);
tempFile.deleteOnExit();
FileOutputStream tempFileOutputStream = new FileOutputStream(tempFile);
long dataLength = Long.parseLong(message.get("DataLength"));