From b56d9c55ebfc3651b8b3a0b2e43240a9054d680c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 10 Apr 2008 14:48:24 +0000 Subject: [PATCH] add AllData git-svn-id: http://trooper/svn/projects/jSite/trunk@694 c3eda9e8-030b-0410-8277-bc7414b0a119 --- src/net/pterodactylus/util/fcp/AllData.java | 98 +++++++++++++++++++++++ src/net/pterodactylus/util/fcp/FcpAdapter.java | 8 ++ src/net/pterodactylus/util/fcp/FcpConnection.java | 28 +++++++ src/net/pterodactylus/util/fcp/FcpListener.java | 10 +++ 4 files changed, 144 insertions(+) create mode 100644 src/net/pterodactylus/util/fcp/AllData.java diff --git a/src/net/pterodactylus/util/fcp/AllData.java b/src/net/pterodactylus/util/fcp/AllData.java new file mode 100644 index 0000000..c2ead16 --- /dev/null +++ b/src/net/pterodactylus/util/fcp/AllData.java @@ -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 David Roden + * @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 -1 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 -1 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 -1 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; + } + +} diff --git a/src/net/pterodactylus/util/fcp/FcpAdapter.java b/src/net/pterodactylus/util/fcp/FcpAdapter.java index ca078f7..7111176 100644 --- a/src/net/pterodactylus/util/fcp/FcpAdapter.java +++ b/src/net/pterodactylus/util/fcp/FcpAdapter.java @@ -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) */ diff --git a/src/net/pterodactylus/util/fcp/FcpConnection.java b/src/net/pterodactylus/util/fcp/FcpConnection.java index dffb110..96f2ea0 100644 --- a/src/net/pterodactylus/util/fcp/FcpConnection.java +++ b/src/net/pterodactylus/util/fcp/FcpConnection.java @@ -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)) { diff --git a/src/net/pterodactylus/util/fcp/FcpListener.java b/src/net/pterodactylus/util/fcp/FcpListener.java index 96c6d02..61c1292 100644 --- a/src/net/pterodactylus/util/fcp/FcpListener.java +++ b/src/net/pterodactylus/util/fcp/FcpListener.java @@ -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 -- 2.7.4