Update of /cvsroot/freenet/Freenet0.7Rewrite/src/freenet/node
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22884/src/freenet/node

Modified Files:
        Node.java Version.java PeerManager.java InsertHandler.java 
        TextModeClientInterface.java RequestSender.java 
        RequestHandler.java InsertSender.java 
Log Message:
Build 127 (mandatory):
New backtracking.
Ability to add nodes from the command line rather than editing a file.

Index: Node.java
===================================================================
RCS file: /cvsroot/freenet/Freenet0.7Rewrite/src/freenet/node/Node.java,v
retrieving revision 1.45
retrieving revision 1.46
diff -u -w -r1.45 -r1.46
--- Node.java   25 Aug 2005 17:28:19 -0000      1.45
+++ Node.java   7 Sep 2005 14:34:47 -0000       1.46
@@ -104,7 +104,7 @@
     final PacketSender ps;
     final NodeDispatcher dispatcher;
     final String filenamesPrefix;
-    static short MAX_HTL = 20;
+    static short MAX_HTL = 10;
     private static final int EXIT_STORE_FILE_NOT_FOUND = 1;
     private static final int EXIT_STORE_IOEXCEPTION = 2;
     private static final int EXIT_STORE_OTHER = 3;
@@ -304,7 +304,7 @@
      * Either it succeeds or it doesn't.
      */
     public ClientCHKBlock getCHK(ClientCHK key) {
-        Object o = makeRequestSender(key.getNodeCHK(), MAX_HTL, 
random.nextLong(), null);
+        Object o = makeRequestSender(key.getNodeCHK(), MAX_HTL, 
random.nextLong(), null, lm.loc.getValue());
         if(o instanceof CHKBlock) {
             try {
                 return new ClientCHKBlock((CHKBlock)o, key);
@@ -344,7 +344,7 @@
                 Logger.error(this, "Datastore failure: "+e, e);
             }
             is = makeInsertSender(block.getClientKey().getNodeCHK(), 
-                    MAX_HTL, uid, null, headers, prb, false);
+                    MAX_HTL, uid, null, headers, prb, false, 
lm.getLocation().getValue());
         }
         is.waitUntilFinished();
         if(is.getStatus() == InsertSender.SUCCESS) {
@@ -430,11 +430,12 @@
      * check whether another node is requesting the same key at
      * the same HTL, and if all else fails, create a new 
      * RequestSender for the key/htl.
+     * @param closestLocation The closest location to the key so far.
      * @return A CHKBlock if the data is in the store, otherwise
      * a RequestSender, unless the HTL is 0, in which case NULL.
      * RequestSender.
      */
-    public synchronized Object makeRequestSender(NodeCHK key, short htl, long 
uid, PeerNode source) {
+    public synchronized Object makeRequestSender(NodeCHK key, short htl, long 
uid, PeerNode source, double closestLocation) {
         Logger.minor(this, 
"makeRequestSender("+key+","+htl+","+uid+","+source+") on "+portNumber);
         // In store?
         CHKBlock chk = null;
@@ -467,7 +468,7 @@
             return sender;
         }

-        sender = new RequestSender(key, htl, uid, this, source);
+        sender = new RequestSender(key, htl, uid, this, closestLocation, 
source);
         requestSenders.put(kh, sender);
         Logger.minor(this, "Created new sender: "+sender);
         return sender;
@@ -601,7 +602,7 @@
      * if it originated locally.
      */
     public synchronized InsertSender makeInsertSender(NodeCHK key, short htl, 
long uid, PeerNode source,
-            byte[] headers, PartiallyReceivedBlock prb, boolean fromStore) {
+            byte[] headers, PartiallyReceivedBlock prb, boolean fromStore, 
double closestLoc) {
         Logger.minor(this, 
"makeInsertSender("+key+","+htl+","+uid+","+source+",...,"+fromStore);
         KeyHTLPair kh = new KeyHTLPair(key, htl);
         InsertSender is = (InsertSender) insertSenders.get(kh);
@@ -609,7 +610,7 @@
             Logger.minor(this, "Found "+is+" for "+kh);
             return is;
         }
-        is = new InsertSender(key, uid, headers, htl, source, this, prb, 
fromStore);
+        is = new InsertSender(key, uid, headers, htl, source, this, prb, 
fromStore, closestLoc);
         Logger.minor(this, is.toString()+" for "+kh.toString());
         insertSenders.put(kh, is);
         return is;

Index: Version.java
===================================================================
RCS file: /cvsroot/freenet/Freenet0.7Rewrite/src/freenet/node/Version.java,v
retrieving revision 1.121
retrieving revision 1.122
diff -u -w -r1.121 -r1.122
--- Version.java        25 Aug 2005 17:28:19 -0000      1.121
+++ Version.java        7 Sep 2005 14:34:47 -0000       1.122
@@ -20,10 +20,10 @@
        public static final String protocolVersion = "1.0";

        /** The build number of the current revision */
-       public static final int buildNumber = 126;
+       public static final int buildNumber = 127;

        /** Oldest build of Fred we will talk to */
-       public static final int lastGoodBuild = 126;
+       public static final int lastGoodBuild = 127;

        /** The highest reported build of fred */
        public static int highestSeenBuild = buildNumber;

Index: PeerManager.java
===================================================================
RCS file: /cvsroot/freenet/Freenet0.7Rewrite/src/freenet/node/PeerManager.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -w -r1.25 -r1.26
--- PeerManager.java    25 Aug 2005 17:28:19 -0000      1.25
+++ PeerManager.java    7 Sep 2005 14:34:47 -0000       1.26
@@ -91,18 +91,16 @@
         }
     }

