Author: toad
Date: 2007-06-20 20:16:36 +0000 (Wed, 20 Jun 2007)
New Revision: 13663

Modified:
   trunk/freenet/src/freenet/node/NodeDispatcher.java
   trunk/freenet/src/freenet/node/PeerManager.java
Log:
PROBE REQUESTS:
Don't use best/nearest from probe rejections (rejections come from dead ends...)
Be tolerant of best/nearest=silly numbers, but don't set them that way yet.
Only fork if the node is closer than the 3rd-best-so-far-not-visited, in 
accordance with original plans.

Modified: trunk/freenet/src/freenet/node/NodeDispatcher.java
===================================================================
--- trunk/freenet/src/freenet/node/NodeDispatcher.java  2007-06-20 18:45:54 UTC 
(rev 13662)
+++ trunk/freenet/src/freenet/node/NodeDispatcher.java  2007-06-20 20:16:36 UTC 
(rev 13663)
@@ -496,7 +496,7 @@
                        for(int i=0;i<locsNotVisited.length;i++)
                                notVisitedList.add(new 
Double(locsNotVisited[i]));
                }
-               innerHandleProbeRequest(src, id, lid, target, best, nearest, 
htl, counter, true, true, false, null, notVisitedList);
+               innerHandleProbeRequest(src, id, lid, target, best, nearest, 
htl, counter, true, true, false, null, notVisitedList, 2.0);
                return true;
        }

@@ -517,11 +517,17 @@
         * @param canReject
         * @param cb
         * @param locsNotVisited 
