Update trust values in a dedicated background thread.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 7 Sep 2012 07:26:34 +0000 (09:26 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 7 Sep 2012 07:26:44 +0000 (09:26 +0200)
This will decouple Sone’s web interface from WoT’s excessive (b)locking.

1  2 
src/main/java/net/pterodactylus/sone/core/Core.java

@@@ -112,6 -112,9 +112,9 @@@ public class Core extends AbstractServi
        /** The update checker. */
        private final UpdateChecker updateChecker;
  
+       /** The trust updater. */
+       private final TrustUpdater trustUpdater;
        /** The FCP interface. */
        private volatile FcpInterface fcpInterface;
  
         * @param identityManager
         *            The identity manager
         */
-       public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
+       public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, TrustUpdater trustUpdater) {
                super("Sone Core");
                this.configuration = configuration;
                this.freenetInterface = freenetInterface;
                this.soneDownloader = new SoneDownloader(this, freenetInterface);
                this.imageInserter = new ImageInserter(this, freenetInterface);
                this.updateChecker = new UpdateChecker(freenetInterface);
+               this.trustUpdater = trustUpdater;
        }
  
        //
                        logger.log(Level.WARNING, String.format("Tried to get trust from remote Sone: %s", origin));
                        return null;
                }
+               trustUpdater.getTrust((OwnIdentity) origin.getIdentity(), target.getIdentity());
                return target.getIdentity().getTrust((OwnIdentity) origin.getIdentity());
        }
  
         */
        public void setTrust(Sone origin, Sone target, int trustValue) {
                Validation.begin().isNotNull("Trust Origin", origin).check().isInstanceOf("Trust Origin", origin.getIdentity(), OwnIdentity.class).isNotNull("Trust Target", target).isLessOrEqual("Trust Value", trustValue, 100).isGreaterOrEqual("Trust Value", trustValue, -100).check();
-               try {
-                       ((OwnIdentity) origin.getIdentity()).setTrust(target.getIdentity(), trustValue, preferences.getTrustComment());
-               } catch (WebOfTrustException wote1) {
-                       logger.log(Level.WARNING, String.format("Could not set trust for Sone: %s", target), wote1);
-               }
+               trustUpdater.setTrust((OwnIdentity) origin.getIdentity(), target.getIdentity(), trustValue, preferences.getTrustComment());
        }
  
        /**
         */
        public void removeTrust(Sone origin, Sone target) {
                Validation.begin().isNotNull("Trust Origin", origin).isNotNull("Trust Target", target).check().isInstanceOf("Trust Origin Identity", origin.getIdentity(), OwnIdentity.class).check();
-               try {
-                       ((OwnIdentity) origin.getIdentity()).removeTrust(target.getIdentity());
-               } catch (WebOfTrustException wote1) {
-                       logger.log(Level.WARNING, String.format("Could not remove trust for Sone: %s", target), wote1);
-               }
+               trustUpdater.setTrust((OwnIdentity) origin.getIdentity(), target.getIdentity(), null, null);
        }
  
        /**
                loadConfiguration();
                updateChecker.addUpdateListener(this);
                updateChecker.start();
+               trustUpdater.start();
        }
  
        /**
                                soneInserter.stop();
                        }
                }
+               trustUpdater.stop();
                updateChecker.stop();
                updateChecker.removeUpdateListener(this);
                soneDownloader.stop();
                        configuration.getStringValue("Option/TrustComment").setValue(options.getStringOption("TrustComment").getReal());
                        configuration.getBooleanValue("Option/ActivateFcpInterface").setValue(options.getBooleanOption("ActivateFcpInterface").getReal());
                        configuration.getIntValue("Option/FcpFullAccessRequired").setValue(options.getIntegerOption("FcpFullAccessRequired").getReal());
 -                      configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").getReal());
 -                      configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
 -                      configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
  
                        /* save known Sones. */
                        int soneCounter = 0;
                        }
  
                }));
 -              options.addBooleanOption("SoneRescueMode", new DefaultOption<Boolean>(false));
 -              options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
 -              options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
 -
 -              /* read options from configuration. */
 -              options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
 -              options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
 -              boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
 -              options.getBooleanOption("ClearOnNextRestart").set(null);
 -              options.getBooleanOption("ReallyClearOnNextRestart").set(null);
 -              if (clearConfiguration) {
 -                      /* stop loading the configuration. */
 -                      return;
 -              }
  
                loadConfigurationValue("InsertionDelay");
                loadConfigurationValue("PostsPerPage");
                options.getStringOption("TrustComment").set(configuration.getStringValue("Option/TrustComment").getValue(null));
                options.getBooleanOption("ActivateFcpInterface").set(configuration.getBooleanValue("Option/ActivateFcpInterface").getValue(null));
                options.getIntegerOption("FcpFullAccessRequired").set(configuration.getIntValue("Option/FcpFullAccessRequired").getValue(null));
 -              options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").getValue(null));
  
                /* load known Sones. */
                int soneCounter = 0;
                        return this;
                }
  
 -              /**
 -               * Returns whether Sone should clear its settings on the next restart.
 -               * In order to be effective, {@link #isReallyClearOnNextRestart()} needs
 -               * to return {@code true} as well!
 -               *
 -               * @return {@code true} if Sone should clear its settings on the next
 -               *         restart, {@code false} otherwise
 -               */
 -              public boolean isClearOnNextRestart() {
 -                      return options.getBooleanOption("ClearOnNextRestart").get();
 -              }
 -
 -              /**
 -               * Sets whether Sone will clear its settings on the next restart.
 -               *
 -               * @param clearOnNextRestart
 -               *            {@code true} if Sone should clear its settings on the next
 -               *            restart, {@code false} otherwise
 -               * @return This preferences
 -               */
 -              public Preferences setClearOnNextRestart(Boolean clearOnNextRestart) {
 -                      options.getBooleanOption("ClearOnNextRestart").set(clearOnNextRestart);
 -                      return this;
 -              }
 -
 -              /**
 -               * Returns whether Sone should really clear its settings on next
 -               * restart. This is a confirmation option that needs to be set in
 -               * addition to {@link #isClearOnNextRestart()} in order to clear Sone’s
 -               * settings on the next restart.
 -               *
 -               * @return {@code true} if Sone should really clear its settings on the
 -               *         next restart, {@code false} otherwise
 -               */
 -              public boolean isReallyClearOnNextRestart() {
 -                      return options.getBooleanOption("ReallyClearOnNextRestart").get();
 -              }
 -
 -              /**
 -               * Sets whether Sone should really clear its settings on the next
 -               * restart.
 -               *
 -               * @param reallyClearOnNextRestart
 -               *            {@code true} if Sone should really clear its settings on
 -               *            the next restart, {@code false} otherwise
 -               * @return This preferences
 -               */
 -              public Preferences setReallyClearOnNextRestart(Boolean reallyClearOnNextRestart) {
 -                      options.getBooleanOption("ReallyClearOnNextRestart").set(reallyClearOnNextRestart);
 -                      return this;
 -              }
 -
        }
  
  }