Author: toad
Date: 2006-05-13 17:14:10 +0000 (Sat, 13 May 2006)
New Revision: 8692

Added:
   trunk/freenet/src/freenet/node/ARKFetcher.java
Modified:
   trunk/freenet/src/freenet/keys/USK.java
   trunk/freenet/src/freenet/node/PeerNode.java
   trunk/freenet/src/freenet/node/Version.java
Log:
706: Fix an NPE. Non-working beginnings of ARK fetch support.

Modified: trunk/freenet/src/freenet/keys/USK.java
===================================================================
--- trunk/freenet/src/freenet/keys/USK.java     2006-05-13 14:14:58 UTC (rev 
8691)
+++ trunk/freenet/src/freenet/keys/USK.java     2006-05-13 17:14:10 UTC (rev 
8692)
@@ -59,9 +59,18 @@
                this.siteName = siteName2;
                this.suggestedEdition = suggestedEdition2;
                hashCode = Fields.hashCode(pubKeyHash) ^ 
Fields.hashCode(cryptoKey) ^
-               siteName.hashCode() ^ (int)suggestedEdition ^ 
(int)(suggestedEdition >> 32);
+                       siteName.hashCode() ^ (int)suggestedEdition ^ 
(int)(suggestedEdition >> 32);
        }

+       public USK(ClientSSK ssk, long myARKNumber) {
+               this.pubKeyHash = ssk.pubKeyHash;
+               this.cryptoKey = ssk.cryptoKey;
+               this.siteName = ssk.docName;
+               this.suggestedEdition = myARKNumber;
+               hashCode = Fields.hashCode(pubKeyHash) ^ 
Fields.hashCode(cryptoKey) ^
+                       siteName.hashCode() ^ (int)suggestedEdition ^ 
(int)(suggestedEdition >> 32);
+       }
+
        public FreenetURI getURI() {
                return new FreenetURI(pubKeyHash, cryptoKey, 
ClientSSK.getExtraBytes(), siteName, suggestedEdition);
        }
@@ -103,5 +112,9 @@
        public int hashCode() {
                return hashCode;
        }
+
+       public FreenetURI getBaseSSK() {
+               return new FreenetURI("SSK", siteName, pubKeyHash, cryptoKey, 
ClientSSK.getExtraBytes());
+       }

 }

Added: trunk/freenet/src/freenet/node/ARKFetcher.java
===================================================================
--- trunk/freenet/src/freenet/node/ARKFetcher.java      2006-05-13 14:14:58 UTC 
(rev 8691)
+++ trunk/freenet/src/freenet/node/ARKFetcher.java      2006-05-13 17:14:10 UTC 
(rev 8692)
@@ -0,0 +1,36 @@
+package freenet.node;
+
+/**
+ * Fetch an ARK. Permanent, tied to a PeerNode, stops itself after a 
successful fetch.
+ */
+public class ARKFetcher {
+
+       final PeerNode peer;
+       final Node node;
+
+       public ARKFetcher(PeerNode peer, Node node) {
+               this.peer = peer;
+               this.node = node;
+       }
+
+       /**
+        * Called when the node starts / is added, and also when we fail to 
connect twice 
+        * after a new reference. (So we get one from the ARK, we wait for the 
current
+        * connect attempt to fail, we start another one, that fails, we start 
another one,
+        * that also fails, so we try the fetch again to see if we can find 
something more
+        * recent).
+        */
+       public void start() {
+               // Start fetch
+               // FIXME
+       }
+       
+       /**
+        * Called when the node connects successfully.
+        */
+       public void stop() {
+               // Stop fetch
+               // FIXME
+       }
+       
+}

Modified: trunk/freenet/src/freenet/node/PeerNode.java
===================================================================
--- trunk/freenet/src/freenet/node/PeerNode.java        2006-05-13 14:14:58 UTC 
(rev 8691)
+++ trunk/freenet/src/freenet/node/PeerNode.java        2006-05-13 17:14:10 UTC 
(rev 8692)
@@ -7,15 +7,16 @@
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
 import java.net.InetAddress;
+import java.net.MalformedURLException;
 import java.net.UnknownHostException;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.Arrays;
+import java.util.Hashtable;
 import java.util.LinkedList;
+import java.util.Vector;
 import java.util.zip.DataFormatException;
 import java.util.zip.Inflater;
-import java.util.Vector;
-import java.util.Hashtable;

 import freenet.crypt.BlockCipher;
 import freenet.crypt.DiffieHellmanContext;
@@ -23,12 +24,16 @@
 import freenet.crypt.ciphers.Rijndael;
 import freenet.io.comm.DMT;
 import freenet.io.comm.DisconnectedException;
+import freenet.io.comm.FreenetInetAddress;
 import freenet.io.comm.Message;
 import freenet.io.comm.MessageFilter;
 import freenet.io.comm.NotConnectedException;
 import freenet.io.comm.Peer;
 import freenet.io.comm.PeerContext;
 import freenet.io.comm.PeerParseException;