-    /**
-     * @param pn
-     */
-    private synchronized void addPeer(PeerNode pn) {
+    public synchronized boolean addPeer(PeerNode pn) {
         for(int i=0;i<myPeers.length;i++) {
-            if(myPeers[i] == pn) return;
+            if(myPeers[i] == pn) return false;
         }
         PeerNode[] newMyPeers = new PeerNode[myPeers.length+1];
         System.arraycopy(myPeers, 0, newMyPeers, 0, myPeers.length);
         newMyPeers[myPeers.length] = pn;
         myPeers = newMyPeers;
         Logger.normal(this, "Added "+pn);
+        return true;
     }

     public synchronized void addConnectedPeer(PeerNode pn) {

Index: InsertHandler.java
===================================================================
RCS file: 
/cvsroot/freenet/Freenet0.7Rewrite/src/freenet/node/InsertHandler.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -w -r1.12 -r1.13
--- InsertHandler.java  25 Aug 2005 17:28:19 -0000      1.12
+++ InsertHandler.java  7 Sep 2005 14:34:47 -0000       1.13
@@ -32,6 +32,7 @@
     final PeerNode source;
     final NodeCHK key;
     final long startTime;
+    private double closestLoc;
     private short htl;
     private InsertSender sender;
     private byte[] headers;
@@ -48,6 +49,11 @@
         this.startTime = startTime;
         key = (NodeCHK) req.getObject(DMT.FREENET_ROUTING_KEY);
         htl = req.getShort(DMT.HTL);
+        closestLoc = req.getDouble(DMT.NEAREST_LOCATION);
+        double targetLoc = key.toNormalizedDouble();
+        double myLoc = node.lm.getLocation().getValue();
+        if(Math.abs(targetLoc - myLoc) < Math.abs(targetLoc - closestLoc))
+            closestLoc = myLoc;
     }

     public String toString() {
@@ -95,7 +101,7 @@

         prb = new PartiallyReceivedBlock(Node.PACKETS_IN_BLOCK, 
Node.PACKET_SIZE);
         if(htl > 0)
-            sender = node.makeInsertSender(key, htl, uid, source, headers, 
prb, false);
+            sender = node.makeInsertSender(key, htl, uid, source, headers, 
prb, false, closestLoc);
         br = new BlockReceiver(node.usm, source, uid, prb);

         // Receive the data, off thread

Index: TextModeClientInterface.java
===================================================================
RCS file: 
/cvsroot/freenet/Freenet0.7Rewrite/src/freenet/node/TextModeClientInterface.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -w -r1.13 -r1.14
--- TextModeClientInterface.java        25 Aug 2005 17:28:19 -0000      1.13
+++ TextModeClientInterface.java        7 Sep 2005 14:34:48 -0000       1.14
@@ -12,6 +12,7 @@
 import java.net.MalformedURLException;

 import freenet.crypt.RandomSource;
+import freenet.io.comm.PeerParseException;
 import freenet.keys.CHKBlock;
 import freenet.keys.CHKDecodeException;
 import freenet.keys.CHKEncodeException;
@@ -19,6 +20,7 @@
 import freenet.keys.ClientCHKBlock;
 import freenet.keys.FreenetURI;
 import freenet.support.Logger;
+import freenet.support.SimpleFieldSet;

 /**
  * @author amphibian
@@ -52,6 +54,8 @@
         System.out.println("PUT:<text> - put a single line of text to a CHK 
and return the key.");
         System.out.println("PUTFILE:<filename> - put a file from disk.");
         System.out.println("GETFILE:<filename> - fetch a key and put it in a 
file. If the key includes a filename we will use it but we will not overwrite 
local files.");
+        System.out.println("CONNECT:<filename> - connect to a node from its 
ref in a file.");
+        System.out.println("CONNECT:\n<text, until a . on a line by itself> - 
enter a noderef directly.");
         System.out.println("STATUS - display some status information on the 
node.");
         System.out.println("QUIT - exit the program");
         // Read command, and data
@@ -260,11 +264,78 @@
             }
         } else if(line.startsWith("STATUS")) {
             System.out.println(n.getStatus());
+        } else if(line.startsWith("CONNECT:")) {
+            String key = line.substring("CONNECT:".length());
+            while(key.length() > 0 && key.charAt(0) == ' ')
+                key = key.substring(1);
+            while(key.length() > 0 && key.charAt(key.length()-1) == ' ')
+                key = key.substring(0, key.length()-2);
+            if(key.length() > 0) {
+                // Filename
+                System.out.println("Trying to connect to noderef in "+key);
+                File f = new File(line);
+                System.out.println("Attempting to read file "+line);
+                try {
+                    FileInputStream fis = new FileInputStream(line);
+                    DataInputStream dis = new DataInputStream(fis);
+                    int length = (int)f.length();
+                    byte[] data = new byte[length];
+                    dis.readFully(data);
+                    dis.close();
+                    connect(new String(data));
+                } catch (IOException e) {
+                    System.err.println("Could not read file: "+e);
+                    e.printStackTrace(System.err);
+                }
+            } else {
+                StringBuffer sb = new StringBuffer(1000);
+                while(true) {
+                    try {
+                        line = reader.readLine();
+                        if(line == null) throw new EOFException();
+                    } catch (IOException e1) {
+                        System.err.println("Bye... ("+e1+")");
+                        return;
+                    }
+                    if(line.equals(".")) break;
+                    sb.append(line).append('\n');
+                }
+                String content = sb.toString();
+                connect(content);
+            }
         } else {

         }
     }

+    /**
+     * Connect to a node, given its reference.
+     */
+    private void connect(String content) {
+        SimpleFieldSet fs;
+        try {
+            fs = new SimpleFieldSet(content);
+        } catch (IOException e) {
+            System.err.println("Did not parse: "+e);
+            e.printStackTrace();
+            return;
+        }
+        PeerNode pn;
+        try {
+            pn = new PeerNode(fs, n);
+        } catch (FSParseException e1) {
+            System.err.println("Did not parse: "+e1);
+            e1.printStackTrace();
+            return;
+        } catch (PeerParseException e1) {
+            System.err.println("Did not parse: "+e1);
+            e1.printStackTrace();
+            return;
+        }
+        if(n.peers.addPeer(pn))
+            System.out.println("Added peer: "+pn);
+    }
+
     private String sanitize(String fnam) {
         StringBuffer sb = new StringBuffer(fnam.length());
         for(int i=0;i<fnam.length();i++) {

Index: RequestSender.java
===================================================================
RCS file: 
/cvsroot/freenet/Freenet0.7Rewrite/src/freenet/node/RequestSender.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -w -r1.10 -r1.11
--- RequestSender.java  25 Aug 2005 17:28:19 -0000      1.10
+++ RequestSender.java  7 Sep 2005 14:34:48 -0000       1.11
@@ -39,6 +39,7 @@
     private short htl;
     final long uid;
     final Node node;
+    private double nearestLoc;
     /** The source of this request if any - purely so we can avoid routing to 
it */
     final PeerNode source;
     private PartiallyReceivedBlock prb = null;
@@ -63,13 +64,15 @@
         return super.toString()+" for "+uid;
     }

-    public RequestSender(NodeCHK key, short htl, long uid, Node n, 
+    public RequestSender(NodeCHK key, short htl, long uid, Node n, double 
nearestLoc, 
             PeerNode source) {
         this.key = key;
         this.htl = htl;
         this.uid = uid;
         this.node = n;
         this.source = source;
+        this.nearestLoc = nearestLoc;
+        
         target = key.toNormalizedDouble();
         Thread t = new Thread(this, "RequestSender for UID "+uid);
         t.setDaemon(true);
@@ -78,7 +81,6 @@

     public void run() {
         short origHTL = htl;
-        Message req = DMT.createFNPDataRequest(uid, htl, key);
         node.addSender(key, htl, this);
         HashSet nodesRoutedTo = new HashSet();
         try {
@@ -91,18 +93,33 @@
                 finish(ROUTE_NOT_FOUND);
                 return;
             }
+            
             // Route it
             PeerNode next;
-            // Can backtrack, so only route to nodes closer than we are to 
target.
+            double nextValue;
+            synchronized(node.peers) {
             next = node.peers.closerPeer(source, nodesRoutedTo, target, source 
== null);
+                if(next != null)
+                    nextValue = next.getLocation().getValue();
+                else
+                    nextValue = -1.0;
+            }

             if(next == null) {
-                // RNF
+                // Backtrack
                 finish(ROUTE_NOT_FOUND);
                 return;
             }
+            Logger.minor(this, "Routing insert to "+next);
             nodesRoutedTo.add(next);

+            if(Math.abs(target - nextValue) > Math.abs(target - nearestLoc)) {
+                Logger.minor(this, "Backtracking: target="+target+" 
next="+nextValue+" closest="+nearestLoc);
+                htl = node.decrementHTL(source, htl);
+            }
+            
+            Message req = DMT.createFNPDataRequest(uid, htl, key, nearestLoc);
+            
             /**
              * What are we waiting for?
              * FNPAccepted - continue
@@ -138,7 +155,6 @@
             }

             if(msg.getSpec() == DMT.FNPRejectedLoop) {
-                htl = node.decrementHTL(source, htl);
                 // Find another node to route to
                 continue;
             }
@@ -181,7 +197,6 @@
             if(msg.getSpec() == DMT.FNPRouteNotFound) {
                 // Backtrack within available hops
                 short newHtl = msg.getShort(DMT.HTL);
-                htl = node.decrementHTL(source, htl);
                 if(newHtl < htl) htl = newHtl;
                 continue;
             }

Index: RequestHandler.java
===================================================================
RCS file: 
/cvsroot/freenet/Freenet0.7Rewrite/src/freenet/node/RequestHandler.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -w -r1.8 -r1.9
--- RequestHandler.java 25 Aug 2005 17:28:19 -0000      1.8
+++ RequestHandler.java 7 Sep 2005 14:34:48 -0000       1.9
@@ -18,6 +18,10 @@
     final Message req;
     final Node node;
     final long uid;
+    private short htl;
+    final PeerNode source;
+    private double closestLoc;
+    final NodeCHK key;

     public String toString() {
         return super.toString()+" for "+uid;
@@ -27,20 +31,25 @@
         req = m;
         node = n;
         uid = id;
+        htl = req.getShort(DMT.HTL);
+        source = (PeerNode) req.getSource();
+        closestLoc = req.getDouble(DMT.NEAREST_LOCATION);
+        double myLoc = n.lm.getLocation().getValue();
+        // FIXME should be more generic when implement SSKs
+        key = (NodeCHK) req.getObject(DMT.FREENET_ROUTING_KEY);
+        double keyLoc = key.toNormalizedDouble();
+        if(Math.abs(keyLoc - myLoc) < Math.abs(keyLoc - closestLoc))
+            closestLoc = myLoc;
     }

     public void run() {
         try {
-        short htl = req.getShort(DMT.HTL);
-        PeerNode source = (PeerNode) req.getSource();
         htl = source.decrementHTL(htl);
-        // FIXME should be more generic when implement SSKs
-        NodeCHK key = (NodeCHK) req.getObject(DMT.FREENET_ROUTING_KEY);

         Message accepted = DMT.createFNPAccepted(uid);
         source.send(accepted);

-        Object o = node.makeRequestSender(key, htl, uid, source);
+        Object o = node.makeRequestSender(key, htl, uid, source, closestLoc);
         if(o instanceof CHKBlock) {
             CHKBlock block = (CHKBlock) o;
             Message df = DMT.createFNPDataFound(uid, block.getHeader());

Index: InsertSender.java
===================================================================
RCS file: 
/cvsroot/freenet/Freenet0.7Rewrite/src/freenet/node/InsertSender.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -w -r1.12 -r1.13
--- InsertSender.java   25 Aug 2005 17:28:19 -0000      1.12
+++ InsertSender.java   7 Sep 2005 14:34:48 -0000       1.13
@@ -16,7 +16,7 @@
 public final class InsertSender implements Runnable {

     InsertSender(NodeCHK myKey, long uid, byte[] headers, short htl, 
-            PeerNode source, Node node, PartiallyReceivedBlock prb, boolean 
fromStore) {
+            PeerNode source, Node node, PartiallyReceivedBlock prb, boolean 
fromStore, double closestLocation) {
         this.myKey = myKey;
         this.target = myKey.toNormalizedDouble();
         this.uid = uid;
@@ -26,6 +26,7 @@
         this.node = node;
         this.prb = prb;
         this.fromStore = fromStore;
+        this.closestLocation = closestLocation;
         Thread t = new Thread(this, "InsertSender for UID "+uid+" on 
"+node.portNumber);
         t.setDaemon(true);
         t.start();
@@ -46,6 +47,7 @@
     final PartiallyReceivedBlock prb;
     final boolean fromStore;
     private boolean receiveFailed = false;
+    final double closestLocation;

     private int status = -1;
     static final int NOT_FINISHED = -1;
@@ -63,8 +65,6 @@
         HashSet nodesRoutedTo = new HashSet();

         while(true) {
-            Message req = DMT.createFNPInsertRequest(uid, htl, myKey);
-            
             if(receiveFailed) return; // don't need to set status as killed by 
InsertHandler

             if(htl == 0) {
@@ -76,7 +76,14 @@
             // Route it
             PeerNode next;
             // Can backtrack, so only route to nodes closer than we are to 
target.
+            double nextValue;
+            synchronized(node.peers) {
             next = node.peers.closerPeer(source, nodesRoutedTo, target, source 
== null);
+                if(next != null)
+                    nextValue = next.getLocation().getValue();
+                else
+                    nextValue = -1.0;
+            }

             if(next == null) {
                 // Backtrack
@@ -86,6 +93,13 @@
             Logger.minor(this, "Routing insert to "+next);
             nodesRoutedTo.add(next);

+            if(Math.abs(target - nextValue) > Math.abs(target - 
closestLocation)) {
+                Logger.minor(this, "Backtracking: target="+target+" 
next="+nextValue+" closest="+closestLocation);
+                htl = node.decrementHTL(source, htl);
+            }
+            
+            Message req = DMT.createFNPInsertRequest(uid, htl, myKey);
+            
             // Wait for ack or reject... will come before even a locally 
generated DataReply

             MessageFilter mfAccepted = 
MessageFilter.create().setSource(next).setField(DMT.UID, 
uid).setTimeout(ACCEPTED_TIMEOUT).setType(DMT.FNPAccepted);
@@ -114,13 +128,11 @@

             if(msg == null) {
                 // No response, move on
-                htl = node.decrementHTL(source, htl);
                 continue;
             }

             if(msg.getSpec() == DMT.FNPRejectedLoop) {
                 // Loop - we don't want to send the data to this one
-                htl = node.decrementHTL(source, htl);
                 continue;
             }

@@ -197,7 +209,6 @@
                 Logger.minor(this, "Rejected: RNF");
                 // Still gets the data - but not yet
                 short newHtl = msg.getShort(DMT.HTL);
-                htl = node.decrementHTL(source, htl);
                 if(htl > newHtl) htl = newHtl;
                 continue;
             }


Reply via email to