Author: dbkr
Date: 2006-03-24 19:03:57 +0000 (Fri, 24 Mar 2006)
New Revision: 8302

Modified:
   trunk/freenet/src/freenet/clients/http/WelcomeToadlet.java
   trunk/freenet/src/freenet/keys/USK.java
   trunk/freenet/src/freenet/node/Version.java
Log:
565: USK Bookmark subscription and updating basically working, athough beware 
sharp edges in this revision. Also fix bug #149 by moving the fetch-a-key from 
up.


Modified: trunk/freenet/src/freenet/clients/http/WelcomeToadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/WelcomeToadlet.java  2006-03-24 
18:38:39 UTC (rev 8301)
+++ trunk/freenet/src/freenet/clients/http/WelcomeToadlet.java  2006-03-24 
19:03:57 UTC (rev 8302)
@@ -3,21 +3,26 @@
 import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.net.MalformedURLException;
 import java.util.Vector;
 import java.util.Enumeration;

 import freenet.client.HighLevelSimpleClient;
+import freenet.client.async.USKCallback;
 import freenet.node.Node;
 import freenet.node.Version;
 import freenet.config.Option;
 import freenet.config.SubConfig;
 import freenet.config.StringArrCallback;
 import freenet.config.StringArrOption;
+import freenet.config.InvalidConfigValueException;
 import freenet.pluginmanager.HTTPRequest;
 import freenet.support.Bucket;
 import freenet.support.BucketTools;
 import freenet.support.Logger;
 import freenet.support.HTMLEncoder;
