Author: toad
Date: 2006-03-25 20:11:04 +0000 (Sat, 25 Mar 2006)
New Revision: 8319

Modified:
   trunk/freenet/src/freenet/client/async/USKInserter.java
   trunk/freenet/src/freenet/client/async/USKManager.java
   trunk/freenet/src/freenet/clients/http/BookmarkManager.java
   trunk/freenet/src/freenet/keys/BaseClientKey.java
   trunk/freenet/src/freenet/keys/FreenetURI.java
   trunk/freenet/src/freenet/keys/InsertableUSK.java
   trunk/freenet/src/freenet/keys/USK.java
   trunk/freenet/src/freenet/node/fcp/FCPMessage.java
Log:
more USK stuff


Modified: trunk/freenet/src/freenet/client/async/USKInserter.java
===================================================================
--- trunk/freenet/src/freenet/client/async/USKInserter.java     2006-03-25 
19:54:32 UTC (rev 8318)
+++ trunk/freenet/src/freenet/client/async/USKInserter.java     2006-03-25 
20:11:04 UTC (rev 8319)
@@ -105,12 +105,14 @@
                        finished = true;
                        sbi = null;
                }
-               FreenetURI targetURI = pubUSK.copy(edition).getURI();
+               FreenetURI targetURI = pubUSK.getSSK(edition).getURI();
                FreenetURI realURI = ((SingleBlockInserter)state).getURI();
                if(!targetURI.equals(realURI))
                        Logger.error(this, "URI should be "+targetURI+" 
actually is "+realURI);
-               else
+               else {
                        Logger.minor(this, "URI should be "+targetURI+" 
actually is "+realURI);
+                       ctx.uskManager.update(pubUSK, edition);
+               }
                // FINISHED!!!! Yay!!!
        }

@@ -145,7 +147,7 @@
                        parent.addMustSucceedBlocks(1);
                        parent.notifyClients();
                }
-               privUSK = InsertableUSK.create(uri);
+               privUSK = InsertableUSK.createInsertable(uri);
                pubUSK = privUSK.getUSK();
                edition = pubUSK.suggestedEdition;
        }