+import freenet.keys.ClientSSK;
+import freenet.keys.FreenetURI;
+import freenet.keys.USK;
 import freenet.support.Base64;
 import freenet.support.Fields;
 import freenet.support.HexUtil;
@@ -37,7 +42,6 @@
 import freenet.support.Logger;
 import freenet.support.SimpleFieldSet;
 import freenet.support.WouldBlockException;
-import freenet.support.math.BootstrappingDecayingRunningAverage;
 import freenet.support.math.RunningAverage;
 import freenet.support.math.SimpleRunningAverage;
 import freenet.support.math.TimeDecayingRunningAverage;
@@ -101,7 +105,17 @@
      */
     private boolean isConnected;

+    /**
+     * ARK fetcher.
+     */
+    private final ARKFetcher arkFetcher;

+    /** My ARK SSK public key */
+    private USK myARK;
+    
+    /** My ARK sequence number */
+    private long myARKNumber;
+    
     /** Current location in the keyspace */
     private Location currentLocation;

@@ -320,7 +334,15 @@
         // So go for a filter.
         pingAverage = 
                new TimeDecayingRunningAverage(1, 60000 /* should be 
significantly longer than a typical transfer */, 0, Long.MAX_VALUE);
+

+        
+        // ARK stuff.
+
+        parseARK(fs);
+        
+        arkFetcher = new ARKFetcher(this, node);
+        
         // Now for the metadata.
         // The metadata sub-fieldset contains data about the node which is not 
part of the node reference.
         // It belongs to this node, not to the node being described.
@@ -350,7 +372,39 @@

     }

-    private void randomizeMaxTimeBetweenPacketSends() {
+    private boolean parseARK(SimpleFieldSet fs) {
+        USK ark = null;
+        long arkNo = 0;
+        try {
+               String arkNumber = fs.get("ark.number");
+               
+               if(arkNumber != null) {
+                       arkNo = Long.parseLong(arkNumber);
+               }
+               
+               String arkPubKey = fs.get("ark.pubURI");
+               if(arkPubKey != null) {
+                       FreenetURI uri = new FreenetURI(arkPubKey);
+                       ClientSSK ssk = new ClientSSK(uri);
+                       ark = new USK(ssk, myARKNumber);
+               }
+        } catch (MalformedURLException e) {
+               Logger.error(this, "Couldn't parse ARK info for "+this+": "+e, 
e);
+        } catch (NumberFormatException e) {
+               Logger.error(this, "Couldn't parse ARK info for "+this+": "+e, 
e);
+        }
+
+        if(ark != null) {
+               if(myARKNumber != arkNo || myARK != ark) {
+                       myARKNumber = arkNo;
+                       myARK = ark;
+                       return true;
+               }
+        }
+        return false;
+       }
+
+       private void randomizeMaxTimeBetweenPacketSends() {
         int x = Node.KEEPALIVE_INTERVAL;
         x += node.random.nextInt(x);
     }
@@ -388,7 +442,8 @@
        }
        // Hack for two nodes on the same IP that can't talk over inet for 
routing reasons
        InetAddress localhost = node.localhostAddress;
-       InetAddress nodeIP = node.getPrimaryIPAddress().getAddress();
+       FreenetInetAddress nodeAddr = node.getPrimaryIPAddress();
+       InetAddress nodeIP = nodeAddr == null ? null : nodeAddr.getAddress();
        if(nodeIP != null && nodeIP.equals(localhost)) return p;
        if(peerIP != null && peerIP.equals(localhost)) return p;
        if(nodeIP != null && nodeIP.equals(peerIP)) {
@@ -995,6 +1050,8 @@
         }
         String name = fs.get("myName");
         if(name == null) throw new FSParseException("No name");
+        if(parseARK(fs))
+               changedAnything = true;
         if(!name.equals(myName)) changedAnything = true;
         myName = name;
         if(changedAnything) node.peers.writePeers();
@@ -1097,6 +1154,10 @@
         fs.put("testnet", Boolean.toString(testnetEnabled));
         fs.put("version", version);
         fs.put("myName", myName);
+        if(myARK != null) {
+               fs.put("ark.number", Long.toString(this.myARKNumber));
+               fs.put("ark.pubURI", myARK.getBaseSSK().toString(false));
+        }
         return fs;
     }


Modified: trunk/freenet/src/freenet/node/Version.java
===================================================================
--- trunk/freenet/src/freenet/node/Version.java 2006-05-13 14:14:58 UTC (rev 
8691)
+++ trunk/freenet/src/freenet/node/Version.java 2006-05-13 17:14:10 UTC (rev 
8692)
@@ -18,7 +18,7 @@
        public static final String protocolVersion = "1.0";

        /** The build number of the current revision */
-       private static final int buildNumber = 705;
+       private static final int buildNumber = 706;

        /** Oldest build of Fred we will talk to */
        private static final int lastGoodBuild = 591;


Reply via email to