+import freenet.keys.USK;
+import freenet.keys.FreenetURI;

 public class WelcomeToadlet extends Toadlet {
        private static final String[] DEFAULT_BOOKMARKS = {
@@ -28,30 +33,38 @@
        Vector bookmarks = new Vector();

        private class Bookmark {
-               private final String key;
-               private final String desc;
+               private FreenetURI key;
+               private String desc;

-               Bookmark(String k, String d) {
-                       this.key = k;
+               Bookmark(String k, String d) throws MalformedURLException {
+                       this.key = new FreenetURI(k);
                        this.desc = d;
                }

-               Bookmark(String from) {
+               Bookmark(String from) throws MalformedURLException {
                        int eqpos = from.indexOf("=");

                        if (eqpos < 0) {
-                               this.key = from;
+                               this.key = new FreenetURI(from);
                                this.desc = from;
                        } else {
-                               this.key = from.substring(0, eqpos);
+                               this.key = new FreenetURI(from.substring(0, 
eqpos));
                                this.desc = from.substring(eqpos + 1);
                        }
                }

                public String getKey() {
-                       return key;
+                       return key.toString();
                }

+               public void setKey(FreenetURI uri) {
+                       key = uri;
+               }
+               
+               public String getKeyType() {
+                       return key.getKeyType();
+               }
+               
                public String getDesc() {
                        if (desc == "") {
                                return "Unnamed Bookmark";
@@ -61,7 +74,7 @@
                }

                public String toString() {
-                       return this.key + "=" + this.desc;
+                       return this.key.toString() + "=" + this.desc;
                }
        }

@@ -78,14 +91,55 @@
                        return buf.substring(0, buf.length() - 1);
                }

-               public void set(String newval) {
+               public void set(String newval) throws 
InvalidConfigValueException {
                        String[] newvals = 
newval.split(StringArrOption.delimiter);
                        bookmarks.clear();
                        for (int i = 0; i < newvals.length; i++) {
-                               bookmarks.add(new Bookmark(newvals[i]));
+                               try {
+                                       bookmarks.add(new Bookmark(newvals[i]));
+                               } catch (MalformedURLException mue) {
+                                       throw new 
InvalidConfigValueException(mue.getMessage());
+                               }
                        }
                }
        }
+       
+       private class USKUpdatedCallback implements USKCallback {
+               public void onFoundEdition(long edition, USK key) {
+                       
+                       for (Enumeration e = bookmarks.elements(); 
e.hasMoreElements(); ) {
+                               Bookmark i = (Bookmark) e.nextElement();
+                               
+                               if (!i.getKeyType().equals("USK")) continue;
+                               
+                               try {
+                                       FreenetURI furi = new 
FreenetURI(i.getKey());
+                                       USK usk = new USK(furi);
+                                       
+                                       if (usk.equals(key, false)) {
+                                               i.setKey(key.getURI());
+                                       } else {
+                                       }
+                               } catch (MalformedURLException mue) {
+                               }
+                       }
+               }
+       }
+       
+       private void subscribeUSKs() {
+               for (Enumeration e = bookmarks.elements(); e.hasMoreElements(); 
) {
+                       Bookmark i = (Bookmark)e.nextElement();
+                       
+                       if (i.getKeyType().equals("USK")) {
+                               try {
+                                       FreenetURI furi = new 
FreenetURI(i.getKey());
+                                       USK usk = new USK(furi);
+                                       node.uskManager.subscribe(usk, new 
USKUpdatedCallback(), true);
+                               } catch (MalformedURLException mue) {
+                               }
+                       }
+               }
+       }

        WelcomeToadlet(HighLevelSimpleClient client, Node n, SubConfig sc) {
                super(client);
@@ -96,8 +150,13 @@

                String[] initialbookmarks = sc.getStringArr("bookmarks");
                for (int i = 0; i < initialbookmarks.length; i++) {
-                       bookmarks.add(new Bookmark(initialbookmarks[i]));
+                       try {
+                               bookmarks.add(new 
Bookmark(initialbookmarks[i]));
+                       } catch (MalformedURLException mue) {
+                               // just ignore that one
+                       }
                }
+               subscribeUSKs();
        }

        public void handlePost(URI uri, Bucket data, ToadletContext ctx) throws 
ToadletContextClosedException, IOException {
@@ -185,6 +244,17 @@
                if(node.isTestnetEnabled())
                        buf.append("<div style=\"color: red; font-size: 200%; 
\">WARNING: TESTNET MODE ENABLED</div>");

+               
+               // Fetch-a-key box
+               buf.append("<br style=\"clear: all; \" />\n");
+               buf.append("<form action=\"/\" method=\"get\">\n");
+               buf.append("<div class=\"infobox\">\n");
+               buf.append("<h2>Fetch a Key</h2>\n");
+               buf.append("Key: <input type=\"text\" size=\"80\" 
name=\"key\"/>\n");
+               buf.append("<input type=\"submit\" value=\"Fetch\" />\n");
+               buf.append("</div>\n");
+               buf.append("</form>\n");
+               
                // Bookmarks
                buf.append("<div class=\"infobox\">\n");
                buf.append("<h2>My Bookmarks</h2>");
@@ -225,16 +295,6 @@
                }
                buf.append("</div>\n");

-               // Fetch-a-key box
-               buf.append("<br style=\"clear: all; \" />\n");
-               buf.append("<form action=\"/\" method=\"get\">\n");
-               buf.append("<div class=\"infobox\">\n");
-               buf.append("<h2>Fetch a Key</h2>\n");
-               buf.append("Key: <input type=\"text\" size=\"80\" 
name=\"key\"/>\n");
-               buf.append("<input type=\"submit\" value=\"Fetch\" />\n");
-               buf.append("</div>\n");
-               buf.append("</form>\n");
-               
                // Quit Form
                buf.append("<form method=\"post\" action=\".\">\n");
                buf.append("<div class=\"exit\">\n");

Modified: trunk/freenet/src/freenet/keys/USK.java
===================================================================
--- trunk/freenet/src/freenet/keys/USK.java     2006-03-24 18:38:39 UTC (rev 
8301)
+++ trunk/freenet/src/freenet/keys/USK.java     2006-03-24 19:03:57 UTC (rev 
8302)
@@ -84,12 +84,16 @@
        }

        public boolean equals(Object o) {
+               return equals(o, true);
+       }
+       
+       public boolean equals(Object o, boolean includeVersion) {
                if(o instanceof USK) {
                        USK u = (USK)o;
                        if(!Arrays.equals(pubKeyHash, u.pubKeyHash)) return 
false;
                        if(!Arrays.equals(cryptoKey, u.cryptoKey)) return false;
                        if(!siteName.equals(u.siteName)) return false;
-                       if(suggestedEdition != u.suggestedEdition) return false;
+                       if(includeVersion && suggestedEdition != 
u.suggestedEdition) return false;
                        return true;
                }
                return false;

Modified: trunk/freenet/src/freenet/node/Version.java
===================================================================
--- trunk/freenet/src/freenet/node/Version.java 2006-03-24 18:38:39 UTC (rev 
8301)
+++ trunk/freenet/src/freenet/node/Version.java 2006-03-24 19:03:57 UTC (rev 
8302)
@@ -20,7 +20,7 @@
        public static final String protocolVersion = "1.0";

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

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


Reply via email to