Author: nextgens
Date: 2006-08-15 17:15:54 +0000 (Tue, 15 Aug 2006)
New Revision: 10090

Added:
   trunk/freenet/src/freenet/tools/
   trunk/freenet/src/freenet/tools/AddRef.java
Removed:
   trunk/freenet/src/freenet/support/AddRef.java
Modified:
   trunk/freenet/src/freenet/node/fcp/AddPeer.java
   trunk/freenet/src/freenet/node/fcp/FCPMessage.java
   trunk/freenet/src/freenet/node/fcp/NodeHelloMessage.java
Log:
Move freenet.support.AddRef to freenet.tools.AddRef ... I wonder whether we 
should sanitize the input of the file or let the node do it.

Modified: trunk/freenet/src/freenet/node/fcp/AddPeer.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/AddPeer.java     2006-08-15 17:13:25 UTC 
(rev 10089)
+++ trunk/freenet/src/freenet/node/fcp/AddPeer.java     2006-08-15 17:15:54 UTC 
(rev 10090)
@@ -18,7 +18,7 @@

 public class AddPeer extends FCPMessage {

-       static final String name = "AddPeer";
+       public static final String name = "AddPeer";

        SimpleFieldSet fs;


Modified: trunk/freenet/src/freenet/node/fcp/FCPMessage.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/FCPMessage.java  2006-08-15 17:13:25 UTC 
(rev 10089)
+++ trunk/freenet/src/freenet/node/fcp/FCPMessage.java  2006-08-15 17:15:54 UTC 
(rev 10090)
@@ -66,11 +66,21 @@
                        return new WatchGlobal(fs);
                if(name.equals("Void"))
                        return null;
+               if(name.equals(NodeHelloMessage.name))
+                       return new NodeHelloMessage(fs);
                throw new 
MessageInvalidException(ProtocolErrorMessage.INVALID_MESSAGE, "Unknown message 
name "+name, null);
 //             if(name.equals("ClientPut"))
 //                     return new ClientPutFCPMessage(fs);
                // TODO Auto-generated method stub
        }
+       
+       /**
+        * Create a message from a SimpleFieldSet, and the message's name, if 
possible. 
+        * Usefull for FCPClients
+        */
+       public static FCPMessage create(String name, SimpleFieldSet fs) throws 
MessageInvalidException {
+               return FCPMessage.create(name, fs, null, null);
+       }

        /** Do whatever it is that we do with this type of message. 
         * @throws MessageInvalidException */

Modified: trunk/freenet/src/freenet/node/fcp/NodeHelloMessage.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/NodeHelloMessage.java    2006-08-15 
17:13:25 UTC (rev 10089)
+++ trunk/freenet/src/freenet/node/fcp/NodeHelloMessage.java    2006-08-15 
17:15:54 UTC (rev 10090)
@@ -2,6 +2,7 @@

 import freenet.node.Node;
 import freenet.node.Version;
+import freenet.support.Fields;
 import freenet.support.SimpleFieldSet;
 import freenet.support.compress.Compressor;

@@ -15,10 +16,40 @@
  * EndMessage
  */
 public class NodeHelloMessage extends FCPMessage {
-
-       private final Node node;
+       public static final String name = "NodeHello";
+       String nodeVersion;
+       String nodeFCPVersion;
+       String nodeNode;
+       String nodeCompressionCodecs;
+       boolean isTestnet;

-       public NodeHelloMessage(Node node) {
+       private Node node;
+       
+       public NodeHelloMessage(SimpleFieldSet fs) throws 
MessageInvalidException {     
+               this.nodeNode = fs.get("Node");
+               if(nodeNode == null)
+                       throw new 
MessageInvalidException(ProtocolErrorMessage.MISSING_FIELD, "No Node!", null);
+               else if(!nodeNode.equals("Fred"))
+                       throw new 
MessageInvalidException(ProtocolErrorMessage.INVALID_FIELD, "Not talking to 
Fred!", null);
+               
+               this.nodeFCPVersion = fs.get("FCPVersion");
+               if(nodeFCPVersion == null)
+                       throw new 
MessageInvalidException(ProtocolErrorMessage.MISSING_FIELD, "No FCPVersion!", 
null);
+               else if(!nodeFCPVersion.equals("2.0"))
+                       throw new 
MessageInvalidException(ProtocolErrorMessage.NOT_SUPPORTED, "FCPVersion is 
incompatible!", null);
+               
+               this.nodeVersion = fs.get("Version");
+               if(nodeVersion == null)
+                       throw new 
MessageInvalidException(ProtocolErrorMessage.MISSING_FIELD, "No Version!", 
null);
+               
+               this.nodeCompressionCodecs = fs.get("CompressionCodecs");
+               if(nodeCompressionCodecs == null)
+                       throw new 
MessageInvalidException(ProtocolErrorMessage.MISSING_FIELD, "No 
CompressionCodecs!", null);   
+               
+               this.isTestnet = Fields.stringToBool(fs.get("Testnet"), false);
+       }
+       
+       public NodeHelloMessage(final Node node) {
                this.node = node;
        }

@@ -34,7 +65,7 @@
        }

        public String getName() {
-               return "NodeHello";
+               return NodeHelloMessage.name;
        }

        public void run(FCPConnectionHandler handler, Node node) {

Deleted: trunk/freenet/src/freenet/support/AddRef.java
===================================================================
--- trunk/freenet/src/freenet/support/AddRef.java       2006-08-15 17:13:25 UTC 
(rev 10089)
+++ trunk/freenet/src/freenet/support/AddRef.java       2006-08-15 17:15:54 UTC 
(rev 10090)
@@ -1,80 +0,0 @@
-package freenet.support;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.net.Socket;
-import java.net.SocketException;
-
-import freenet.node.fcp.FCPMessage;
-import freenet.node.fcp.FCPServer;
-import freenet.node.fcp.MessageInvalidException;
-import freenet.node.fcp.NodeHelloMessage;
-import freenet.support.io.LineReadingInputStream;
-
-public class AddRef {
-
-       /**
-        * Connects to a FCP server and adds a reference
-        * @param args
-        */
-       public static void main(String[] args) {
-               Socket fcpSocket = null;
-               File reference = null;
-               FCPMessage fcpm;
-               SimpleFieldSet sfs = new SimpleFieldSet();
-               
-               if(args.length < 1){
-                       System.err.println("Please provide a file name as the 
first argument.");
-                       System.exit(-1);
-               }
-               
-               reference = new File(args[0]);
-               if((reference == null) || !(reference.isFile()) || 
!(reference.canRead())){
-                       System.err.println("Please provide a file name as the 
first argument.");
-                       System.exit(-1);        
-               }
-                       
-               
-               try{
-                       
-                       
-                       fcpSocket = new Socket("127.0.0.1", 
FCPServer.DEFAULT_FCP_PORT);
-                       fcpSocket.setSoTimeout(2000);
-                       
-                       InputStream is = fcpSocket.getInputStream();
-                       LineReadingInputStream lis = new 
LineReadingInputStream(is);
-                       OutputStream os = fcpSocket.getOutputStream();
-                       
-                       try{
-                               sfs.put("Name", "AddRef");
-                               sfs.put("ExpectedVersion", "2.0");
-                               fcpm = FCPMessage.create("ClientHello", sfs, 
null, null);
-                               fcpm.send(os);
-                               os.flush();
-                               String messageType = lis.readLine(128, 128, 
true);
-                               fcpm = FCPMessage.create("NodeHello", sfs, 
null, null);
-                               if((fcpm == null) || !(fcpm instanceof 
NodeHelloMessage)){
-                                       System.err.println("Not a valid node!");
-                                       System.exit(1);
-                               }else{
-                                       System.out.println(fcpm.getFieldSet());
-                               }
-                       } catch(MessageInvalidException me){
-                               me.printStackTrace();
-                       }
-                       
-                       fcpSocket.close();
-                       System.out.println("That reference has been added");
-               }catch (SocketException se){
-                       System.err.println(se);
-                       se.printStackTrace();
-                       System.exit(1);
-               }catch (IOException ioe){
-                       System.err.println(ioe);
-                       ioe.printStackTrace();
-                       System.exit(2);
-               }               
-       }
-}

Copied: trunk/freenet/src/freenet/tools/AddRef.java (from rev 10087, 
trunk/freenet/src/freenet/support/AddRef.java)
===================================================================
--- trunk/freenet/src/freenet/support/AddRef.java       2006-08-15 17:02:21 UTC 
(rev 10087)
+++ trunk/freenet/src/freenet/tools/AddRef.java 2006-08-15 17:15:54 UTC (rev 
10090)
@@ -0,0 +1,115 @@
+package freenet.tools;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.net.SocketException;
+
+import freenet.node.fcp.AddPeer;
+import freenet.node.fcp.FCPMessage;
+import freenet.node.fcp.FCPServer;
+import freenet.node.fcp.MessageInvalidException;
+import freenet.node.fcp.NodeHelloMessage;
+import freenet.support.SimpleFieldSet;
+import freenet.support.io.LineReadingInputStream;
+
+public class AddRef {
+
+       /**
+        * Connects to a FCP server and adds a reference
+        * @param args
+        */
+       public static void main(String[] args) {
+               if(args.length < 1){
+                       System.err.println("Please provide a file name as the 
first argument.");
+                       System.exit(-1);
+               }
+
+               final File reference = new File(args[0]);
+               if((reference == null) || !(reference.isFile()) || 
!(reference.canRead())){
+                       System.err.println("Please provide a file name as the 
first argument.");
+                       System.exit(-1);        
+               }
+
+               AddRef addref = new AddRef(reference);
+       }
+
+       AddRef(File reference){
+               Socket fcpSocket = null;
+
+               FCPMessage fcpm;
+               SimpleFieldSet sfs = new SimpleFieldSet();
+
+               try{
+                       fcpSocket = new Socket("127.0.0.1", 
FCPServer.DEFAULT_FCP_PORT);
+                       fcpSocket.setSoTimeout(2000);
+
+                       InputStream is = fcpSocket.getInputStream();
+                       LineReadingInputStream lis = new 
LineReadingInputStream(is);
+                       OutputStream os = fcpSocket.getOutputStream();
+
+                       try{
+                               sfs.put("Name", "AddRef");
+                               sfs.put("ExpectedVersion", "2.0");
+                               fcpm = FCPMessage.create("ClientHello", sfs);
+                               fcpm.send(os);
+                               os.flush();
+
+                               String messageName = lis.readLine(128, 128, 
true);
+                               sfs = getMessage(lis);
+                               fcpm = FCPMessage.create(messageName, sfs);
+                               if((fcpm == null) || !(fcpm instanceof 
NodeHelloMessage)){
+                                       System.err.println("Not a valid node!");
+                                       System.exit(1);
+                               }else{
+                                       fcpm = (NodeHelloMessage) fcpm;
+                                       System.out.println(fcpm.getFieldSet());
+                               }
+                       } catch(MessageInvalidException me){
+                               me.printStackTrace();
+                       }
+                       
+                       try{
+                               sfs = SimpleFieldSet.readFrom(reference);
+                               fcpm = FCPMessage.create(AddPeer.name, sfs);
+                               fcpm.send(os);
+                               os.flush();
+
+                               //ACK ?
+                       } catch(MessageInvalidException me){
+                               System.err.println("Invalid reference 
file!"+me);
+                               me.printStackTrace();
+                       }
+
+                       fcpSocket.close();
+                       System.out.println("That reference has been added");
+               }catch (SocketException se){
+                       System.err.println(se);
+                       se.printStackTrace();
+                       System.exit(1);
+               }catch (IOException ioe){
+                       System.err.println(ioe);
+                       ioe.printStackTrace();
+                       System.exit(2);
+               }               
+       }
+
+       protected SimpleFieldSet getMessage(LineReadingInputStream lis){
+               SimpleFieldSet sfs = new SimpleFieldSet();
+               sfs=new SimpleFieldSet();
+               try {
+                       while(lis.available()>0){
+                               String line = lis.readLine(128, 128, true);
+                               int index = line.indexOf('=');
+                               if(index == -1 || line.startsWith("End")) 
return sfs;
+                               sfs.put(line.substring(0, index), 
line.substring(index+1));
+                       }
+               }catch(IOException e){
+                       return sfs;
+               }
+
+               return sfs;
+       }
+}


Reply via email to