[freenet-cvs] r14505 - in trunk/freenet/src/freenet: clients/http node

toad at freenetproject.org toad at freenetproject.org
Tue Aug 7 15:04:07 UTC 2007


Author: toad
Date: 2007-08-07 15:04:06 +0000 (Tue, 07 Aug 2007)
New Revision: 14505

Modified:
   trunk/freenet/src/freenet/clients/http/StatisticsToadlet.java
   trunk/freenet/src/freenet/node/CHKInsertSender.java
   trunk/freenet/src/freenet/node/FailureTableEntry.java
   trunk/freenet/src/freenet/node/InsertHandler.java
   trunk/freenet/src/freenet/node/Location.java
   trunk/freenet/src/freenet/node/LocationManager.java
   trunk/freenet/src/freenet/node/NodeDispatcher.java
   trunk/freenet/src/freenet/node/PeerManager.java
   trunk/freenet/src/freenet/node/RequestHandler.java
   trunk/freenet/src/freenet/node/RequestSender.java
   trunk/freenet/src/freenet/node/SSKInsertHandler.java
   trunk/freenet/src/freenet/node/SSKInsertSender.java
Log:
Move distance(...) from PeerManager to Location

Modified: trunk/freenet/src/freenet/clients/http/StatisticsToadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/StatisticsToadlet.java	2007-08-07 13:54:38 UTC (rev 14504)
+++ trunk/freenet/src/freenet/clients/http/StatisticsToadlet.java	2007-08-07 15:04:06 UTC (rev 14505)
@@ -17,6 +17,7 @@
 import freenet.io.comm.IOStatisticCollector;
 import freenet.l10n.L10n;
 import freenet.node.DarknetPeerNodeStatus;
+import freenet.node.Location;
 import freenet.node.Node;
 import freenet.node.NodeClientCore;
 import freenet.node.NodeStarter;
