Author: nextgens
Date: 2007-04-12 12:53:45 +0000 (Thu, 12 Apr 2007)
New Revision: 12599

Added:
   trunk/freenet/src/freenet/node/fcp/TestDDACompleteMessage.java
   trunk/freenet/src/freenet/node/fcp/TestDDAReplyMessage.java
   trunk/freenet/src/freenet/node/fcp/TestDDARequestMessage.java
   trunk/freenet/src/freenet/node/fcp/TestDDAResponseMessage.java
Log:
Rename testDDAMessages to follow naming conventions

Copied: trunk/freenet/src/freenet/node/fcp/TestDDACompleteMessage.java (from 
rev 12589, trunk/freenet/src/freenet/node/fcp/testDDAComplete.java)
===================================================================
--- trunk/freenet/src/freenet/node/fcp/TestDDACompleteMessage.java              
                (rev 0)
+++ trunk/freenet/src/freenet/node/fcp/TestDDACompleteMessage.java      
2007-04-12 12:53:45 UTC (rev 12599)
@@ -0,0 +1,88 @@
+/* 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 java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+
+import freenet.node.Node;
+import freenet.node.fcp.FCPConnectionHandler.DDACheckJob;
+import freenet.support.Logger;
+import freenet.support.SimpleFieldSet;
+
+/**
+ * client -> node: DDARequest { WantRead=true, WantWrite=true, Dir=/tmp/blah }
+ * node -> client: DDAReply { Dir=/tmp/blah, ReadFilename=random1, 
WriteFilename=random2, ContentToWrite=random3 }
+ * client -> node: DDAResponse { Dir=/tmp/blah, ReadContent=blah }
+ * node -> client: DDAComplete { Dir=/tmp/blah, ReadAllowed=true, 
WriteAllowed=true }
+ * 
+ * @author Florent Daignière <nextgens at freenetproject.org>
+ *
+ */
+public class TestDDACompleteMessage extends FCPMessage {
+       public static String NAME = "TestDDAComplete";
+       public static String READ_ALLOWED = "ReadAllowed";
+       public static String WRITE_ALLOWED = "WriteAllowed";
+
+       final DDACheckJob checkJob;
+       final String readContentFromClient;
+       private final FCPConnectionHandler handler;
+       
+       public TestDDACompleteMessage(FCPConnectionHandler handler, DDACheckJob 
job, String readContent) {
+               this.checkJob = job;
+               this.readContentFromClient = readContent;
+               this.handler = handler;
+       }
+
+       public SimpleFieldSet getFieldSet() {
+               SimpleFieldSet sfs = new SimpleFieldSet(true);
+               
+               sfs.putSingle(TestDDARequestMessage.DIRECTORY, 
checkJob.directory.toString());
+               
+               boolean isReadAllowed = false; 
+               boolean isWriteAllowed = false;
+               
+               if(checkJob.readFilename != null) {
+                       isReadAllowed = (readContentFromClient != null) &&  
(checkJob.readContent.equals(readContentFromClient));
+                       // cleanup in any case : we created it!... let's hope 
the client will do the same on its side.
+                       checkJob.readFilename.delete();
+                       sfs.putSingle(READ_ALLOWED, 
String.valueOf(isReadAllowed));
+               }
+               
+               if(checkJob.writeFilename != null) {
+                       File maybeWrittenFile = checkJob.writeFilename;
+                       if (maybeWrittenFile.exists() && 
maybeWrittenFile.isFile() && maybeWrittenFile.canRead()) {
+                               try {
+                                       FileReader fr = new 
FileReader(maybeWrittenFile);
+                                       StringBuffer sb = new StringBuffer();
+                                       
+                                       int current = fr.read();                
                        
+                                       while(current != -1) {
+                                               sb.append((char)current);
+                                               current = fr.read();
+                                       }
+                                       
+                                       fr.close();
+                                       isWriteAllowed = 
checkJob.writeContent.equals(sb.toString().trim());
+                               } catch (IOException e) {
+                                       Logger.error(this, "Caught an IOE 
trying to read the file (" + maybeWrittenFile + ")! " + e.getMessage());
+                               }
+                       }
+                       sfs.putSingle(WRITE_ALLOWED, 
String.valueOf(isWriteAllowed));
+               }
+               
+               handler.registerTestDDAResult(checkJob.directory.toString(), 
isReadAllowed, isWriteAllowed);
+               
+               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", NAME, false);
+       }
+}

Copied: trunk/freenet/src/freenet/node/fcp/TestDDAReplyMessage.java (from rev 
12589, trunk/freenet/src/freenet/node/fcp/TestDDAReply.java)
===================================================================
--- trunk/freenet/src/freenet/node/fcp/TestDDAReplyMessage.java                 
        (rev 0)
+++ trunk/freenet/src/freenet/node/fcp/TestDDAReplyMessage.java 2007-04-12 
12:53:45 UTC (rev 12599)
@@ -0,0 +1,54 @@
+/* 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.node.fcp.FCPConnectionHandler.DDACheckJob;
+import freenet.support.SimpleFieldSet;
+
+/**
+ * client -> node: DDARequest { WantRead=true, WantWrite=true, Dir=/tmp/blah }
+ * node -> client: DDAReply { Dir=/tmp/blah, ReadFilename=random1, 
WriteFilename=random2, ContentToWrite=random3 }
+ * client -> node: DDAResponse { Dir=/tmp/blah, ReadContent=blah }
+ * node -> client: DDAComplete { Dir=/tmp/blah, ReadAllowed=true, 
WriteAllowed=true }
+ * 
+ * @author Florent Daignière <nextgens at freenetproject.org>
+ *
+ */
+public class TestDDAReplyMessage extends FCPMessage {
+       public static final String NAME = "TestDDAReply";
+       public static final String READ_FILENAME = "ReadFilename";
+       public static final String WRITE_FILENAME = "WriteFilename";
+       public static final String CONTENT_TO_WRITE = "ContentToWrite";
+       
+       final DDACheckJob checkJob;
+       
+       TestDDAReplyMessage(DDACheckJob job) {
+               this.checkJob = job;
+       }
+       
+       public SimpleFieldSet getFieldSet() {
+               SimpleFieldSet sfs = new SimpleFieldSet(true);
+               sfs.putSingle(TestDDARequestMessage.DIRECTORY, 
checkJob.directory.toString());
+               
+               if(checkJob.readFilename != null) {
+                       sfs.putSingle(READ_FILENAME, 
checkJob.readFilename.toString());
+               }
+               
+               if(checkJob.writeFilename != null) {
+                       sfs.putSingle(WRITE_FILENAME, 
checkJob.writeFilename.toString());
+                       sfs.putSingle(CONTENT_TO_WRITE, checkJob.writeContent);
+               }
+               
+               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", NAME, false);
+       }
+}

Copied: trunk/freenet/src/freenet/node/fcp/TestDDARequestMessage.java (from rev 
12589, trunk/freenet/src/freenet/node/fcp/TestDDARequest.java)
===================================================================
--- trunk/freenet/src/freenet/node/fcp/TestDDARequestMessage.java               
                (rev 0)
+++ trunk/freenet/src/freenet/node/fcp/TestDDARequestMessage.java       
2007-04-12 12:53:45 UTC (rev 12599)
@@ -0,0 +1,62 @@
+/* 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.node.fcp.FCPConnectionHandler.DDACheckJob;
+import freenet.support.SimpleFieldSet;
+
+/**
+ * client -> node: DDARequest { WantRead=true, WantWrite=true, Dir=/tmp/blah }
+ * node -> client: DDAReply { Dir=/tmp/blah, ReadFilename=random1, 
WriteFilename=random2, ContentToWrite=random3 }
+ * client -> node: DDAResponse { Dir=/tmp/blah, ReadContent=blah }
+ * node -> client: DDAComplete { Dir=/tmp/blah, ReadAllowed=true, 
WriteAllowed=true }
+ * 
+ *  @author Florent Daignière <nextgens at freenetproject.org>
+ */
+public class TestDDARequestMessage extends FCPMessage {
+       public static final String NAME = "TestDDARequest";
+       public static final String DIRECTORY = "Directory";
+       public static final String WANT_READ = "WantRead";
+       public static final String WANT_WRITE = "WantWrite";
+       
+       final String identifier;
+       final boolean wantRead, wantWrite;
+       
+       
+       /** 
+        * @throws MessageInvalidException 
+        */
+       public TestDDARequestMessage(SimpleFieldSet fs) throws 
MessageInvalidException {
+               identifier = fs.get(DIRECTORY);
+               if(identifier == null)
+                       throw new 
MessageInvalidException(ProtocolErrorMessage.MISSING_FIELD, "No Directory 
given!", null, false);
+               if(identifier.length() == 0)
+                       throw new 
MessageInvalidException(ProtocolErrorMessage.MISSING_FIELD, "The specified 
Directory can't be empty!", null, false);
+               
+               wantRead = fs.getBoolean(WANT_READ, false);
+               wantWrite = fs.getBoolean(WANT_WRITE, false);
+               if((wantRead == false) && (wantWrite == false))
+                       throw new 
MessageInvalidException(ProtocolErrorMessage.INVALID_MESSAGE, "Both "+ 
WANT_READ + " and " + WANT_WRITE + " are set to false: what's the point of 
sending a message?", identifier, false);
+       }
+
+       public SimpleFieldSet getFieldSet() {
+               return null;
+       }
+
+       public String getName() {
+               return NAME;
+       }
+
+       public void run(FCPConnectionHandler handler, Node node) throws 
MessageInvalidException {
+               DDACheckJob job;
+               try {
+                       job = handler.enqueueDDACheck(identifier, wantRead, 
wantWrite);
+               } catch (IllegalArgumentException e) {
+                       throw new 
MessageInvalidException(ProtocolErrorMessage.INVALID_FIELD, e.getMessage(), 
identifier, false);
+               }
+               TestDDAReplyMessage reply = new TestDDAReplyMessage(job);
+               handler.outputHandler.queue(reply);
+       }
+}

Copied: trunk/freenet/src/freenet/node/fcp/TestDDAResponseMessage.java (from 
rev 12589, trunk/freenet/src/freenet/node/fcp/TestDDAResponse.java)
===================================================================
--- trunk/freenet/src/freenet/node/fcp/TestDDAResponseMessage.java              
                (rev 0)
+++ trunk/freenet/src/freenet/node/fcp/TestDDAResponseMessage.java      
2007-04-12 12:53:45 UTC (rev 12599)
@@ -0,0 +1,59 @@
+/* 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.node.fcp.FCPConnectionHandler.DDACheckJob;
+import freenet.support.SimpleFieldSet;
+
+/**
+ * client -> node: DDARequest { WantRead=true, WantWrite=true, Dir=/tmp/blah }
+ * node -> client: DDAReply { Dir=/tmp/blah, ReadFilename=random1, 
WriteFilename=random2, ContentToWrite=random3 }
+ * client -> node: DDAResponse { Dir=/tmp/blah, ReadContent=blah }
+ * node -> client: DDAComplete { Dir=/tmp/blah, ReadAllowed=true, 
WriteAllowed=true }
+ * 
+ * @author Florent Daignière <nextgens at freenetproject.org>
+ *
+ */
+public class TestDDAResponseMessage extends FCPMessage {
+       public static final String NAME = "TestDDAResponse";
+       public static final String READ_CONTENT = "ReadContent";
+       
+       final String identifier;
+       final String readContent;
+       
+       public TestDDAResponseMessage(SimpleFieldSet sfs) throws 
MessageInvalidException {
+               identifier = sfs.get(TestDDARequestMessage.DIRECTORY);
+               if(identifier == null)
+                       throw new 
MessageInvalidException(ProtocolErrorMessage.MISSING_FIELD, "No Directory 
given!", null, false);
+               if(identifier.length() == 0)
+                       throw new 
MessageInvalidException(ProtocolErrorMessage.MISSING_FIELD, "The specified 
Directory can't be empty!", null, false);
+               
+               readContent = sfs.get(READ_CONTENT);
+       }
+
+       public SimpleFieldSet getFieldSet() {
+               return null;
+       }
+
+       public String getName() {
+               return NAME;
+       }
+
+       public void run(FCPConnectionHandler handler, Node node) throws 
MessageInvalidException {
+               DDACheckJob job;
+               try {
+                        job = handler.popDDACheck(identifier);
+               } catch (IllegalArgumentException e) {
+                       throw new 
MessageInvalidException(ProtocolErrorMessage.INVALID_FIELD, e.getMessage(), 
identifier, false);
+               }
+               if(job == null)
+                       throw new 
MessageInvalidException(ProtocolErrorMessage.INVALID_MESSAGE, "The node doesn't 
know that testDDA identifier! double check it! (" + identifier + ").", 
identifier, false);
+               else if((job.readFilename != null) && (readContent == null))
+                       throw new 
MessageInvalidException(ProtocolErrorMessage.MISSING_FIELD, "You need to send " 
+ READ_CONTENT + " back to the node if you specify " + 
TestDDARequestMessage.WANT_READ + " in " + TestDDARequestMessage.NAME + '.', 
identifier, false);
+               
+               TestDDACompleteMessage reply = new 
TestDDACompleteMessage(handler, job, readContent);
+               handler.outputHandler.queue(reply);
+       }
+}


Reply via email to