Author: toad
Date: 2007-04-18 19:45:16 +0000 (Wed, 18 Apr 2007)
New Revision: 12794

Modified:
   trunk/freenet/src/freenet/io/comm/DMT.java
   trunk/freenet/src/freenet/io/comm/Message.java
   trunk/freenet/src/freenet/node/LocationManager.java
   trunk/freenet/src/freenet/node/PeerManager.java
Log:
Basic implementation of including UIDs on swapcomplete/swapconfirm as a 
sub-message.

Modified: trunk/freenet/src/freenet/io/comm/DMT.java
===================================================================
--- trunk/freenet/src/freenet/io/comm/DMT.java  2007-04-18 19:23:54 UTC (rev 
12793)
+++ trunk/freenet/src/freenet/io/comm/DMT.java  2007-04-18 19:45:16 UTC (rev 
12794)
@@ -90,6 +90,7 @@
        public static final String NODE_TO_NODE_MESSAGE_TYPE = 
"nodeToNodeMessageType";
        public static final String NODE_TO_NODE_MESSAGE_TEXT = 
"nodeToNodeMessageText";
        public static final String NODE_TO_NODE_MESSAGE_DATA = 
"nodeToNodeMessageData";
+       public static final String NODE_UIDS = "nodeUIDs";

        //Diagnostic
        public static final MessageType ping = new MessageType("ping") {{
@@ -875,6 +876,18 @@
                return msg;
        }

+       // Secondary messages (debug messages attached to primary messages)
+       
+       public static final MessageType FNPSwapNodeUIDs = new 
MessageType("FNPSwapNodeUIDs") {{
+               addField(NODE_UIDS, ShortBuffer.class);
+       }};
+       
+       public static final Message createFNPSwapLocations(byte[] uids) {
+               Message msg = new Message(FNPSwapNodeUIDs);
+               msg.set(NODE_UIDS, new ShortBuffer(uids));
+               return msg;
+       }
+       
        public static void init() { }

 }

Modified: trunk/freenet/src/freenet/io/comm/Message.java
===================================================================
--- trunk/freenet/src/freenet/io/comm/Message.java      2007-04-18 19:23:54 UTC 
(rev 12793)
+++ trunk/freenet/src/freenet/io/comm/Message.java      2007-04-18 19:45:16 UTC 
(rev 12794)
@@ -286,7 +286,7 @@
                return _receivedByteCount;
        }

-       private void addSubMessage(Message subMessage) {
+       public void addSubMessage(Message subMessage) {
                if(_subMessages == null) _subMessages = new Vector();
                _subMessages.add(subMessage);
        }

Modified: trunk/freenet/src/freenet/node/LocationManager.java
===================================================================
--- trunk/freenet/src/freenet/node/LocationManager.java 2007-04-18 19:23:54 UTC 
(rev 12793)
+++ trunk/freenet/src/freenet/node/LocationManager.java 2007-04-18 19:45:16 UTC 
(rev 12794)
@@ -15,6 +15,7 @@
 import freenet.io.comm.Message;
 import freenet.io.comm.MessageFilter;
 import freenet.io.comm.NotConnectedException;
+import freenet.node.PeerManager.LocationUIDPair;
 import freenet.support.Fields;
 import freenet.support.Logger;
 import freenet.support.ShortBuffer;
@@ -246,7 +247,8 @@

             long random = r.nextLong();
             double myLoc = loc.getValue();
-            double[] friendLocs = node.peers.getPeerLocationDoubles();
+            LocationUIDPair[] friendLocsAndUIDs = 
node.peers.getPeerLocationsAndUIDs();
+            double[] friendLocs = extractLocs(friendLocsAndUIDs);
             long[] myValueLong = new long[1+1+friendLocs.length];
             myValueLong[0] = random;
             myValueLong[1] = Double.doubleToLongBits(myLoc);
@@ -324,6 +326,7 @@
             // Send our SwapComplete

             Message confirm = DMT.createFNPSwapComplete(uid, myValue);
+            
confirm.addSubMessage(DMT.createFNPSwapLocations(Fields.longsToBytes(extractUIDs(friendLocsAndUIDs))));

             node.usm.send(pn, confirm, null);

@@ -348,6 +351,7 @@
             removeRecentlyForwardedItem(item);
         }
         }