@@ -871,7 +872,7 @@
 		for (int peerIndex = 0; peerIndex < peerCount; peerIndex++) {
 			peerNodeStatus = peerNodeStatuses[peerIndex];
 			peerLocation = peerNodeStatus.getLocation();
-			peerDistance = PeerManager.distance( myLocation, peerLocation );
+			peerDistance = Location.distance( myLocation, peerLocation );
 			histogramIndex = (int) (Math.floor(peerDistance * HISTOGRAM_LENGTH * 2));
 			if (peerNodeStatus.isConnected()) {
 				histogramConnected[histogramIndex]++;

Modified: trunk/freenet/src/freenet/node/CHKInsertSender.java
===================================================================
--- trunk/freenet/src/freenet/node/CHKInsertSender.java	2007-08-07 13:54:38 UTC (rev 14504)
+++ trunk/freenet/src/freenet/node/CHKInsertSender.java	2007-08-07 15:04:06 UTC (rev 14505)
@@ -263,7 +263,7 @@
             
             Message req;
             synchronized (this) {
-            	if(PeerManager.distance(target, nextValue) > PeerManager.distance(target, closestLocation)) {
+            	if(Location.distance(target, nextValue) > Location.distance(target, closestLocation)) {
             		if(logMINOR) Logger.minor(this, "Backtracking: target="+target+" next="+nextValue+" closest="+closestLocation);
             		htl = node.decrementHTL(source, htl);
             	}

Modified: trunk/freenet/src/freenet/node/FailureTableEntry.java
===================================================================
--- trunk/freenet/src/freenet/node/FailureTableEntry.java	2007-08-07 13:54:38 UTC (rev 14504)
+++ trunk/freenet/src/freenet/node/FailureTableEntry.java	2007-08-07 15:04:06 UTC (rev 14505)
@@ -271,7 +271,7 @@
 			PeerNode pn = (PeerNode) requestedNodes[i].get();
 			if(pn == null) continue;
 			if(!(pn.isRoutable() && pn.isRoutingBackedOff())) continue;
-			double diff = PeerManager.distance(key.toNormalizedDouble(), requestedLocs[i]);
+			double diff = Location.distance(key.toNormalizedDouble(), requestedLocs[i]);
 			if(diff < bestDiff) bestDiff = diff;
 		}
 		return bestDiff;

Modified: trunk/freenet/src/freenet/node/InsertHandler.java
===================================================================
--- trunk/freenet/src/freenet/node/InsertHandler.java	2007-08-07 13:54:38 UTC (rev 14504)
+++ trunk/freenet/src/freenet/node/InsertHandler.java	2007-08-07 15:04:06 UTC (rev 14505)
@@ -58,7 +58,7 @@
         closestLoc = req.getDouble(DMT.NEAREST_LOCATION);
         double targetLoc = key.toNormalizedDouble();
         double myLoc = node.lm.getLocation().getValue();
-        if(PeerManager.distance(targetLoc, myLoc) < PeerManager.distance(targetLoc, closestLoc)) {
+        if(Location.distance(targetLoc, myLoc) < Location.distance(targetLoc, closestLoc)) {
             closestLoc = myLoc;
             htl = node.maxHTL();
         }

Modified: trunk/freenet/src/freenet/node/Location.java
===================================================================
--- trunk/freenet/src/freenet/node/Location.java	2007-08-07 13:54:38 UTC (rev 14504)
+++ trunk/freenet/src/freenet/node/Location.java	2007-08-07 15:04:06 UTC (rev 14505)
@@ -4,6 +4,7 @@
 package freenet.node;
 
 import freenet.crypt.RandomSource;
+import freenet.support.Logger;
 
 /**
  * @author amphibian
@@ -63,4 +64,28 @@
     public synchronized void randomize(RandomSource r) {
         setValue(r.nextDouble());
     }
+
+	static double distance(PeerNode p, double loc) {
+		double d = distance(p.getLocation().getValue(), loc);
+		return d;
+		//return d * p.getBias();
+	}
+
+	/**
+	 * Distance between two locations.
+	 * Both parameters must be in [0.0, 1.0].
+	 */
+	public static double distance(double a, double b) {
+		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();
+	    }
+	    // Circular keyspace
+		if (a > b) return Math.min (a - b, 1.0 - a + b);
+		else return Math.min (b - a, 1.0 - b + a);
+	}
 }

Modified: trunk/freenet/src/freenet/node/LocationManager.java
===================================================================
--- trunk/freenet/src/freenet/node/LocationManager.java	2007-08-07 13:54:38 UTC (rev 14504)
+++ trunk/freenet/src/freenet/node/LocationManager.java	2007-08-07 15:04:06 UTC (rev 14505)
@@ -640,22 +640,22 @@
         double A = 1.0;
         for(int i=0;i<friendLocs.length;i++) {
             if(Math.abs(friendLocs[i] - myLoc) <= Double.MIN_VALUE) continue;
-            A *= PeerManager.distance(friendLocs[i], myLoc);
+            A *= Location.distance(friendLocs[i], myLoc);
         }
         for(int i=0;i<hisFriendLocs.length;i++) {
             if(Math.abs(hisFriendLocs[i] - hisLoc) <= Double.MIN_VALUE) continue;
-            A *= PeerManager.distance(hisFriendLocs[i], hisLoc);
+            A *= Location.distance(hisFriendLocs[i], hisLoc);
         }
         
         // B = the same, with our two values swapped
         double B = 1.0;
         for(int i=0;i<friendLocs.length;i++) {
             if(Math.abs(friendLocs[i] - hisLoc) <= Double.MIN_VALUE) continue;
-            B *= PeerManager.distance(friendLocs[i], hisLoc);
+            B *= Location.distance(friendLocs[i], hisLoc);
         }
         for(int i=0;i<hisFriendLocs.length;i++) {
             if(Math.abs(hisFriendLocs[i] - myLoc) <= Double.MIN_VALUE) continue;
-            B *= PeerManager.distance(hisFriendLocs[i], myLoc);
+            B *= Location.distance(hisFriendLocs[i], myLoc);
         }
         
         //Logger.normal(this, "A="+A+" B="+B);

Modified: trunk/freenet/src/freenet/node/NodeDispatcher.java
===================================================================
--- trunk/freenet/src/freenet/node/NodeDispatcher.java	2007-08-07 13:54:38 UTC (rev 14504)
+++ trunk/freenet/src/freenet/node/NodeDispatcher.java	2007-08-07 15:04:06 UTC (rev 14505)
@@ -651,12 +651,12 @@
 		// Rejected, or even reply, cannot make nearest *worse* and thereby prolong the request.
 		// In fact, rejected probe requests result in clearing nearest at the beginning of the function, so it is vital
 		// that we restore it here.
-		if(PeerManager.distance(ctx.nearest, target, true) < PeerManager.distance(nearest, target, true)) {
+		if(Location.distance(ctx.nearest, target, true) < Location.distance(nearest, target, true)) {
 			nearest = ctx.nearest;
 		}
 		
 		// If we are closer to the target than nearest, update nearest and reset HTL, else decrement HTL
-		if(PeerManager.distance(myLoc, target, true) < PeerManager.distance(nearest, target, true)) {
+		if(Location.distance(myLoc, target, true) < Location.distance(nearest, target, true)) {
 			if(logMINOR)
 				Logger.minor(this, "Updating nearest to "+myLoc+" from "+nearest+" for "+target+" and resetting htl from "+htl+" to "+max);
 			if(Math.abs(nearest - myLoc) > Double.MIN_VALUE * 2)
@@ -707,8 +707,8 @@
 				public int compare(Object arg0, Object arg1) {
 					double d0 = ((Double) arg0).doubleValue();
 					double d1 = ((Double) arg1).doubleValue();
-					double dist0 = PeerManager.distance(d0, target, true);
-					double dist1 = PeerManager.distance(d1, target, true);
+					double dist0 = Location.distance(d0, target, true);
+					double dist1 = Location.distance(d1, target, true);
 					if(dist0 < dist1) return -1; // best at the beginning
 					if(dist0 > dist1) return 1;
 					return 0; // should not happen
@@ -871,8 +871,8 @@
 						public int compare(Object arg0, Object arg1) {
 							double d0 = ((Double) arg0).doubleValue();
 							double d1 = ((Double) arg1).doubleValue();
-							double dist0 = PeerManager.distance(d0, target, true);
-							double dist1 = PeerManager.distance(d1, target, true);
+							double dist0 = Location.distance(d0, target, true);
+							double dist1 = Location.distance(d1, target, true);
 							if(dist0 < dist1) return -1; // best at the beginning
 							if(dist0 > dist1) return 1;
 							return 0; // should not happen
@@ -883,7 +883,7 @@
 					
 					for(int i=0;i<notVisitedList.size();i++) {
 						double loc = ((Double)(notVisitedList.get(i))).doubleValue();
-						double dist = PeerManager.distance(loc, target);
+						double dist = Location.distance(loc, target);
 						if(dist > furthestDist) {
 							furthestDist = dist;
 						}

Modified: trunk/freenet/src/freenet/node/PeerManager.java
===================================================================
--- trunk/freenet/src/freenet/node/PeerManager.java	2007-08-07 13:54:38 UTC (rev 14504)
+++ trunk/freenet/src/freenet/node/PeerManager.java	2007-08-07 15:04:06 UTC (rev 14505)
@@ -516,7 +516,7 @@
             double peerloc = p.getLocation().getValue();
             if(Math.abs(peerloc - ignoreLoc) < Double.MIN_VALUE*2)
             	continue;
-            double diff = distance(peerloc, loc);
+            double diff = Location.distance(peerloc, loc);
             if(diff < bestDiff) {
             	foundOne = true;
                 bestDiff = diff;
@@ -531,7 +531,7 @@
                 double peerloc = p.getLocation().getValue();
                 if(Math.abs(peerloc - ignoreLoc) < Double.MIN_VALUE*2)
                 	continue;
-                double diff = distance(peerloc, loc);
+                double diff = Location.distance(peerloc, loc);
                 if(diff < bestDiff) {
                 	foundOne = true;
                     bestDiff = diff;
@@ -544,40 +544,16 @@
 
     public boolean isCloserLocation(double loc) {
     	double nodeLoc = node.lm.getLocation().getValue();
-    	double nodeDist = distance(nodeLoc, loc);
+    	double nodeDist = Location.distance(nodeLoc, loc);
     	double closest = closestPeerLocation(loc, nodeLoc);
     	if(closest > 1.0) {
     		// No peers found
     		return false;
     	}
-    	double closestDist = distance(closest, loc);
+    	double closestDist = Location.distance(closest, loc);
     	return closestDist < nodeDist;
     }
     
-    static double distance(PeerNode p, double loc) {
-    	double d = distance(p.getLocation().getValue(), loc);
-    	return d;
-    	//return d * p.getBias();
-    }
-    
-    /**
-     * Distance between two locations.
-     * Both parameters must be in [0.0, 1.0].
-     */
-    public static double distance(double a, double b) {
-    	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();
-        }
-        // Circular keyspace
-    	if (a > b) return Math.min (a - b, 1.0 - a + b);
-    	else return Math.min (b - a, 1.0 - b + a);
-    }
-
     public PeerNode closerPeer(PeerNode pn, Set routedTo, Set notIgnored, double loc, boolean ignoreSelf, boolean calculateMisrouting, int minVersion, Vector addUnpickedLocsTo) {
     	return closerPeer(pn, routedTo, notIgnored, loc, ignoreSelf, calculateMisrouting, minVersion, addUnpickedLocsTo, 2.0);
     }
@@ -593,7 +569,7 @@
     	if (calculateMisrouting) {
     		PeerNode nbo = _closerPeer(pn, routedTo, notIgnored, loc, ignoreSelf, true, minVersion, null, maxDistance);
     		if(nbo != null) {
-    			node.nodeStats.routingMissDistance.report(distance(best, nbo.getLocation().getValue()));
+    			node.nodeStats.routingMissDistance.report(Location.distance(best, nbo.getLocation().getValue()));
     			int numberOfConnected = getPeerNodeStatusSize(PEER_NODE_STATUS_CONNECTED);
     			int numberOfRoutingBackedOff = getPeerNodeStatusSize(PEER_NODE_STATUS_ROUTING_BACKED_OFF);
     			if (numberOfRoutingBackedOff + numberOfConnected > 0 ) {
@@ -627,7 +603,7 @@
         double bestDiff = Double.MAX_VALUE;
         double maxDiff = 0.0;
         if(!ignoreSelf)
-            maxDiff = distance(node.lm.getLocation().getValue(), target);
+            maxDiff = Location.distance(node.lm.getLocation().getValue(), target);
         PeerNode best = null;
         double bestLoc = -2;
         int count = 0;
@@ -654,9 +630,9 @@
             	continue;
             }
             count++;
-            double diff = distance(p, target);
+            double diff = Location.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(logMINOR) Logger.minor(this, "p.loc="+p.getLocation().getValue()+", target="+target+", d="+Location.distance(p.getLocation().getValue(), target)+" usedD="+diff+" for "+p.getPeer());
             if((!ignoreSelf) && (diff > maxDiff)) {
             	if(logMINOR) Logger.minor(this, "Ignoring because >maxDiff="+maxDiff);
             	continue;

Modified: trunk/freenet/src/freenet/node/RequestHandler.java
===================================================================
--- trunk/freenet/src/freenet/node/RequestHandler.java	2007-08-07 13:54:38 UTC (rev 14504)
+++ trunk/freenet/src/freenet/node/RequestHandler.java	2007-08-07 15:04:06 UTC (rev 14505)
@@ -57,7 +57,7 @@
         double myLoc = n.lm.getLocation().getValue();
         key = (Key) req.getObject(DMT.FREENET_ROUTING_KEY);
         double keyLoc = key.toNormalizedDouble();
-        if(PeerManager.distance(keyLoc, myLoc) < PeerManager.distance(keyLoc, closestLoc)) {
+        if(Location.distance(keyLoc, myLoc) < Location.distance(keyLoc, closestLoc)) {
             closestLoc = myLoc;
             htl = node.maxHTL();
             resetClosestLoc = true;

Modified: trunk/freenet/src/freenet/node/RequestSender.java
===================================================================
--- trunk/freenet/src/freenet/node/RequestSender.java	2007-08-07 13:54:38 UTC (rev 14504)
+++ trunk/freenet/src/freenet/node/RequestSender.java	2007-08-07 15:04:06 UTC (rev 14505)
@@ -154,7 +154,7 @@
             if(logMINOR) Logger.minor(this, "Routing request to "+next);
             nodesRoutedTo.add(next);
             
-            if(PeerManager.distance(target, nextValue) > PeerManager.distance(target, nearestLoc)) {
+            if(Location.distance(target, nextValue) > Location.distance(target, nearestLoc)) {
                 htl = node.decrementHTL(source, htl);
                 if(logMINOR) Logger.minor(this, "Backtracking: target="+target+" next="+nextValue+" closest="+nearestLoc+" so htl="+htl);
             }

Modified: trunk/freenet/src/freenet/node/SSKInsertHandler.java
===================================================================
--- trunk/freenet/src/freenet/node/SSKInsertHandler.java	2007-08-07 13:54:38 UTC (rev 14504)
+++ trunk/freenet/src/freenet/node/SSKInsertHandler.java	2007-08-07 15:04:06 UTC (rev 14505)
@@ -56,7 +56,7 @@
         closestLoc = req.getDouble(DMT.NEAREST_LOCATION);
         double targetLoc = key.toNormalizedDouble();
         double myLoc = node.lm.getLocation().getValue();
-        if(PeerManager.distance(targetLoc, myLoc) < PeerManager.distance(targetLoc, closestLoc)) {
+        if(Location.distance(targetLoc, myLoc) < Location.distance(targetLoc, closestLoc)) {
             closestLoc = myLoc;
             htl = node.maxHTL();
             resetClosestLoc = true;

Modified: trunk/freenet/src/freenet/node/SSKInsertSender.java
===================================================================
--- trunk/freenet/src/freenet/node/SSKInsertSender.java	2007-08-07 13:54:38 UTC (rev 14504)
+++ trunk/freenet/src/freenet/node/SSKInsertSender.java	2007-08-07 15:04:06 UTC (rev 14505)
@@ -152,7 +152,7 @@
             if(logMINOR) Logger.minor(this, "Routing insert to "+next);
             nodesRoutedTo.add(next);
             
-            if(PeerManager.distance(target, nextValue) > PeerManager.distance(target, closestLocation)) {
+            if(Location.distance(target, nextValue) > Location.distance(target, closestLocation)) {
             	if(logMINOR) Logger.minor(this, "Backtracking: target="+target+" next="+nextValue+" closest="+closestLocation);
                 htl = node.decrementHTL(source, htl);
             }




More information about the cvs mailing list