Author: toad
Date: 2006-12-18 22:06:05 +0000 (Mon, 18 Dec 2006)
New Revision: 11473

Modified:
   trunk/freenet/src/freenet/node/LocationManager.java
Log:
Spy on other people's swaps that go through us. Should result in much faster 
and more accurate size estimates.

Modified: trunk/freenet/src/freenet/node/LocationManager.java
===================================================================
--- trunk/freenet/src/freenet/node/LocationManager.java 2006-12-18 22:03:59 UTC 
(rev 11472)
+++ trunk/freenet/src/freenet/node/LocationManager.java 2006-12-18 22:06:05 UTC 
(rev 11473)
@@ -897,10 +897,11 @@
         } catch (NotConnectedException e) {
                if(logMINOR) Logger.minor(this, "Lost connection forwarding 
SwapCommit "+uid+" to "+item.routedTo);
         }
+        spyOnLocations(m);
         return true;
     }
-    
-    /**
+
+       /**
      * Handle an unmatched FNPSwapComplete
      * @return True if we recognized and forwarded this message.
      */
@@ -936,9 +937,36 @@
         }
         item.lastMessageTime = System.currentTimeMillis();
         removeRecentlyForwardedItem(item);
+        spyOnLocations(m);
         return true;
     }

+    /** Spy on locations in somebody else's swap request. Greatly increases the
+     * speed at which we can gather location data to estimate the network's 
size.
+     */
+    private void spyOnLocations(Message m) {
+        byte[] data = ((ShortBuffer)m.getObject(DMT.DATA)).getData();
+        if(data.length < 16 || data.length % 8 != 0) {
+               Logger.error(this, "Data invalid length in swap commit: 
"+data.length, new Exception("error"));
+               return;
+        }
+        
+        long[] longs = Fields.bytesToLongs(data);
+        // First field is his random
+        // Second field is his loc
+        double hisLoc = Double.longBitsToDouble(longs[1]);
+        if(hisLoc < 0.0 || hisLoc > 1.0) {
+               Logger.error(this, "Invalid hisLoc in swap commit: "+hisLoc, 
new Exception("error"));
+               return;
+        }
+        registerKnownLocation(hisLoc);
+        // Third etc are locs of peers
+        for(int i=2;i<longs.length;i++) {
+               double loc = Double.longBitsToDouble(longs[i]);
+               registerLocationLink(hisLoc, loc);
+        }
+       }
+
     public void clearOldSwapChains() {
         long now = System.currentTimeMillis();
         synchronized(recentlyForwardedIDs) {


Reply via email to