From: David ‘Bombe’ Roden Date: Wed, 5 Sep 2012 09:57:35 +0000 (+0200) Subject: Add background trust updater. X-Git-Tag: 0.8.3^2~54^2~14 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=3f28d9bce06f467c088f0c937adbe04ed79f17d0;p=Sone.git Add background trust updater. --- diff --git a/src/main/java/net/pterodactylus/sone/core/TrustUpdater.java b/src/main/java/net/pterodactylus/sone/core/TrustUpdater.java new file mode 100644 index 0000000..6221d39 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/core/TrustUpdater.java @@ -0,0 +1,265 @@ +/* + * Sone - TrustUpdater.java - Copyright © 2012 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.core; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.logging.Level; +import java.util.logging.Logger; + +import net.pterodactylus.sone.freenet.plugin.PluginException; +import net.pterodactylus.sone.freenet.wot.DefaultIdentity; +import net.pterodactylus.sone.freenet.wot.Identity; +import net.pterodactylus.sone.freenet.wot.OwnIdentity; +import net.pterodactylus.sone.freenet.wot.Trust; +import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector; +import net.pterodactylus.sone.freenet.wot.WebOfTrustException; +import net.pterodactylus.util.logging.Logging; +import net.pterodactylus.util.service.AbstractService; + +/** + * Updates identity’s trust in a background thread because getting updates from + * the WebOfTrust plugin can potentially last quite long. + * + * @author David ‘Bombe’ Roden + */ +public class TrustUpdater extends AbstractService { + + /** The logger. */ + private static final Logger logger = Logging.getLogger(TrustUpdater.class); + + /** Stop job. */ + private static final TrustUpdateJob stopJob = new TrustUpdateJob(null, null); + + /** The web of trust connector. */ + private final WebOfTrustConnector webOfTrustConnector; + + /** The queue for jobs. */ + private final BlockingQueue updateJobs = new LinkedBlockingQueue(); + + /** + * Creates a new trust updater. + * + * @param webOfTrustConnector + * The web of trust connector + */ + public TrustUpdater(WebOfTrustConnector webOfTrustConnector) { + super("Trust Updater"); + this.webOfTrustConnector = webOfTrustConnector; + } + + // + // ACTIONS + // + + /** + * Retrieves the trust relation between the truster and the trustee. This + * method will return immediately and perform a trust update in the + * background. + * + * @param truster + * The identity giving the trust + * @param trustee + * The identity receiving the trust + */ + public void getTrust(OwnIdentity truster, Identity trustee) { + updateJobs.add(new GetTrustJob(truster, trustee)); + } + + /** + * Updates the trust relation between the truster and the trustee. This + * method will return immediately and perform a trust update in the + * background. + * + * @param truster + * The identity giving the trust + * @param trustee + * The identity receiving the trust + * @param score + * The new level of trust (from -100 to 100, may be {@code null} + * to remove the trust completely) + * @param comment + * The comment of the trust relation + */ + public void setTrust(OwnIdentity truster, Identity trustee, Integer score, String comment) { + updateJobs.add(new SetTrustJob(truster, trustee, score, comment)); + } + + // + // SERVICE METHODS + // + + /** + * {@inheritDoc} + */ + @Override + protected void serviceRun() { + while (!shouldStop()) { + try { + TrustUpdateJob updateJob = updateJobs.take(); + if (shouldStop() || (updateJob == stopJob)) { + break; + } + updateJob.run(); + } catch (InterruptedException ie1) { + /* happens, ignore, loop. */ + } + } + } + + /** + * {@inheritDoc} + */ + @Override + protected void serviceStop() { + try { + updateJobs.put(stopJob); + } catch (InterruptedException ie1) { + /* the queue is unbounded so it should never block. */ + } + } + + /** + * Base class for trust update jobs. + * + * @author David ‘Bombe’ Roden + */ + private static class TrustUpdateJob { + + /** The identity giving the trust. */ + protected final OwnIdentity truster; + + /** The identity receiving the trust. */ + protected final Identity trustee; + + /** + * Creates a new trust update job. + * + * @param truster + * The identity giving the trust + * @param trustee + * The identity receiving the trust + */ + public TrustUpdateJob(OwnIdentity truster, Identity trustee) { + this.truster = truster; + this.trustee = trustee; + } + + // + // ACCESSORS + // + + /** + * Performs the actual update operation. + *

+ * The implementation of this class does nothing. + */ + public void run() { + /* does nothing. */ + } + + } + + /** + * Update job that sets the trust relation between two identities. + * + * @author David ‘Bombe’ Roden + */ + private static class SetTrustJob extends TrustUpdateJob { + + /** The score of the relation. */ + private final Integer score; + + /** The comment of the relation. */ + private final String comment; + + /** + * Creates a new set trust job. + * + * @param truster + * The identity giving the trust + * @param trustee + * The identity receiving the trust + * @param score + * The score of the trust (from -100 to 100, may be + * {@code null} to remote the trust relation completely) + * @param comment + * The comment of the trust relation + */ + public SetTrustJob(OwnIdentity truster, Identity trustee, Integer score, String comment) { + super(truster, trustee); + this.score = score; + this.comment = comment; + } + + /** + * {@inheritDoc} + */ + @Override + @SuppressWarnings("synthetic-access") + public void run() { + try { + if (score != null) { + truster.setTrust(trustee, score, comment); + } else { + truster.removeTrust(trustee); + } + } catch (WebOfTrustException wote1) { + logger.log(Level.WARNING, "Could not set Trust value for " + truster + " -> " + trustee + " to " + score + " (" + comment + ")!", wote1); + } + } + + } + + /** + * Update job that retrieves the trust relation between two identities. + * + * @author David ‘Bombe’ Roden + */ + private class GetTrustJob extends TrustUpdateJob { + + /** + * Creates a new trust update job. + * + * @param truster + * The identity giving the trust + * @param trustee + * The identity receiving the trust + */ + public GetTrustJob(OwnIdentity truster, Identity trustee) { + super(truster, trustee); + } + + /** + * {@inheritDoc} + */ + @Override + @SuppressWarnings("synthetic-access") + public void run() { + try { + Trust trust = webOfTrustConnector.getTrust(truster, trustee); + if (trustee instanceof DefaultIdentity) { + ((DefaultIdentity) trustee).setTrust(truster, trust); + } + } catch (PluginException pe1) { + logger.log(Level.WARNING, "Could not get Trust value for " + truster + " -> " + trustee + "!", pe1); + } + } + } + +}