+        * @param maxDistance 
+        * @param furthest 
         * @return
         */
        private void innerHandleProbeRequest(PeerNode src, long id, Long lid, 
final double target, double best, 
                        double nearest, short htl, short counter, boolean 
checkRecent, boolean canReject, 
-                       boolean fromRejection, ProbeCallback cb, Vector 
locsNotVisited) {
+                       boolean fromRejection, ProbeCallback cb, Vector 
locsNotVisited, double maxDistance) {
+               if(fromRejection) {
+                       nearest = furthestLoc(target);
+                       best = furthestGreater(target);
+               }
                short max = node.maxHTL();
                if(htl > max) htl = max;
                if(htl <= 1) htl = 1;
@@ -609,10 +615,11 @@

                // Update nearest, htl

-               if(PeerManager.distance(myLoc, target) < 
PeerManager.distance(nearest, target)) {
+               if(PeerManager.distance(myLoc, target, true) < 
PeerManager.distance(nearest, target, true)) {
                        if(logMINOR)
                                Logger.minor(this, "Updating nearest to 
"+myLoc+" from "+nearest+" for "+target+" and resetting htl from "+htl+" to 
"+max);
-                       nearest = myLoc;
+                       if(Math.abs(nearest - myLoc) > Double.MIN_VALUE * 2)
+                               nearest = myLoc;
                        htl = max;
                        ctx.nearest = nearest;
                        ctx.htl = htl;
@@ -649,7 +656,7 @@

                        Vector newBestLocs = new Vector();
                        newBestLocs.addAll(locsNotVisited);
-                       PeerNode pn = node.peers.closerPeer(src, visited, null, 
target, true, false, 965, newBestLocs);
+                       PeerNode pn = node.peers.closerPeer(src, visited, null, 
target, true, false, 965, newBestLocs, maxDistance);

                        if(logMINOR)
                                Logger.minor(this, "newBestLocs (unsorted): 
"+newBestLocs);
@@ -659,8 +666,8 @@
                                public int compare(Object arg0, Object arg1) {
                                        double d0 = ((Double) 
arg0).doubleValue();
                                        double d1 = ((Double) 
arg1).doubleValue();
-                                       double dist0 = PeerManager.distance(d0, 
target);
-                                       double dist1 = PeerManager.distance(d1, 
target);
+                                       double dist0 = PeerManager.distance(d0, 
target, true);
+                                       double dist1 = PeerManager.distance(d1, 
target, true);
                                        if(dist0 < dist1) return -1; // best at 
the beginning
                                        if(dist0 > dist1) return 1;
                                        return 0; // should not happen
@@ -785,10 +792,18 @@

                // Maybe fork

+               double furthestDist = 0.0;
                if(notVisitedList.size() > 0) {
                        if(ctx.forkCount < MAX_FORKS) {
                                ctx.forkCount++;
-                               innerHandleProbeRequest(src, id, lid, target, 
best, nearest, ctx.htl, counter, false, false, false, null, notVisitedList);
+                               for(int i=0;i<notVisitedList.size();i++) {
+                                       double loc = 
((Double)(notVisitedList.get(i))).doubleValue();
+                                       double dist = PeerManager.distance(loc, 
target);
+                                       if(dist > furthestDist) {
+                                               furthestDist = dist;
+                                       }
+                               }
+                               innerHandleProbeRequest(src, id, lid, target, 
best, nearest, ctx.htl, counter, false, false, false, null, notVisitedList, 
furthestDist);
                                return true;
                        }
                }
@@ -886,7 +901,7 @@
                        for(int i=0;i<locsNotVisited.length;i++)
                                notVisitedList.add(new 
Double(locsNotVisited[i]));
                }
-               innerHandleProbeRequest(src, id, lid, target, best, nearest, 
htl, counter, false, false, true, null, notVisitedList);
+               innerHandleProbeRequest(src, id, lid, target, best, nearest, 
htl, counter, false, false, true, null, notVisitedList, 2.0);
                return true;
        }

@@ -897,9 +912,19 @@
                        recentProbeRequestIDs.push(ll);
                }
                double nodeLoc = node.getLocation();
-               innerHandleProbeRequest(null, l, ll, d, (nodeLoc > d) ? nodeLoc 
: 1.0, nodeLoc, node.maxHTL(), (short)0, false, false, false, cb, new Vector());
+               innerHandleProbeRequest(null, l, ll, d, (nodeLoc > d) ? nodeLoc 
: furthestGreater(d), nodeLoc, node.maxHTL(), (short)0, false, false, false, 
cb, new Vector(), 2.0);
        }

+       private double furthestLoc(double d) {
+               if(d > 0.5) return d - 0.5;
+               return d + 0.5;
+       }
+       
+       private double furthestGreater(double d) {
+               if(d < 0.5) return d + 0.5;
+               return 1.0;
+       }
+
        void start(NodeStats stats) {
                this.nodeStats = stats;
        }

Modified: trunk/freenet/src/freenet/node/PeerManager.java
===================================================================
--- trunk/freenet/src/freenet/node/PeerManager.java     2007-06-20 18:45:54 UTC 
(rev 13662)
+++ trunk/freenet/src/freenet/node/PeerManager.java     2007-06-20 20:16:36 UTC 
(rev 13663)
@@ -536,7 +536,11 @@
      * Both parameters must be in [0.0, 1.0].
      */
     public static double distance(double a, double b) {
-        if((a < 0.0 || a > 1.0)||(b < 0.0 || b > 1.0)) {
+       return distance(a, b, false);
+    }
+    
+    public static double distance(double a, double b, boolean allowCrazy) {
+        if(((a < 0.0 || a > 1.0)||(b < 0.0 || b > 1.0)) && !allowCrazy) {
                Logger.error(PeerManager.class, "Invalid Location ! a = "+a +" 
b = "+ b + "Please report this bug!", new Exception("error"));
                throw new NullPointerException();
         }
@@ -545,16 +549,20 @@
        else return Math.min (b - a, 1.0 - b + a);
     }

+    public PeerNode closerPeer(PeerNode pn, HashSet routedTo, HashSet 
notIgnored, double loc, boolean ignoreSelf, boolean calculateMisrouting, int 
minVersion, Vector addUnpickedLocsTo) {
+       return closerPeer(pn, routedTo, notIgnored, loc, ignoreSelf, 
calculateMisrouting, minVersion, addUnpickedLocsTo, 2.0);
+    }
+    
     /*
      * FIXME
      * This scans the same array 4 times.  It would be better to scan once and 
execute 4 callbacks...
      * For this reason the metrics are only updated if advanced mode is enabled
      */
-    public PeerNode closerPeer(PeerNode pn, HashSet routedTo, HashSet 
notIgnored, double loc, boolean ignoreSelf, boolean calculateMisrouting, int 
minVersion, Vector addUnpickedLocsTo) {
-       PeerNode best = closerPeerBackoff(pn, routedTo, notIgnored, loc, 
ignoreSelf, minVersion, addUnpickedLocsTo);
+    public PeerNode closerPeer(PeerNode pn, HashSet routedTo, HashSet 
notIgnored, double loc, boolean ignoreSelf, boolean calculateMisrouting, int 
minVersion, Vector addUnpickedLocsTo, double maxDistance) {
+       PeerNode best = closerPeerBackoff(pn, routedTo, notIgnored, loc, 
ignoreSelf, minVersion, addUnpickedLocsTo, maxDistance);

        if (calculateMisrouting) {
-               PeerNode nbo = _closerPeer(pn, routedTo, notIgnored, loc, 
ignoreSelf, true, minVersion, null);
+               PeerNode nbo = _closerPeer(pn, routedTo, notIgnored, loc, 
ignoreSelf, true, minVersion, null, maxDistance);
                if(nbo != null) {
                        
node.nodeStats.routingMissDistance.report(distance(best, 
nbo.getLocation().getValue()));
                        int numberOfConnected = 
getPeerNodeStatusSize(PEER_NODE_STATUS_CONNECTED);
@@ -567,10 +575,10 @@
        return best;
     }

-    private PeerNode closerPeerBackoff(PeerNode pn, HashSet routedTo, HashSet 
notIgnored, double loc, boolean ignoreSelf, int minVersion, Vector 
addUnpickedLocsTo) {
-       PeerNode best = _closerPeer(pn, routedTo, notIgnored, loc, ignoreSelf, 
false, minVersion, addUnpickedLocsTo);
+    private PeerNode closerPeerBackoff(PeerNode pn, HashSet routedTo, HashSet 
notIgnored, double loc, boolean ignoreSelf, int minVersion, Vector 
addUnpickedLocsTo, double maxDistance) {
+       PeerNode best = _closerPeer(pn, routedTo, notIgnored, loc, ignoreSelf, 
false, minVersion, addUnpickedLocsTo, maxDistance);
        if(best == null) {
-               best = _closerPeer(pn, routedTo, notIgnored, loc, ignoreSelf, 
true, minVersion, addUnpickedLocsTo);
+               best = _closerPeer(pn, routedTo, notIgnored, loc, ignoreSelf, 
true, minVersion, addUnpickedLocsTo, maxDistance);
        }
        return best;
        }
@@ -581,7 +589,7 @@
         * @param addUnpickedLocsTo Add all locations we didn't choose which we 
could have routed to to 
         * this array. Remove the location of the peer we pick from it.
      */
-    private PeerNode _closerPeer(PeerNode pn, HashSet routedTo, HashSet 
notIgnored, double target, boolean ignoreSelf, boolean ignoreBackedOff, int 
minVersion, Vector addUnpickedLocsTo) {
+    private PeerNode _closerPeer(PeerNode pn, HashSet routedTo, HashSet 
notIgnored, double target, boolean ignoreSelf, boolean ignoreBackedOff, int 
minVersion, Vector addUnpickedLocsTo, double maxDistance) {
         PeerNode[] peers;  
         synchronized (this) {
                        peers = connectedPeers;
@@ -618,6 +626,7 @@
             }
             count++;
             double diff = distance(p, target);
+            if(diff > maxDistance) continue;
             if(logMINOR) Logger.minor(this, 
"p.loc="+p.getLocation().getValue()+", target="+target+", 
d="+distance(p.getLocation().getValue(), target)+" usedD="+diff+" for 
"+p.getPeer());
             if((!ignoreSelf) && (diff > maxDiff)) {
                if(logMINOR) Logger.minor(this, "Ignoring because 
>maxDiff="+maxDiff);


Reply via email to