Merge branch 'master' into next
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 29 Oct 2010 04:47:52 +0000 (06:47 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 29 Oct 2010 04:47:52 +0000 (06:47 +0200)
pom.xml
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/FreenetInterface.java
src/main/java/net/pterodactylus/sone/core/SoneDownloader.java
src/main/java/net/pterodactylus/sone/core/SoneInserter.java
src/main/java/net/pterodactylus/sone/data/Post.java
src/main/java/net/pterodactylus/sone/data/Profile.java
src/main/java/net/pterodactylus/sone/data/Reply.java
src/main/java/net/pterodactylus/sone/data/Sone.java
src/main/java/net/pterodactylus/sone/main/SonePlugin.java

diff --git a/pom.xml b/pom.xml
index 4d37027..18b5594 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -2,7 +2,7 @@
        <modelVersion>4.0.0</modelVersion>
        <groupId>net.pterodactylus</groupId>
        <artifactId>sone</artifactId>
-       <version>0.1-RC1</version>
+       <version>0.1-RC2</version>
        <dependencies>
                <dependency>
                        <groupId>net.pterodactylus</groupId>
index accfa1e..99ebc6d 100644 (file)
@@ -56,6 +56,16 @@ import freenet.keys.FreenetURI;
  */
 public class Core extends AbstractService {
 
+       /** The default Sones. */
+       private static final Set<String> defaultSones = new HashSet<String>();
+
+       static {
+               /* Sone of Sone. */
+               defaultSones.add("USK@eRHt0ceFsHjRZ11j6dd68RSdIvfd8f9YjJLZ9lnhEyo,iJWjIWh6TkMZm1NY8qBranKTIuwsCPkVPG6T6c6ft-I,AQACAAE/Sone/3");
+               /* Sone of Bombe. */
+               defaultSones.add("USK@RuW~uAO35Ipne896-1OmaVJNPuYE4ZIB5oZ5ziaU57A,7rV3uiyztXBDt03DCoRiNwiGjgFCJuznM9Okc1opURU,AQACAAE/Sone/24");
+       }
+
        /**
         * Enumeration for the possible states of a {@link Sone}.
         *
@@ -92,13 +102,13 @@ public class Core extends AbstractService {
        private SoneDownloader soneDownloader;
 
        /** The Sone blacklist. */
-       private final Set<Sone> blacklistedSones = new HashSet<Sone>();
+       private final Set<Sone> blacklistedSones = Collections.synchronizedSet(new HashSet<Sone>());
 
        /** The local Sones. */
-       private final Set<Sone> localSones = new HashSet<Sone>();
+       private final Set<Sone> localSones = Collections.synchronizedSet(new HashSet<Sone>());
 
        /** Sone inserters. */
-       private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
+       private final Map<Sone, SoneInserter> soneInserters = Collections.synchronizedMap(new HashMap<Sone, SoneInserter>());
 
        /** The Sones’ statuses. */
        private final Map<Sone, SoneStatus> soneStatuses = Collections.synchronizedMap(new HashMap<Sone, SoneStatus>());
@@ -106,13 +116,13 @@ public class Core extends AbstractService {
        /* various caches follow here. */
 
        /** Cache for all known Sones. */
-       private final Map<String, Sone> soneCache = new HashMap<String, Sone>();
+       private final Map<String, Sone> soneCache = Collections.synchronizedMap(new HashMap<String, Sone>());
 
        /** Cache for all known posts. */
-       private final Map<String, Post> postCache = new HashMap<String, Post>();
+       private final Map<String, Post> postCache = Collections.synchronizedMap(new HashMap<String, Post>());
 
        /** Cache for all known replies. */
-       private final Map<String, Reply> replyCache = new HashMap<String, Reply>();
+       private final Map<String, Reply> replyCache = Collections.synchronizedMap(new HashMap<String, Reply>());
 
        /**
         * Creates a new core.
@@ -715,10 +725,9 @@ public class Core extends AbstractService {
         * Adds some default Sones.
         */
        private void addDefaultSones() {
-               /* Sone’s Sone. */
-               loadSone("USK@eRHt0ceFsHjRZ11j6dd68RSdIvfd8f9YjJLZ9lnhEyo,iJWjIWh6TkMZm1NY8qBranKTIuwsCPkVPG6T6c6ft-I,AQACAAE/Sone/0");
-               /* Bombe’s Sone. */
-               loadSone("USK@RuW~uAO35Ipne896-1OmaVJNPuYE4ZIB5oZ5ziaU57A,7rV3uiyztXBDt03DCoRiNwiGjgFCJuznM9Okc1opURU,AQACAAE/Sone/15");
+               for (String soneUri : defaultSones) {
+                       loadSone(soneUri);
+               }
        }
 
        /**
index 901019c..30ba69a 100644 (file)
@@ -151,7 +151,7 @@ public class FreenetInterface {
                                public void onFoundEdition(long edition, USK key, ObjectContainer objectContainer, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) {
                                        logger.log(Level.FINE, "Found USK update for Sone “%s” at %s, new known good: %s, new slot too: %s.", new Object[] { sone, key, newKnownGood, newSlotToo });
                                        if (newKnownGood) {
-                                               sone.updateUris(key.getURI());
+                                               sone.updateUris(key.suggestedEdition);
                                                soneDownloader.fetchSone(sone);
                                        }
                                }
index 94001a7..94f2b63 100644 (file)
@@ -257,9 +257,10 @@ public class SoneDownloader extends AbstractService {
                }
 
                String soneInsertUri = soneXml.getValue("insert-uri", null);
-               if (soneInsertUri != null) {
+               if ((soneInsertUri != null) && (sone.getInsertUri() == null)) {
                        try {
                                sone.setInsertUri(new FreenetURI(soneInsertUri));
+                               sone.updateUris(Math.max(sone.getRequestUri().getSuggestedEdition(), sone.getInsertUri().getSuggestedEdition()));
                        } catch (MalformedURLException mue1) {
                                /* TODO - mark Sone as bad. */
                                logger.log(Level.WARNING, "Downloaded Sone " + sone + " has invalid insert URI: " + soneInsertUri, mue1);
index 89f1b5e..05e3600 100644 (file)
@@ -147,7 +147,7 @@ public class SoneInserter extends AbstractService {
                                try {
                                        core.setSoneStatus(sone, SoneStatus.inserting);
                                        FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri().setKeyType("USK").setSuggestedEdition(0), insertInformation.generateManifestEntries(), "index.html");
-                                       sone.updateUris(finalUri);
+                                       sone.updateUris(finalUri.getEdition());
                                        success = true;
                                        logger.log(Level.INFO, "Inserted Sone “%s” at %s.", new Object[] { sone.getName(), finalUri });
                                } catch (SoneException se1) {
index d0938c9..c30665a 100644 (file)
@@ -31,13 +31,13 @@ public class Post {
        private final UUID id;
 
        /** The Sone this post belongs to. */
-       private Sone sone;
+       private volatile Sone sone;
 
        /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
-       private long time;
+       private volatile long time;
 
        /** The text of the post. */
-       private String text;
+       private volatile String text;
 
        /**
         * Creates a new post.
index 72901b3..7c29430 100644 (file)
@@ -26,25 +26,25 @@ package net.pterodactylus.sone.data;
 public class Profile {
 
        /** Whether the profile was modified. */
-       private boolean modified;
+       private volatile boolean modified;
 
        /** The first name. */
-       private String firstName;
+       private volatile String firstName;
 
        /** The middle name(s). */
-       private String middleName;
+       private volatile String middleName;
 
        /** The last name. */
-       private String lastName;
+       private volatile String lastName;
 
        /** The day of the birth date. */
-       private Integer birthDay;
+       private volatile Integer birthDay;
 
        /** The month of the birth date. */
-       private Integer birthMonth;
+       private volatile Integer birthMonth;
 
        /** The year of the birth date. */
-       private Integer birthYear;
+       private volatile Integer birthYear;
 
        /**
         * Creates a new empty profile.
index 4429abf..05d8049 100644 (file)
@@ -31,16 +31,16 @@ public class Reply {
        private final UUID id;
 
        /** The Sone that posted this reply. */
-       private Sone sone;
+       private volatile Sone sone;
 
        /** The Post this reply refers to. */
-       private Post post;
+       private volatile Post post;
 
        /** The time of the reply. */
-       private long time;
+       private volatile long time;
 
        /** The text of the reply. */
-       private String text;
+       private volatile String text;
 
        /**
         * Creates a new reply.
index dd31c03..129293c 100644 (file)
@@ -49,38 +49,38 @@ public class Sone {
        private final UUID id;
 
        /** The name of this Sone. */
-       private String name;
+       private volatile String name;
 
        /** The URI under which the Sone is stored in Freenet. */
-       private FreenetURI requestUri;
+       private volatile FreenetURI requestUri;
 
        /** The URI used to insert a new version of this Sone. */
        /* This will be null for remote Sones! */
-       private FreenetURI insertUri;
+       private volatile FreenetURI insertUri;
 
        /** The time of the last inserted update. */
-       private long time;
+       private volatile long time;
 
        /** The profile of this Sone. */
-       private Profile profile;
+       private volatile Profile profile;
 
        /** All friend Sones. */
-       private final Set<Sone> friendSones = new HashSet<Sone>();
+       private final Set<Sone> friendSones = Collections.synchronizedSet(new HashSet<Sone>());
 
        /** All posts. */
-       private final Set<Post> posts = new HashSet<Post>();
+       private final Set<Post> posts = Collections.synchronizedSet(new HashSet<Post>());
 
        /** All replies. */
-       private final Set<Reply> replies = new HashSet<Reply>();
+       private final Set<Reply> replies = Collections.synchronizedSet(new HashSet<Reply>());
 
        /** The IDs of all blocked Sones. */
-       private final Set<String> blockedSoneIds = new HashSet<String>();
+       private final Set<String> blockedSoneIds = Collections.synchronizedSet(new HashSet<String>());
 
        /** The IDs of all liked posts. */
-       private final Set<String> likedPostIds = new HashSet<String>();
+       private final Set<String> likedPostIds = Collections.synchronizedSet(new HashSet<String>());
 
        /** The IDs of all liked replies. */
-       private final Set<String> likedReplyIds = new HashSet<String>();
+       private final Set<String> likedReplyIds = Collections.synchronizedSet(new HashSet<String>());
 
        /** Modification count. */
        private volatile long modificationCounter = 0;
@@ -146,7 +146,19 @@ public class Sone {
         * @return This Sone (for method chaining)
         */
        public Sone setRequestUri(FreenetURI requestUri) {
-               this.requestUri = requestUri;
+               if (this.requestUri == null) {
+                       this.requestUri = requestUri;
+                       updateEditions();
+                       return this;
+               }
+               if (!this.requestUri.equalsKeypair(requestUri)) {
+                       logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { requestUri, this.requestUri });
+                       return this;
+               }
+               long latestEdition = requestUri.getEdition();
+               if ((latestEdition > this.requestUri.getEdition()) || (latestEdition > this.requestUri.getSuggestedEdition())) {
+                       this.requestUri.setSuggestedEdition(latestEdition);
+               }
                return this;
        }
 
@@ -167,7 +179,19 @@ public class Sone {
         * @return This Sone (for method chaining)
         */
        public Sone setInsertUri(FreenetURI insertUri) {
-               this.insertUri = insertUri;
+               if (this.insertUri == null) {
+                       this.insertUri = insertUri;
+                       updateEditions();
+                       return this;
+               }
+               if (!this.insertUri.equalsKeypair(insertUri)) {
+                       logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { insertUri, this.insertUri });
+                       return this;
+               }
+               long latestEdition = insertUri.getEdition();
+               if ((latestEdition > this.insertUri.getEdition()) || (latestEdition > this.insertUri.getSuggestedEdition())) {
+                       this.insertUri.setSuggestedEdition(latestEdition);
+               }
                return this;
        }
 
@@ -589,16 +613,36 @@ public class Sone {
        /**
         * Updates the suggested edition in both the request URI and the insert URI.
         *
-        * @param requestUri
-        *            The request URI that resulted from an insert
-        */
-       public void updateUris(FreenetURI requestUri) {
-               /* TODO - check for the correct URI. */
-               long latestEdition = requestUri.getSuggestedEdition();
-               this.requestUri = this.requestUri.setSuggestedEdition(latestEdition);
-               if (this.insertUri != null) {
-                       this.insertUri = this.insertUri.setSuggestedEdition(latestEdition);
+        * @param latestEdition
+        *            The latest edition to update the URIs to
+        */
+       public void updateUris(long latestEdition) {
+               if ((requestUri != null) && (requestUri.getEdition() < latestEdition)) {
+                       requestUri = requestUri.setSuggestedEdition(latestEdition);
+               }
+               if ((insertUri != null) && (insertUri.getEdition() < latestEdition)) {
+                       insertUri = insertUri.setSuggestedEdition(latestEdition);
+               }
+       }
+
+       //
+       // PRIVATE METHODS
+       //
+
+       /**
+        * Updates the editions of the request URI and the insert URI (if latter is
+        * not {@code null}) with the greater edition of either one.
+        */
+       private void updateEditions() {
+               long requestEdition = 0;
+               if (requestUri != null) {
+                       requestEdition = requestUri.getEdition();
+               }
+               long insertEdition = 0;
+               if (insertUri != null) {
+                       insertEdition = insertUri.getEdition();
                }
+               updateUris(Math.max(requestEdition, insertEdition));
        }
 
        //
index 2215745..ecf7f5b 100644 (file)
@@ -79,7 +79,7 @@ public class SonePlugin implements FredPlugin, FredPluginL10n, FredPluginBaseL10
        }
 
        /** The version. */
-       public static final Version VERSION = new Version("RC1", 0, 1);
+       public static final Version VERSION = new Version("RC2", 0, 1);
 
        /** The logger. */
        private static final Logger logger = Logging.getLogger(SonePlugin.class);