+
     }

     /**
@@ -369,7 +373,8 @@
                 // pretend that they're locked
                 long random = r.nextLong();
                 double myLoc = loc.getValue();
-                double[] friendLocs = node.peers.getPeerLocationDoubles();
+                LocationUIDPair[] friendLocsAndUIDs = 
node.peers.getPeerLocationsAndUIDs();
+                double[] friendLocs = extractLocs(friendLocsAndUIDs);
                 long[] myValueLong = new long[1+1+friendLocs.length];
                 myValueLong[0] = random;
                 myValueLong[1] = Double.doubleToLongBits(myLoc);
@@ -429,6 +434,7 @@
                 byte[] hisHash = 
((ShortBuffer)reply.getObject(DMT.HASH)).getData();

                 Message confirm = DMT.createFNPSwapCommit(uid, myValue);
+                
confirm.addSubMessage(DMT.createFNPSwapLocations(Fields.longsToBytes(extractUIDs(friendLocsAndUIDs))));

                 filter1.clearOr();
                 MessageFilter filter3 = 
MessageFilter.create().setField(DMT.UID, 
uid).setType(DMT.FNPSwapComplete).setTimeout(TIMEOUT).setSource(pn);
@@ -1059,4 +1065,18 @@
                return knownLocs.pairsAfter(timestamp, new 
Double[knownLocs.size()]);
        }
        }
+    
+       private static double[] extractLocs(LocationUIDPair[] pairs) {
+               double[] locs = new double[pairs.length];
+               for(int i=0;i<pairs.length;i++)
+                       locs[i] = pairs[i].location;
+               return locs;
+       }
+
+       private static long[] extractUIDs(LocationUIDPair[] pairs) {
+               long[] uids = new long[pairs.length];
+               for(int i=0;i<pairs.length;i++)
+                       uids[i] = pairs[i].uid;
+               return uids;
+       }
 }

Modified: trunk/freenet/src/freenet/node/PeerManager.java
===================================================================
--- trunk/freenet/src/freenet/node/PeerManager.java     2007-04-18 19:23:54 UTC 
(rev 12793)
+++ trunk/freenet/src/freenet/node/PeerManager.java     2007-04-18 19:45:16 UTC 
(rev 12794)
@@ -334,6 +334,24 @@
                writePeers();
     }

+    class LocationUIDPair implements Comparable {
+       double location;
+       long uid;
+       
+       LocationUIDPair(PeerNode pn) {
+               location = pn.getLocation().getValue();
+               uid = pn.swapIdentifier;
+       }
+
+               public int compareTo(Object arg0) {
+                       // Compare purely on location, so result is the same as 
getPeerLocationDoubles()
+                       LocationUIDPair p = (LocationUIDPair) arg0;
+                       if(p.location > location) return 1;
+                       if(p.location < location) return -1;
+                       return 0;
+               }
+    }
+    
     /**
      * @return An array of the current locations (as doubles) of all
      * our connected peers.
@@ -359,6 +377,29 @@
         } else return locs;
     }

+    /** Just like getPeerLocationDoubles, except it also
+     * returns the UID for each node. */
+    public LocationUIDPair[] getPeerLocationsAndUIDs() {
+        PeerNode[] conns;
+        LocationUIDPair[] locPairs;
+        synchronized (this) {
+                       conns = connectedPeers;
+               }
+        locPairs = new LocationUIDPair[conns.length];
+        int x = 0;
+        for(int i=0;i<conns.length;i++) {
+            if(conns[i].isRoutable())
+               locPairs[x++] = new LocationUIDPair(conns[i]);
+        }
+        // Sort it
+        Arrays.sort(locPairs);
+        if(x != locPairs.length) {
+            LocationUIDPair[] newLocs = new LocationUIDPair[x];
+            System.arraycopy(locPairs, 0, newLocs, 0, x);
+            return newLocs;
+        } else return locPairs;
+    }
+
     /**
      * @return A random connected peer.
      * FIXME: should this take performance into account?


Reply via email to