Author: saces
Date: 2007-11-19 21:46:50 +0000 (Mon, 19 Nov 2007)
New Revision: 15857
Added:
trunk/freenet/src/freenet/node/fcp/FCPPluginMessage.java
trunk/freenet/src/freenet/node/fcp/FCPPluginReply.java
trunk/freenet/src/freenet/pluginmanager/FCPPluginOutputWrapper.java
Log:
new file for the rewritten fcp plugin interface
Added: trunk/freenet/src/freenet/node/fcp/FCPPluginMessage.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/FCPPluginMessage.java
(rev 0)
+++ trunk/freenet/src/freenet/node/fcp/FCPPluginMessage.java 2007-11-19
21:46:50 UTC (rev 15857)
@@ -0,0 +1,120 @@
+/* This code is part of Freenet. It is distributed under the GNU General
+ * Public License, version 2 (or at your option any later version). See
+ * http://www.gnu.org/ for further details of the GPL. */
+package freenet.node.fcp;
+
+import freenet.node.Node;
+import freenet.pluginmanager.FCPPluginOutputWrapper;
+import freenet.pluginmanager.FredPluginFCP;
+import freenet.support.Logger;
+import freenet.support.SimpleFieldSet;
+import freenet.support.api.Bucket;
+
+/**
+ * @author saces
+ *
+ * FCPPluginMessage
+ * Identifer=me
+ * PluginName=plugins.HelloFCP (or plugins.HelloFCP.HelloFCP?)
+ * Param.Itemname1=value1
+ * Param.Itemname2=value2
+ * ...
+ *
+ * EndMessage
+ * or
+ * DataLength=datasize
+ * Data
+ * <datasize> bytes of data
+ *
+ */
+public class FCPPluginMessage extends DataCarryingMessage {
+
+ public static final String NAME = "FCPPluginMessage";
+
+ public static final String PARAM_PREFIX = "Param";
+
+ private final String identifier;
+ private final String pluginname;
+
+ private final long dataLength;
+
+ private final SimpleFieldSet plugparams;
+
+ FCPPluginMessage(SimpleFieldSet fs) throws MessageInvalidException {
+ identifier = fs.get("Identifier");
+ if(identifier == null)
+ throw new
MessageInvalidException(ProtocolErrorMessage.MISSING_FIELD, "FCPPluginMessage
must contain a Identifier field", null, false);
+ pluginname = fs.get("PluginName");
+ if(pluginname == null)
+ throw new
MessageInvalidException(ProtocolErrorMessage.MISSING_FIELD, "FCPPluginMessage
must contain a PluginName field", null, false);
+
+ boolean havedata = "Data".equals(fs.getEndMarker());
+
+ String dataLengthString = fs.get("DataLength");
+
+ if(!havedata && (dataLengthString != null))
+ throw new
MessageInvalidException(ProtocolErrorMessage.INVALID_FIELD, "A nondata message
can't have a DataLength field", identifier, false);
+
+ if(havedata) {
+ if (dataLengthString == null)
+ throw new
MessageInvalidException(ProtocolErrorMessage.MISSING_FIELD, "Need DataLength on
a Datamessage", identifier, false);
+
+ try {
+ dataLength = Long.parseLong(dataLengthString,
10);
+ } catch (NumberFormatException e) {
+ throw new
MessageInvalidException(ProtocolErrorMessage.ERROR_PARSING_NUMBER, "Error
parsing DataLength field: "+e.getMessage(), identifier, false);
+ }
+ } else {
+ dataLength = -1;
+ }
+
+ plugparams = fs.subset(PARAM_PREFIX);
+ }
+
+ String getIdentifier() {
+ return identifier;
+ }
+
+ boolean isGlobal() {
+ return false;
+ }
+
+ long dataLength() {
+ return dataLength;
+ }
+
+ public SimpleFieldSet getFieldSet() {
+ return null;
+ }
+
+ public String getName() {
+ return NAME;
+ }
+
+ public void run(final FCPConnectionHandler handler, final Node node)
throws MessageInvalidException {
+
+ final Bucket data2 = this.bucket;
+ final FCPPluginOutputWrapper replysender = new
FCPPluginOutputWrapper(handler, pluginname, identifier);
+ node.executor.execute(new Runnable() {
+
+ public void run() {
+ Logger.normal(this, "Searching fcp plugin: " +
pluginname);
+ FredPluginFCP plug =
node.pluginManager.getFCPPlugin(pluginname);
+ if (plug == null) {
+ Logger.error(this, "Could not find fcp
plugin: " + pluginname);
+ return;
+ }
+ Logger.normal(this, "Found fcp plugin: " +
pluginname);
+
+ try {
+ plug.handle(replysender, plugparams,
data2, handler.hasFullAccess());
+ } catch (Throwable t) {
+ Logger.error(this, "Cought error while
execute fcp plugin handler" + t.getMessage(), t);
+ }
+
+ }
+ }, "FCPPlugin runner for " + pluginname);
+
+ }
+
+}
Added: trunk/freenet/src/freenet/node/fcp/FCPPluginReply.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/FCPPluginReply.java
(rev 0)
+++ trunk/freenet/src/freenet/node/fcp/FCPPluginReply.java 2007-11-19
21:46:50 UTC (rev 15857)
@@ -0,0 +1,64 @@
+/* This code is part of Freenet. It is distributed under the GNU General
+ * Public License, version 2 (or at your option any later version). See
+ * http://www.gnu.org/ for further details of the GPL. */
+package freenet.node.fcp;
+
+import freenet.node.Node;
+import freenet.support.SimpleFieldSet;
+
+/**
+ * @author saces
+ *
+ */
+public class FCPPluginReply extends DataCarryingMessage {
+
+ private static final String NAME = "FCPPluginReply";
+
+ public static final String PARAM_PREFIX = "Param";
+
+ private final String plugname;
+ private final String identifier;
+ private final SimpleFieldSet plugparams;
+
+ public FCPPluginReply(String pluginname, String identifier2,
SimpleFieldSet fs) {
+ plugname = pluginname;
+ identifier = identifier2;
+ plugparams = fs;
+ }
+
+ String getIdentifier() {
+ return identifier;
+ }
+
+ boolean isGlobal() {
+ return false;
+ }
+
+ long dataLength() {
+ return -1;
+ }
+
+ String getEndString() {
+ if (dataLength() > 0)
+ return "Data";
+ else
+ return "EndMessage";
+ }
+
+ public SimpleFieldSet getFieldSet() {
+ SimpleFieldSet sfs = new SimpleFieldSet(true);
+ sfs.putSingle("PluginName", plugname);
+ sfs.putSingle("Identifier", identifier);
+ sfs.put("Replies", plugparams);
+ return sfs;
+ }
+
+ public String getName() {
+ return NAME;
+ }
+
+ public void run(FCPConnectionHandler handler, Node node) throws
MessageInvalidException {
+ throw new
MessageInvalidException(ProtocolErrorMessage.INVALID_MESSAGE, NAME + " goes
from server to client not the other way around", null, false);
+ }
+
+}
Added: trunk/freenet/src/freenet/pluginmanager/FCPPluginOutputWrapper.java
===================================================================
--- trunk/freenet/src/freenet/pluginmanager/FCPPluginOutputWrapper.java
(rev 0)
+++ trunk/freenet/src/freenet/pluginmanager/FCPPluginOutputWrapper.java
2007-11-19 21:46:50 UTC (rev 15857)
@@ -0,0 +1,43 @@
+/* This code is part of Freenet. It is distributed under the GNU General
+ * Public License, version 2 (or at your option any later version). See
+ * http://www.gnu.org/ for further details of the GPL. */
+package freenet.pluginmanager;
+
+import freenet.node.fcp.FCPConnectionHandler;
+import freenet.node.fcp.FCPPluginReply;
+import freenet.support.SimpleFieldSet;
+import freenet.support.api.Bucket;
+
+/**
+ * @author saces
+ *
+ */
+public class FCPPluginOutputWrapper {
+
+ private final String identifier;
+ private final String plugname;
+ private final FCPConnectionHandler handler;
+
+
+ public FCPPluginOutputWrapper(FCPConnectionHandler handler2, String
pluginname, String identifier2) {
+ identifier = identifier2;
+ plugname = pluginname;
+ handler = handler2;
+ }
+
+ public void send(SimpleFieldSet params) {
+ FCPPluginReply reply = new FCPPluginReply(plugname, identifier,
params);
+ handler.outputHandler.queue(reply);
+ }
+
+ public void send(SimpleFieldSet params, byte[] data) {
+ FCPPluginReply reply = new FCPPluginReply(plugname, identifier,
params);
+ handler.outputHandler.queue(reply);
+ }
+
+ public void send(SimpleFieldSet params, Bucket data) {
+ FCPPluginReply reply = new FCPPluginReply(plugname, identifier,
params);
+ handler.outputHandler.queue(reply);
+ }
+
+}