add AllData
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 10 Apr 2008 14:48:24 +0000 (14:48 +0000)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 10 Apr 2008 14:48:24 +0000 (14:48 +0000)
git-svn-id: http://trooper/svn/projects/jSite/trunk@694 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/util/fcp/AllData.java [new file with mode: 0644]
src/net/pterodactylus/util/fcp/FcpAdapter.java
src/net/pterodactylus/util/fcp/FcpConnection.java
src/net/pterodactylus/util/fcp/FcpListener.java

diff --git a/src/net/pterodactylus/util/fcp/AllData.java b/src/net/pterodactylus/util/fcp/AllData.java
new file mode 100644 (file)
index 0000000..c2ead16
--- /dev/null
@@ -0,0 +1,98 @@
+/**
+ * © 2008 INA Service GmbH
+ */
+package net.pterodactylus.util.fcp;
+
+import java.io.InputStream;
+
+/**
+ * The “AllData” message carries the payload of a successful {@link ClientGet}
+ * request. You will only received this message if the {@link ClientGet} request
+ * was started with a return type of {@link ReturnType#direct}. If you get this
+ * message and decide that the data is for you, call
+ * {@link #getPayloadInputStream()} to get the data. If an AllData message
+ * passes through all registered {@link FcpListener}s without the payload being
+ * consumed, the payload is discarded!
+ * 
+ * @author <a href="mailto:dr@ina-germany.de">David Roden</a>
+ * @version $Id$
+ */
+public class AllData extends BaseMessage {
+
+       /** The payload. */
+       private InputStream payloadInputStream;
+
+       /**
+        * Creates an “AllData” message that wraps the received message.
+        * 
+        * @param receivedMessage
+        *            The received message
+        * @param payloadInputStream
+        *            The payload
+        */
+       public AllData(FcpMessage receivedMessage, InputStream payloadInputStream) {
+               super(receivedMessage);
+               this.payloadInputStream = payloadInputStream;
+       }
+
+       /**
+        * Returns the identifier of the request.
+        * 
+        * @return The identifier of the request
+        */
+       public String getIdentifier() {
+               return getField("Identifier");
+       }
+
+       /**
+        * Returns the length of the data.
+        * 
+        * @return The length of the data, or <code>-1</code> if the length could
+        *         not be parsed
+        */
+       public long getDataLength() {
+               try {
+                       return Long.valueOf(getField("DataLength"));
+               } catch (NumberFormatException nfe1) {
+                       return -1;
+               }
+       }
+
+       /**
+        * Returns the startup time of the request.
+        * 
+        * @return The startup time of the request (in milliseconds since Jan 1,
+        *         1970 UTC), or <code>-1</code> if the time could not be parsed
+        */
+       public long getStartupTime() {
+               try {
+                       return Long.valueOf(getField("StartupTime"));
+               } catch (NumberFormatException nfe1) {
+                       return -1;
+               }
+       }
+
+       /**
+        * Returns the completion time of the request.
+        * 
+        * @return The completion time of the request (in milliseconds since Jan 1,
+        *         1970 UTC), or <code>-1</code> if the time could not be parsed
+        */
+       public long getCompletionTime() {
+               try {
+                       return Long.valueOf(getField("CompletionTime"));
+               } catch (NumberFormatException nfe1) {
+                       return -1;
+               }
+       }
+
+       /**
+        * Returns the payload input stream.
+        * 
+        * @return The payload
+        */
+       public InputStream getPayloadInputStream() {
+               return payloadInputStream;
+       }
+
+}
index ca078f7..7111176 100644 (file)
@@ -128,6 +128,14 @@ public class FcpAdapter implements FcpListener {
        }
 
        /**
+        * @see net.pterodactylus.util.fcp.FcpListener#receivedAllData(net.pterodactylus.util.fcp.FcpConnection,
+        *      net.pterodactylus.util.fcp.AllData)
+        */
+       public void receivedAllData(FcpConnection fcpConnection, AllData allData) {
+               /* empty. */
+       }
+
+       /**
         * @see net.pterodactylus.util.fcp.FcpListener#receivedProtocolError(net.pterodactylus.util.fcp.FcpConnection,
         *      net.pterodactylus.util.fcp.ProtocolError)
         */
index dffb110..96f2ea0 100644 (file)
@@ -29,6 +29,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import net.pterodactylus.util.io.Closer;
+import net.pterodactylus.util.io.LimitedInputStream;
 
 /**
  * An FCP connection to a Freenet node.
@@ -323,6 +324,18 @@ public class FcpConnection {
        }
 
        /**
+        * Notifies all listeners that an “AllData” message was received.
+        * 
+        * @param allData
+        *            The “AllData” message
+        */
+       private void fireReceivedAllData(AllData allData) {
+               for (FcpListener fcpListener: fcpListeners) {
+                       fcpListener.receivedAllData(this, allData);
+               }
+       }
+
+       /**
         * Notifies all listeners that a “ProtocolError” message was received.
         * 
         * @param protocolError
@@ -420,6 +433,21 @@ public class FcpConnection {
                        fireReceivedPeer(new Peer(fcpMessage));
                } else if ("PeerNote".equals(messageName)) {
                        fireReceivedPeerNote(new PeerNote(fcpMessage));
+               } else if ("AllData".equals(messageName)) {
+                       long dataLength;
+                       try {
+                               dataLength = Long.valueOf(fcpMessage.getField("DataLength"));
+                       } catch (NumberFormatException nfe1) {
+                               dataLength = -1;
+                       }
+                       LimitedInputStream payloadInputStream = new LimitedInputStream(remoteInputStream, dataLength);
+                       fireReceivedAllData(new AllData(fcpMessage, payloadInputStream));
+                       try {
+                               payloadInputStream.consume();
+                       } catch (IOException ioe1) {
+                               /* FIXME - what now? */
+                               /* well, ignore. when the connection handler fails, all fails. */
+                       }
                } else if ("EndListPeerNotes".equals(messageName)) {
                        fireReceivedEndListPeerNotes(new EndListPeerNotes(fcpMessage));
                } else if ("EndListPeers".equals(messageName)) {
index 96c6d02..61c1292 100644 (file)
@@ -171,6 +171,16 @@ public interface FcpListener extends EventListener {
        public void receivedURIGenerated(FcpConnection fcpConnection, URIGenerated uriGenerated);
 
        /**
+        * Notifies a listener that an “AllData” was received.
+        * 
+        * @param fcpConnection
+        *            The connection that received the message
+        * @param allData
+        *            The “AllData” message
+        */
+       public void receivedAllData(FcpConnection fcpConnection, AllData allData);
+
+       /**
         * Notifies a listener that a “ProtocolError” was received.
         * 
         * @param fcpConnection