Author: toad
Date: 2007-04-18 20:15:12 +0000 (Wed, 18 Apr 2007)
New Revision: 12796

Modified:
   trunk/freenet/src/freenet/node/LocationManager.java
   trunk/freenet/src/freenet/support/Fields.java
Log:
Refactor slightly

Modified: trunk/freenet/src/freenet/node/LocationManager.java
===================================================================
--- trunk/freenet/src/freenet/node/LocationManager.java 2007-04-18 19:56:42 UTC 
(rev 12795)
+++ trunk/freenet/src/freenet/node/LocationManager.java 2007-04-18 20:15:12 UTC 
(rev 12796)
@@ -952,26 +952,28 @@
      */
     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]);
+        double[] locations = Fields.bytesToDoubles(data, 8, data.length-8);
+        
+        double hisLoc = locations[0];
         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]);
+        
+        for(int i=1;i<locations.length;i++) {
+               double loc = locations[i];
                registerKnownLocation(loc);
                registerLocationLink(hisLoc, loc);
         }
+        
        }

     public void clearOldSwapChains() {

Modified: trunk/freenet/src/freenet/support/Fields.java
===================================================================
--- trunk/freenet/src/freenet/support/Fields.java       2007-04-18 19:56:42 UTC 
(rev 12795)
+++ trunk/freenet/src/freenet/support/Fields.java       2007-04-18 20:15:12 UTC 
(rev 12796)
@@ -459,16 +459,25 @@

     /**
      * Convert an array of bytes to an array of longs.
+     */
+    public static long[] bytesToLongs(byte[] buf) {
+       return bytesToLongs(buf, 0, buf.length);
+    }
+    
+    /**
+     * Convert an array of bytes to an array of longs.
      * @param buf
+     * @param length 
+     * @param offset 
      * @return
      */
-    public static long[] bytesToLongs(byte[] buf) {
-        if(buf.length % 8 != 0) throw new IllegalArgumentException();
-        long[] longs = new long[buf.length/8];
+    public static long[] bytesToLongs(byte[] buf, int offset, int length) {
+        if(length % 8 != 0) throw new IllegalArgumentException();
+        long[] longs = new long[length/8];
         for(int i=0;i<longs.length;i++) {
             long x = 0;
             for(int j=7;j>=0;j--) {
-                long y = (buf[i*8+j] & 0xff);
+                long y = (buf[offset+i*8+j] & 0xff);
                 x = (x << 8) + y;
             }
             longs[i] = x;
@@ -625,4 +634,12 @@
                return res;
        }

+       public static double[] bytesToDoubles(byte[] data, int offset, int 
length) {
+               long[] longs = bytesToLongs(data, offset, length);
+               double[] doubles = new double[longs.length];
+               for(int i=0;i<longs.length;i++)
+                       doubles[i] = Double.longBitsToDouble(longs[i]);
+               return doubles;
+       }
+
 }


Reply via email to