Author: nextgens
Date: 2006-08-14 13:57:23 +0000 (Mon, 14 Aug 2006)
New Revision: 10074

Added:
   trunk/freenet/src/freenet/support/AddRef.java
Modified:
   trunk/freenet/src/freenet/node/fcp/FCPServer.java
Log:
Some notyetworking code for freenet.support.AddRef

Modified: trunk/freenet/src/freenet/node/fcp/FCPServer.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/FCPServer.java   2006-08-14 10:13:49 UTC 
(rev 10073)
+++ trunk/freenet/src/freenet/node/fcp/FCPServer.java   2006-08-14 13:57:23 UTC 
(rev 10074)
@@ -44,6 +44,7 @@
  */
 public class FCPServer implements Runnable {

+       public final static int DEFAULT_FCP_PORT = 9481;
        NetworkInterface networkInterface;
        final NodeClientCore core;
        final Node node;
@@ -293,7 +294,7 @@
        public static FCPServer maybeCreate(Node node, NodeClientCore core, 
Config config) throws IOException, InvalidConfigValueException {
                SubConfig fcpConfig = new SubConfig("fcp", config);
                fcpConfig.register("enabled", true, 2, true, "Is FCP server 
enabled ?", "Is FCP server enabled ?", new FCPEnabledCallback(core));
-               fcpConfig.register("port", 9481 /* anagram of 1984, and 1000 up 
from old number */,
+               fcpConfig.register("port", FCPServer.DEFAULT_FCP_PORT /* 
anagram of 1984, and 1000 up from old number */,
                                2, true, "FCP port number", "FCP port number", 
new FCPPortNumberCallback(core));
                fcpConfig.register("bindTo", "127.0.0.1", 2, true, "Ip address 
to bind to", "Ip address to bind the FCP server to", new 
FCPBindtoCallback(core));
                fcpConfig.register("allowedHosts", "127.0.0.1", 2, true, 
"Allowed hosts", "Hostnames or IP addresses that are allowed to connect to the 
FCP server. May be a comma-separated list of hostnames, single IPs and even 
CIDR masked IPs like 192.168.0.0/24", new FCPAllowedHostsCallback(core));

Added: trunk/freenet/src/freenet/support/AddRef.java
===================================================================
--- trunk/freenet/src/freenet/support/AddRef.java       2006-08-14 10:13:49 UTC 
(rev 10073)
+++ trunk/freenet/src/freenet/support/AddRef.java       2006-08-14 13:57:23 UTC 
(rev 10074)
@@ -0,0 +1,80 @@
+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);
+               }               
+       }
+}


Reply via email to