From: David ‘Bombe’ Roden Date: Sat, 12 Apr 2008 18:25:42 +0000 (+0000) Subject: add FCPPluginReply X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=b905d1b51018f68ba5e8c598781a494921318c91;p=jSite2.git add FCPPluginReply git-svn-id: http://trooper/svn/projects/jSite/trunk@734 c3eda9e8-030b-0410-8277-bc7414b0a119 --- diff --git a/TODO b/TODO index c630871..9d8ba1f 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,5 @@ ClientPutComplexDir ClientPutDiskDir -FCPPluginReply (since 1075) GetPluginInfo (since 1075) GetRequestStatus ModifyConfig (since 1027) diff --git a/src/net/pterodactylus/util/fcp/FCPPluginReply.java b/src/net/pterodactylus/util/fcp/FCPPluginReply.java new file mode 100644 index 0000000..8ca7e23 --- /dev/null +++ b/src/net/pterodactylus/util/fcp/FCPPluginReply.java @@ -0,0 +1,98 @@ +/* + * jSite2 - FCPPluginReply.java - + * Copyright © 2008 David Roden + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.pterodactylus.util.fcp; + +import java.io.InputStream; + +/** + * The “FCPPluginReply” is sent by a plugin as a response to a + * {@link FCPPluginMessage} message. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + * @version $Id$ + */ +public class FCPPluginReply extends BaseMessage { + + /** The payload input stream. */ + private final InputStream payloadInputStream; + + /** + * Creates a new “FCPPluginReply” message that wraps the received message. + * + * @param receivedMessage + * The received message + * @param payloadInputStream + * The optional input stream for the payload + */ + FCPPluginReply(FcpMessage receivedMessage, InputStream payloadInputStream) { + super(receivedMessage); + this.payloadInputStream = payloadInputStream; + } + + /** + * Returns the name of the plugin. + * + * @return The name of the plugin + */ + public String getPluginName() { + return getField("PluginName"); + } + + /** + * Returns the identifier of the request. + * + * @return The identifier of the request + */ + public String getIdentifier() { + return getField("Identifier"); + } + + /** + * Returns the length of the optional payload. + * + * @return The length of the payload, or -1 if there is no + * payload or the length could not be parsed + */ + public long getDataLength() { + return FcpUtils.safeParseLong(getField("DataLength")); + } + + /** + * Returns a reply from the plugin. + * + * @param key + * The name of the reply + * @return The value of the reply + */ + public String getReply(String key) { + return getField("Replies." + key); + } + + /** + * Returns the optional payload. + * + * @return The payload of the reply, or null if there is no + * 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 491adf3..caa16ee 100644 --- a/src/net/pterodactylus/util/fcp/FcpAdapter.java +++ b/src/net/pterodactylus/util/fcp/FcpAdapter.java @@ -228,6 +228,13 @@ public class FcpAdapter implements FcpListener { /** * {@inheritDoc} */ + public void receivedFCPPluginReply(FcpConnection fcpConnection, FCPPluginReply fcpPluginReply) { + /* empty. */ + } + + /** + * {@inheritDoc} + */ public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) { /* empty. */ } diff --git a/src/net/pterodactylus/util/fcp/FcpConnection.java b/src/net/pterodactylus/util/fcp/FcpConnection.java index 12d52af..9a13264 100644 --- a/src/net/pterodactylus/util/fcp/FcpConnection.java +++ b/src/net/pterodactylus/util/fcp/FcpConnection.java @@ -537,6 +537,18 @@ public class FcpConnection { } /** + * Notifies all listeners that an “FCPPluginReply” message was received. + * + * @param fcpPluginReply + * The “FCPPluginReply” message + */ + private void fireReceivedFCPPluginReply(FCPPluginReply fcpPluginReply) { + for (FcpListener fcpListener: fcpListeners) { + fcpListener.receivedFCPPluginReply(this, fcpPluginReply); + } + } + + /** * Notifies all listeners that a “ProtocolError” message was received. * * @param protocolError @@ -677,6 +689,14 @@ public class FcpConnection { fireReceivedUnknownPeerNoteType(new UnknownPeerNoteType(fcpMessage)); } else if ("UnknownNodeIdentifier".equals(messageName)) { fireReceivedUnknownNodeIdentifier(new UnknownNodeIdentifier(fcpMessage)); + } else if ("FCPPluginReply".equals(messageName)) { + LimitedInputStream payloadInputStream = getInputStream(FcpUtils.safeParseLong(fcpMessage.getField("DataLength"))); + fireReceivedFCPPluginReply(new FCPPluginReply(fcpMessage, payloadInputStream)); + try { + payloadInputStream.consume(); + } catch (IOException ioe1) { + /* ignore. */ + } } else if ("PluginInfo".equals(messageName)) { fireReceivedPluginInfo(new PluginInfo(fcpMessage)); } else if ("NodeData".equals(messageName)) { diff --git a/src/net/pterodactylus/util/fcp/FcpListener.java b/src/net/pterodactylus/util/fcp/FcpListener.java index 43967a2..a3a4381 100644 --- a/src/net/pterodactylus/util/fcp/FcpListener.java +++ b/src/net/pterodactylus/util/fcp/FcpListener.java @@ -332,6 +332,16 @@ public interface FcpListener extends EventListener { public void receivedPluginInfo(FcpConnection fcpConnection, PluginInfo pluginInfo); /** + * Notifies a listener that an “FCPPluginReply“ message was received. + * + * @param fcpConnection + * The connection that received the message + * @param fcpPluginReply + * The “FCPPluginReply” message + */ + public void receivedFCPPluginReply(FcpConnection fcpConnection, FCPPluginReply fcpPluginReply); + + /** * Notifies a listener that a “ProtocolError” was received. * * @param fcpConnection