Author: nextgens
Date: 2008-07-17 12:03:12 +0000 (Thu, 17 Jul 2008)
New Revision: 21160

Modified:
   trunk/freenet/src/freenet/clients/http/ConnectionsToadlet.java
   trunk/freenet/src/freenet/io/comm/DMT.java
   trunk/freenet/src/freenet/node/LocationManager.java
   trunk/freenet/src/freenet/node/NodeDispatcher.java
   trunk/freenet/src/freenet/node/PeerNode.java
   trunk/freenet/src/freenet/node/PeerNodeStatus.java
Log:
First part of the FOAF routing changes: publish the location of our peers to 
our peers.
Next step is to tweak the routing algorithm according to them

This is the naive implementation: we send every neighbor the same message, 
including its location... and we do it everytime something changes. We might 
need to rate-limit and to send differential messages.

Modified: trunk/freenet/src/freenet/clients/http/ConnectionsToadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/ConnectionsToadlet.java      
2008-07-17 11:50:40 UTC (rev 21159)
+++ trunk/freenet/src/freenet/clients/http/ConnectionsToadlet.java      
2008-07-17 12:03:12 UTC (rev 21160)
@@ -724,7 +724,14 @@

                // location column
                if (advancedModeEnabled) {
-                       peerRow.addChild("td", "class", "peer-location", 
String.valueOf(peerNodeStatus.getLocation()));
+                       HTMLNode locationNode = peerRow.addChild("td", "class", 
"peer-location");
+                       locationNode.addChild("b", 
String.valueOf(peerNodeStatus.getLocation()));
+                       locationNode.addChild("br");
+                       double[] peersLoc = peerNodeStatus.getPeersLocation();
+                       if(peersLoc != null) {
+                               for(double loc : peersLoc)
+                                       locationNode.addChild("i", 
String.valueOf(loc)).addChild("br");
+                       }
                }

                if (advancedModeEnabled) {

Modified: trunk/freenet/src/freenet/io/comm/DMT.java
===================================================================
--- trunk/freenet/src/freenet/io/comm/DMT.java  2008-07-17 11:50:40 UTC (rev 
21159)
+++ trunk/freenet/src/freenet/io/comm/DMT.java  2008-07-17 12:03:12 UTC (rev 
21160)
@@ -29,6 +29,7 @@
 import freenet.support.Buffer;
 import freenet.support.Fields;
 import freenet.support.ShortBuffer;
+import java.nio.DoubleBuffer;


 /**
@@ -1228,15 +1229,31 @@
                return msg;
        }

+       @Deprecated
        public static final MessageType FNPLocChangeNotification = new 
MessageType("FNPLocationChangeNotification", PRIORITY_LOW) {{
                addField(LOCATION, Double.class);
        }};

+       @Deprecated
        public static final Message createFNPLocChangeNotification(double 
newLoc) {
                Message msg = new Message(FNPLocChangeNotification);
                msg.set(LOCATION, newLoc);
                return msg;
        }
+               
+       public static final MessageType FNPLocChangeNotificationNew = new 
MessageType("FNPLocationChangeNotification2", PRIORITY_LOW) {{
+               addField(LOCATION, Double.class);
+               addField(PEER_LOCATIONS, ShortBuffer.class);
+       }};
+       
+       public static final Message createFNPLocChangeNotificationNew(double 
myLocation, double[] locations) {
+               Message msg = new Message(FNPLocChangeNotificationNew);
+               ShortBuffer dst = new 
ShortBuffer(Fields.doublesToBytes(locations));
+               msg.set(LOCATION, myLocation);
+               msg.set(PEER_LOCATIONS, dst);
+               
+               return msg;
+       }

        public static final MessageType FNPRoutedPing = new 
MessageType("FNPRoutedPing", PRIORITY_LOW) {{
                addRoutedToNodeMessageFields();
@@ -1571,7 +1588,7 @@

                return msg;
        }
-
+       
        public static void init() { }

 }

Modified: trunk/freenet/src/freenet/node/LocationManager.java
===================================================================
--- trunk/freenet/src/freenet/node/LocationManager.java 2008-07-17 11:50:40 UTC 
(rev 21159)
+++ trunk/freenet/src/freenet/node/LocationManager.java 2008-07-17 12:03:12 UTC 
(rev 21160)
@@ -9,8 +9,6 @@
 import java.security.MessageDigest;
 import java.text.DateFormat;
 import java.util.Date;
-import java.util.Enumeration;
-import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -113,7 +111,7 @@
     public synchronized double getLocation() {
         return loc;
     }
-
+    
     /**
      * @param l
      */
@@ -202,7 +200,7 @@
                                 }
                                 if(myFlag) {
                                     setLocation(node.random.nextDouble());
-                                    announceLocChange(true, true);
+                                    announceLocChange(true, true, true);
                                     node.writeNodeFile();
                                 }
                             } finally {
@@ -384,7 +382,7 @@
                 setLocation(hisLoc);
                 if(logMINOR) Logger.minor(this, "Swapped: "+myLoc+" <-> 
"+hisLoc+" - "+uid);
                 swaps++;
-                announceLocChange(false, false);
+                announceLocChange(true, false, false);
                 node.writeNodeFile();
             } else {
                if(logMINOR) Logger.minor(this, "Didn't swap: "+myLoc+" <-> 
"+hisLoc+" - "+uid);
@@ -396,7 +394,7 @@
             // Randomise our location every 2*SWAP_RESET swap attempts, 
whichever way it went.
             if(node.random.nextInt(SWAP_RESET) == 0) {
                 setLocation(node.random.nextDouble());
-                announceLocChange(true, false);
+                announceLocChange(true, true, false);
                 node.writeNodeFile();
             }

@@ -578,7 +576,7 @@
                     setLocation(hisLoc);
                     if(logMINOR) Logger.minor(this, "Swapped: "+myLoc+" <-> 
"+hisLoc+" - "+uid);
                     swaps++;
-                    announceLocChange(false, false);
+                    announceLocChange(true, false, false);
                     node.writeNodeFile();
                 } else {
                        if(logMINOR) Logger.minor(this, "Didn't swap: "+myLoc+" 
<-> "+hisLoc+" - "+uid);
@@ -590,7 +588,7 @@
                 // Randomise our location every 2*SWAP_RESET swap attempts, 
whichever way it went.
                 if(node.random.nextInt(SWAP_RESET) == 0) {
                     setLocation(node.random.nextDouble());
-                    announceLocChange(true, false);
+                    announceLocChange(true, true, false);
                     node.writeNodeFile();
                 }

@@ -608,10 +606,15 @@
     /**
      * Tell all connected peers that our location has changed
      */
-    private void announceLocChange(boolean randomReset, boolean 
fromDupLocation) {
-        Message msg = DMT.createFNPLocChangeNotification(getLocation());
+    protected void announceLocChange() {
+           announceLocChange(false, false, false);
+    }
+    
+    private void announceLocChange(boolean log, boolean randomReset, boolean 
fromDupLocation) {
+        Message msg = DMT.createFNPLocChangeNotificationNew(getLocation(), 
node.peers.getPeerLocationDoubles());
         node.peers.localBroadcast(msg, false, true, this);
-        recordLocChange(randomReset, fromDupLocation);
+       if(log)
+               recordLocChange(randomReset, fromDupLocation);
     }

     private void recordLocChange(final boolean randomReset, final boolean 
fromDupLocation) {

Modified: trunk/freenet/src/freenet/node/NodeDispatcher.java
===================================================================
--- trunk/freenet/src/freenet/node/NodeDispatcher.java  2008-07-17 11:50:40 UTC 
(rev 21159)
+++ trunk/freenet/src/freenet/node/NodeDispatcher.java  2008-07-17 12:03:12 UTC 
(rev 21160)
@@ -155,7 +155,17 @@
                } else if(source.isRealConnection() && spec == 
DMT.FNPLocChangeNotification) {
                        double newLoc = m.getDouble(DMT.LOCATION);
                        source.updateLocation(newLoc);
+                       // TODO: remove dead code when 
FNPLocChangeNotificationNew is mandatory
+                       if(source.getVersionNumber() > 1153)
+                               Logger.error(this, "We received a 
FNPLocChangeNotification from a recent build: that should *not* happen! 
("+source.toString()+')');
                        return true;
+               } else if(source.isRealConnection() && spec == 
DMT.FNPLocChangeNotificationNew) {
+                       double newLoc = m.getDouble(DMT.LOCATION);
+                       ShortBuffer buffer = ((ShortBuffer) 
m.getObject(DMT.PEER_LOCATIONS));
+                       double[] locs = Fields.bytesToDoubles(buffer.getData());
+                       source.updateLocation(newLoc, locs);
+                       
+                       return true;
                }

                if(!source.isRoutable()) return false;

Modified: trunk/freenet/src/freenet/node/PeerNode.java
===================================================================
--- trunk/freenet/src/freenet/node/PeerNode.java        2008-07-17 11:50:40 UTC 
(rev 21159)
+++ trunk/freenet/src/freenet/node/PeerNode.java        2008-07-17 12:03:12 UTC 
(rev 21160)
@@ -74,6 +74,9 @@
 import freenet.support.math.TimeDecayingRunningAverage;
 import freenet.support.transport.ip.HostnameSyntaxException;
 import freenet.support.transport.ip.IPUtil;
+import java.util.ArrayList;
+import java.util.SortedSet;
+import java.util.TreeSet;

 /**
  * @author amphibian
@@ -169,6 +172,8 @@
        private static final int MAX_HANDSHAKE_COUNT = 2;
        /** Current location in the keyspace, or -1 if it is unknown */
        private double currentLocation;
+       /** Current locations of our peer's peers */
+       private double[] currentPeersLocation;
        /** Time the location was set */
        private long locSetTime;
        /** Node identity; for now a block of data, in future a
@@ -373,8 +378,15 @@
                        throw new FSParseException("Invalid version "+version+" 
: "+e2);
                }
                String locationString = fs.get("location");
+               String[] peerLocationsString = fs.getAll("peersLocation");
                try {
                        currentLocation = Location.getLocation(locationString);
+                       if(peerLocationsString != null) {
+                               double[] peerLocations = new 
double[peerLocationsString.length];
+                               for(int i = 0; i < peerLocationsString.length; 
i++)
+                                       peerLocations[i] = 
Location.getLocation(peerLocationsString[i]);
+                               currentPeersLocation = peerLocations;
+                       }
                        locSetTime = System.currentTimeMillis();
                } catch(FSParseException e) {
                        // Wait for them to send us an FNPLocChangeNotification
@@ -901,6 +913,10 @@
        public synchronized double getLocation() {
                return currentLocation;
        }
+       
+       public synchronized  double[] getPeersLocation() {
+               return currentPeersLocation;
+       }

        public synchronized long getLocSetTime() {
                return locSetTime;
@@ -1569,11 +1585,12 @@

        /**
        * Update the Location to a new value.
+       * @deprecated
        */
        public void updateLocation(double newLoc) {
                logMINOR = Logger.shouldLog(Logger.MINOR, PeerNode.class);
                if(newLoc < 0.0 || newLoc > 1.0) {
-                       Logger.error(this, "Invalid location update for " + 
this, new Exception("error"));
+                       Logger.error(this, "Invalid location update for " + 
this+ " ("+newLoc+')', new Exception("error"));
                        // Ignore it
                        return;
                }
@@ -1583,7 +1600,35 @@
                }
                node.peers.writePeers();
        }
+       
+       public void updateLocation(double newLoc, double[] newLocs) {
+               logMINOR = Logger.shouldLog(Logger.MINOR, PeerNode.class);
+               
+               if(newLoc < 0.0 || newLoc > 1.0) {
+                       Logger.error(this, "Invalid location update for " + 
this+ " ("+newLoc+')', new Exception("error"));
+                       // Ignore it
+                       return;
+               }
+               
+               for(double currentLoc : newLocs) {
+                       if(currentLoc < 0.0 || currentLoc > 1.0) {
+                               Logger.error(this, "Invalid location update for 
" + this + " ("+currentLoc+')', new Exception("error"));
+                               // Ignore it
+                               return;
+                       }
+               }

+               Arrays.sort(newLocs);
+               
+               synchronized(this) {
+                       currentLocation = newLoc;
+                       currentPeersLocation = newLocs;
+                       locSetTime = System.currentTimeMillis();
+               }
+               node.peers.writePeers();
+               node.lm.announceLocChange();
+       }
+
        /**
        * Should we reject a swap request?
        */
@@ -2036,7 +2081,9 @@
        * Send any high level messages that need to be sent on connect.
        */
        protected void sendInitialMessages() {
-               Message locMsg = 
DMT.createFNPLocChangeNotification(node.lm.getLocation());
+               Message locMsg = ((getVersionNumber() > 1153) ?
+                       
DMT.createFNPLocChangeNotificationNew(node.lm.getLocation(), 
node.peers.getPeerLocationDoubles()) :
+                       
DMT.createFNPLocChangeNotification(node.lm.getLocation()));
                Message ipMsg = DMT.createFNPDetectedIPAddress(detectedPeer);
                Message timeMsg = DMT.createFNPTime(System.currentTimeMillis());
                Message packetsMsg = createSentPacketsMessage();
@@ -2522,6 +2569,8 @@
                        fs.putSingle("hadRoutableConnectionCount", 
Long.toString(hadRoutableConnectionCount));
                if(routableConnectionCheckCount > 0)
                        fs.putSingle("routableConnectionCheckCount", 
Long.toString(routableConnectionCheckCount));
+               if(currentPeersLocation != null)
+                       fs.put("peersLocation", currentPeersLocation);
                return fs;
        }


Modified: trunk/freenet/src/freenet/node/PeerNodeStatus.java
===================================================================
--- trunk/freenet/src/freenet/node/PeerNodeStatus.java  2008-07-17 11:50:40 UTC 
(rev 21159)
+++ trunk/freenet/src/freenet/node/PeerNodeStatus.java  2008-07-17 12:03:12 UTC 
(rev 21160)
@@ -30,6 +30,7 @@
        private final String statusCSSName;

        private final double location;
+       private final double[] peersLocation;

        private final String version;

@@ -109,6 +110,7 @@
                this.statusName = peerNode.getPeerNodeStatusString();
                this.statusCSSName = peerNode.getPeerNodeStatusCSSClassName();
                this.location = peerNode.getLocation();
+               this.peersLocation = peerNode.getPeersLocation();
                this.version = peerNode.getVersion();
                this.simpleVersion = peerNode.getSimpleVersion();
                this.routingBackoffLength = peerNode.getRoutingBackoffLength();
@@ -248,6 +250,10 @@
                return location;
        }

+       public double[] getPeersLocation() {
+               return peersLocation;
+       }
+       
        /**
         * @return the peerAddress
         */


Reply via email to