Download updates via the thread factory, too.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 275ef9b..3408383 100644 (file)
@@ -53,13 +53,13 @@ import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.freenet.wot.IdentityListener;
 import net.pterodactylus.sone.freenet.wot.IdentityManager;
 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
-import net.pterodactylus.sone.freenet.wot.Trust;
 import net.pterodactylus.sone.main.SonePlugin;
 import net.pterodactylus.util.config.Configuration;
 import net.pterodactylus.util.config.ConfigurationException;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.number.Numbers;
 import net.pterodactylus.util.service.AbstractService;
+import net.pterodactylus.util.thread.NamedThreadFactory;
 import net.pterodactylus.util.thread.Ticker;
 import net.pterodactylus.util.validation.EqualityValidator;
 import net.pterodactylus.util.validation.IntegerRangeValidator;
@@ -78,6 +78,9 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        /** The logger. */
        private static final Logger logger = Logging.getLogger(Core.class);
 
+       /** The start time. */
+       private final long startupTime = System.currentTimeMillis();
+
        /** The options. */
        private final Options options = new Options();
 
@@ -106,7 +109,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        private final ImageInserter imageInserter;
 
        /** Sone downloader thread-pool. */
-       private final ExecutorService soneDownloaders = Executors.newFixedThreadPool(10);
+       private final ExecutorService soneDownloaders = Executors.newFixedThreadPool(10, new NamedThreadFactory("Sone Downloader %2$d"));
 
        /** The update checker. */
        private final UpdateChecker updateChecker;
@@ -229,6 +232,15 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        //
 
        /**
+        * Returns the time Sone was started.
+        *
+        * @return The startup time (in milliseconds since Jan 1, 1970 UTC)
+        */
+       public long getStartupTime() {
+               return startupTime;
+       }
+
+       /**
         * Sets the configuration to use. This will automatically save the current
         * configuration to the given configuration.
         *
@@ -1042,26 +1054,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        }
 
        /**
-        * Retrieves the trust relationship from the origin to the target. If the
-        * trust relationship can not be retrieved, {@code null} is returned.
-        *
-        * @see Identity#getTrust(OwnIdentity)
-        * @param origin
-        *            The origin of the trust tree
-        * @param target
-        *            The target of the trust
-        * @return The trust relationship
-        */
-       public Trust getTrust(Sone origin, Sone target) {
-               if (!isLocalSone(origin)) {
-                       logger.log(Level.WARNING, String.format("Tried to get trust from remote Sone: %s", origin));
-                       return null;
-               }
-               webOfTrustUpdater.getTrust((OwnIdentity) origin.getIdentity(), target.getIdentity());
-               return target.getIdentity().getTrust((OwnIdentity) origin.getIdentity());
-       }
-
-       /**
         * Sets the trust value of the given origin Sone for the target Sone.
         *
         * @param origin
@@ -1169,8 +1161,8 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                                if (!storedPosts.contains(post)) {
                                                        if (post.getTime() < getSoneFollowingTime(sone)) {
                                                                knownPosts.add(post.getId());
+                                                               post.setKnown(true);
                                                        } else if (!knownPosts.contains(post.getId())) {
-                                                               sone.setKnown(false);
                                                                coreListenerManager.fireNewPostFound(post);
                                                        }
                                                }
@@ -1194,8 +1186,8 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                                if (!storedReplies.contains(reply)) {
                                                        if (reply.getTime() < getSoneFollowingTime(sone)) {
                                                                knownReplies.add(reply.getId());
+                                                               reply.setKnown(true);
                                                        } else if (!knownReplies.contains(reply.getId())) {
-                                                               reply.setKnown(false);
                                                                coreListenerManager.fireNewReplyFound(reply);
                                                        }
                                                }
@@ -1987,11 +1979,13 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        @Override
        public void serviceStop() {
                synchronized (localSones) {
-                       for (SoneInserter soneInserter : soneInserters.values()) {
-                               soneInserter.removeSoneInsertListener(this);
-                               soneInserter.stop();
+                       for (Entry<Sone, SoneInserter> soneInserter : soneInserters.entrySet()) {
+                               soneInserter.getValue().removeSoneInsertListener(this);
+                               soneInserter.getValue().stop();
+                               saveSone(soneInserter.getKey());
                        }
                }
+               saveConfiguration();
                webOfTrustUpdater.stop();
                updateChecker.stop();
                updateChecker.removeUpdateListener(this);
@@ -2369,12 +2363,12 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *            The URI to derive the Sone URI from
         * @return The derived URI
         */
-       private FreenetURI getSoneUri(String uriString) {
+       private static FreenetURI getSoneUri(String uriString) {
                try {
                        FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
                        return uri;
                } catch (MalformedURLException mue1) {
-                       logger.log(Level.WARNING, String.format("Could not create Sone URI from URI: %s", uriString, mue1));
+                       logger.log(Level.WARNING, String.format("Could not create Sone URI from URI: %s", uriString), mue1);
                        return null;
                }
        }
@@ -2419,7 +2413,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         */
        @Override
        public void identityUpdated(OwnIdentity ownIdentity, final Identity identity) {
-               new Thread(new Runnable() {
+               soneDownloaders.execute(new Runnable() {
 
                        @Override
                        @SuppressWarnings("synthetic-access")
@@ -2430,7 +2424,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                soneDownloader.addSone(sone);
                                soneDownloader.fetchSone(sone);
                        }
-               }).start();
+               });
        }
 
        /**