Modified: trunk/freenet/src/freenet/client/async/USKManager.java
===================================================================
--- trunk/freenet/src/freenet/client/async/USKManager.java      2006-03-25 
19:54:32 UTC (rev 8318)
+++ trunk/freenet/src/freenet/client/async/USKManager.java      2006-03-25 
20:11:04 UTC (rev 8319)
@@ -131,7 +131,7 @@
                synchronized(this) {
                        Long l = (Long) latestVersionByClearUSK.get(clear);
                        Logger.minor(this, "Old value: "+l);
-                       if(!(l != null && l.longValue() > number)) {
+                       if(l == null || number > l.longValue()) {
                                l = new Long(number);
                                latestVersionByClearUSK.put(clear, l);
                                Logger.minor(this, "Put "+number);
@@ -152,13 +152,19 @@
         */
        public void subscribe(USK origUSK, USKCallback cb, boolean 
runBackgroundFetch) {
                USKFetcher sched = null;
+               long ed = origUSK.suggestedEdition;
+               long curEd;
                synchronized(this) {
+                       curEd = lookup(origUSK);
                        USK clear = origUSK.clearCopy();
                        USKCallback[] callbacks = (USKCallback[]) 
subscribersByClearUSK.get(clear);
                        if(callbacks == null)
                                callbacks = new USKCallback[1];
-                       else
-                               callbacks = new USKCallback[callbacks.length+1];
+                       else {
+                               USKCallback[] newCallbacks = new 
USKCallback[callbacks.length+1];
+                               System.arraycopy(callbacks, 0, newCallbacks, 0, 
callbacks.length);
+                               callbacks = newCallbacks;
+                       }
                        callbacks[callbacks.length-1] = cb;
                        subscribersByClearUSK.put(clear, callbacks);
                        if(runBackgroundFetch) {
@@ -171,6 +177,8 @@
                                f.addSubscriber(cb);
                        }
                }
+               if(curEd > ed)
+                       cb.onFoundEdition(curEd, origUSK.copy(curEd));
                if(sched != null)
                        sched.schedule();
        }

Modified: trunk/freenet/src/freenet/clients/http/BookmarkManager.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/BookmarkManager.java 2006-03-25 
19:54:32 UTC (rev 8318)
+++ trunk/freenet/src/freenet/clients/http/BookmarkManager.java 2006-03-25 
20:11:04 UTC (rev 8319)
@@ -56,7 +56,7 @@

                                try {
                                        FreenetURI furi = new 
FreenetURI(i.getKey());
-                                       USK usk = new USK(furi);
+                                       USK usk = USK.create(furi);

                                        if (usk.equals(key, false)) {
                                                i.setKey(key.getURI());
@@ -90,7 +90,7 @@
                        if (!i.getKeyType().equals("USK")) continue;

                        try {
-                               USK u = new USK(i.key);
+                               USK u = USK.create(i.key);
                                this.node.uskManager.unsubscribe(u, this.uskcb, 
true);
                        } catch (MalformedURLException mue) {

@@ -103,7 +103,7 @@
                this.bookmarks.add(b);
                if (b.getKeyType().equals("USK")) {
                        try {
-                               USK u = new USK(b.key);
+                               USK u = USK.create(b.key);
                                this.node.uskManager.subscribe(u, this.uskcb, 
true);
                        } catch (MalformedURLException mue) {

@@ -114,7 +114,7 @@
        public void removeBookmark(Bookmark b) {
                if (b.getKeyType().equals("USK")) {
                        try {
-                               USK u = new USK(b.key);
+                               USK u = USK.create(b.key);
                                this.node.uskManager.subscribe(u, this.uskcb, 
true);
                        } catch (MalformedURLException mue) {


Modified: trunk/freenet/src/freenet/keys/BaseClientKey.java
===================================================================
--- trunk/freenet/src/freenet/keys/BaseClientKey.java   2006-03-25 19:54:32 UTC 
(rev 8318)
+++ trunk/freenet/src/freenet/keys/BaseClientKey.java   2006-03-25 20:11:04 UTC 
(rev 8319)
@@ -17,7 +17,7 @@
                if(origURI.getKeyType().equals("KSK"))
                        return ClientKSK.create(origURI.getDocName());
                if(origURI.getKeyType().equals("USK"))
-                       return new USK(origURI);
+                       return USK.create(origURI);
                throw new UnsupportedOperationException("Unknown keytype from 
"+origURI);
        }


Modified: trunk/freenet/src/freenet/keys/FreenetURI.java
===================================================================
--- trunk/freenet/src/freenet/keys/FreenetURI.java      2006-03-25 19:54:32 UTC 
(rev 8318)
+++ trunk/freenet/src/freenet/keys/FreenetURI.java      2006-03-25 20:11:04 UTC 
(rev 8319)
@@ -6,6 +6,7 @@
 import java.io.DataOutputStream;
 import java.io.IOException;
 import java.net.MalformedURLException;
+import java.util.Arrays;
 import java.util.LinkedList;
 import java.util.StringTokenizer;
 import java.util.Vector;
@@ -58,11 +59,34 @@
  */
 public class FreenetURI {

-       private String keyType, docName;
-       private String[] metaStr;
-       private byte[] routingKey, cryptoKey, extra;
-       private long suggestedEdition; // for USKs
+       private final String keyType, docName;
+       private final String[] metaStr;
+       private final byte[] routingKey, cryptoKey, extra;
+       private final long suggestedEdition; // for USKs

+       public boolean equals(Object o) {
+               if(!(o instanceof FreenetURI))
+                       return false;
+               else {
+                       FreenetURI f = (FreenetURI)o;
+                       if(!keyType.equals(f.keyType)) return false;
+                       if(keyType.equals("USK")) {
+                               if(!(suggestedEdition == f.suggestedEdition)) 
return false;
+                       }
+                       if(docName == null ^ f.docName == null) return false;
+                       if(metaStr == null ^ f.metaStr == null) return false;
+                       if(routingKey == null ^ f.routingKey == null) return 
false;
+                       if(cryptoKey == null ^ f.cryptoKey == null) return 
false;
+                       if(extra == null ^ f.extra == null) return false;
+                       if(docName != null && !docName.equals(f.docName)) 
return false;
+                       if(metaStr != null && !Arrays.equals(metaStr, 
f.metaStr)) return false;
+                       if(routingKey != null && !Arrays.equals(routingKey, 
f.routingKey)) return false;
+                       if(cryptoKey != null && !Arrays.equals(cryptoKey, 
f.cryptoKey)) return false;
+                       if(extra != null && !Arrays.equals(extra, f.extra)) 
return false;
+                       return true;
+               }
+       }
+
        public Object clone() {
                return new FreenetURI(this);
        }
@@ -76,15 +100,22 @@
                if(uri.routingKey != null) {
                        routingKey = new byte[uri.routingKey.length];
                        System.arraycopy(uri.routingKey, 0, routingKey, 0, 
routingKey.length);
+               } else {
+                       routingKey = null;
                }
                if(uri.cryptoKey != null) {
                        cryptoKey = new byte[uri.cryptoKey.length];
                        System.arraycopy(uri.cryptoKey, 0, cryptoKey, 0, 
cryptoKey.length);
+               } else {
+                       cryptoKey = null;
                }
                if(uri.extra != null) {
                        extra = new byte[uri.extra.length];
                        System.arraycopy(uri.extra, 0, extra, 0, extra.length);
+               } else {
+                       extra = null;
                }
+               this.suggestedEdition = uri.suggestedEdition;
        }

        public FreenetURI(String keyType, String docName) {
@@ -129,6 +160,7 @@
                this.routingKey = routingKey;
                this.cryptoKey = cryptoKey;
                this.extra = extra2;
+               this.suggestedEdition = -1;
        }

        public FreenetURI(
@@ -178,7 +210,6 @@
                }
                boolean b = false;
                if("SSK".equals(keyType) || (b="USK".equals(keyType))) {
-                       // docName not necessary, nor is it supported, for CHKs.

                        if(sv.isEmpty())
                                throw new MalformedURLException("No docname");
@@ -192,27 +223,42 @@
                                        e1.initCause(e);
                                        throw e1;
                                }
-                       }
+                       } else
+                               suggestedEdition = -1;
+               } else if(keyType.equalsIgnoreCase("KSK")) {
+                       docName = URI;
+                       metaStr = null;
+                       routingKey = null;
+                       cryptoKey = null;
+                       extra = null;
+                       suggestedEdition = -1;
+                       return;
+               } else {
+                       // docName not necessary, nor is it supported, for CHKs.
+                       docName = null;
+                       suggestedEdition = -1;
                }

                if (!sv.isEmpty()) {
                        metaStr = new String[sv.size()];
                        for (int i = 0; i < metaStr.length; i++)
                                metaStr[i] = (String) 
sv.elementAt(metaStr.length - 1 - i);
+               } else {
+                       metaStr = null;
                }

-               if(keyType.equalsIgnoreCase("KSK")) {
-                       docName = URI;
-                       return;
-               }

                // URI now contains: routingKey[,cryptoKey][,metaInfo]
                StringTokenizer st = new StringTokenizer(URI, ",");
                try {
                        if (st.hasMoreTokens()) {
                                routingKey = Base64.decode(st.nextToken());
+                       } else {
+                               routingKey = cryptoKey = extra = null;
+                               return;
                        }
                        if (!st.hasMoreTokens()) {
+                               cryptoKey = extra = null;
                                return;
                        }

@@ -220,6 +266,7 @@
                        String t = st.nextToken();
                        cryptoKey = Base64.decode(t);
                        if (!st.hasMoreTokens()) {
+                               extra = null;
                                return;
                        }
                        extra = Base64.decode(st.nextToken());
@@ -237,6 +284,7 @@
                this.extra = extra;
                this.docName = siteName;
                this.suggestedEdition = suggestedEdition2;
+               metaStr = null;
        }

        public void decompose() {

Modified: trunk/freenet/src/freenet/keys/InsertableUSK.java
===================================================================
--- trunk/freenet/src/freenet/keys/InsertableUSK.java   2006-03-25 19:54:32 UTC 
(rev 8318)
+++ trunk/freenet/src/freenet/keys/InsertableUSK.java   2006-03-25 20:11:04 UTC 
(rev 8319)
@@ -26,11 +26,13 @@
        public final DSAPrivateKey privKey;
        public final DSAGroup group;

-       public static InsertableUSK create(FreenetURI uri) throws 
MalformedURLException {
+       public static InsertableUSK createInsertable(FreenetURI uri) throws 
MalformedURLException {
                if(!uri.getKeyType().equalsIgnoreCase("USK"))
                        throw new MalformedURLException();
                if(uri.getDocName() == null || uri.getDocName().length() == 0)
                        throw new MalformedURLException("USK URIs must have a 
document name (to avoid ambiguity)");
+               if(uri.getExtra() != null)
+                       throw new MalformedURLException("Insertable SSK URIs 
must NOT have ,extra - inserting from a pubkey rather than the privkey 
perhaps?");
                DSAGroup g = Global.DSAgroupBigA;
                DSAPrivateKey privKey = new DSAPrivateKey(new 
NativeBigInteger(1, uri.getKeyVal()));
                DSAPublicKey pubKey = new DSAPublicKey(g, privKey);

Modified: trunk/freenet/src/freenet/keys/USK.java
===================================================================
--- trunk/freenet/src/freenet/keys/USK.java     2006-03-25 19:54:32 UTC (rev 
8318)
+++ trunk/freenet/src/freenet/keys/USK.java     2006-03-25 20:11:04 UTC (rev 
8319)
@@ -47,11 +47,12 @@
                hashCode = Fields.hashCode(pubKeyHash) ^ 
Fields.hashCode(cryptoKey) ^
                        siteName.hashCode() ^ (int)suggestedEdition ^ 
(int)(suggestedEdition >> 32);
        }
+
+       public static USK create(FreenetURI uri) throws MalformedURLException {
+               if(!uri.getKeyType().equals("USK")) throw new 
MalformedURLException("Not a USK");
+               return new USK(uri.getRoutingKey(), uri.getCryptoKey(), 
uri.getExtra(), uri.getDocName(), uri.getSuggestedEdition());
+       }

-       public USK(FreenetURI uri) throws MalformedURLException {
-               this(uri.getRoutingKey(), uri.getCryptoKey(), uri.getExtra(), 
uri.getDocName(), uri.getSuggestedEdition());
-       }
-
        protected USK(byte[] pubKeyHash2, byte[] cryptoKey2, String siteName2, 
long suggestedEdition2) {
                this.pubKeyHash = pubKeyHash2;
                this.cryptoKey = cryptoKey2;

Modified: trunk/freenet/src/freenet/node/fcp/FCPMessage.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/FCPMessage.java  2006-03-25 19:54:32 UTC 
(rev 8318)
+++ trunk/freenet/src/freenet/node/fcp/FCPMessage.java  2006-03-25 20:11:04 UTC 
(rev 8319)
@@ -50,6 +50,8 @@
                        return new ClientPutDiskDirMessage(fs);
                if(name.equals(ClientPutComplexDirMessage.name))
                        return new ClientPutComplexDirMessage(fs, bfTemp, 
bfPersistent);
+               if(name.equals(SubscribeUSKMessage.name))
+                       return new SubscribeUSKMessage(fs);
                if(name.equals("Void"))
                        return null;
                throw new 
MessageInvalidException(ProtocolErrorMessage.INVALID_MESSAGE, "Unknown message 
name "+name, null);


Reply via email to