X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FWebOfTrustUpdater.java;h=95d066d710fecd56ce1fa4c4e3a70bcfb124946e;hp=cfb99322ab69834ae229f65f0e11676aa0d94856;hb=HEAD;hpb=aff24d18360f8f77c8b40068280965de72675657 diff --git a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java index cfb9932..95d066d 100644 --- a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java +++ b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java @@ -1,377 +1,19 @@ -/* - * Sone - WebOfTrustUpdater.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; +import net.pterodactylus.util.service.Service; + +import com.google.inject.ImplementedBy; /** - * Updates WebOfTrust identity data in a background thread because communicating - * with the WebOfTrust plugin can potentially last quite long. - * - * @author David ‘Bombe’ Roden + * Updates WebOfTrust identity data. */ -public class WebOfTrustUpdater extends AbstractService { - - /** The logger. */ - private static final Logger logger = Logging.getLogger(WebOfTrustUpdater.class); - - /** Stop job. */ - @SuppressWarnings("synthetic-access") - private final WebOfTrustUpdateJob stopJob = new WebOfTrustUpdateJob(); - - /** 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 WebOfTrustUpdater(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) { - GetTrustJob getTrustJob = new GetTrustJob(truster, trustee); - if (!updateJobs.contains(getTrustJob)) { - logger.log(Level.FINER, "Adding Trust Update Job: " + getTrustJob); - try { - updateJobs.put(getTrustJob); - } catch (InterruptedException ie1) { - /* the queue is unbounded so it should never block. */ - } - } - } - - /** - * 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) { - SetTrustJob setTrustJob = new SetTrustJob(truster, trustee, score, comment); - if (updateJobs.contains(setTrustJob)) { - updateJobs.remove(setTrustJob); - } - logger.log(Level.FINER, "Adding Trust Update Job: " + setTrustJob); - try { - updateJobs.put(setTrustJob); - } catch (InterruptedException e) { - /* the queue is unbounded so it should never block. */ - } - } - - // - // SERVICE METHODS - // - - /** - * {@inheritDoc} - */ - @Override - protected void serviceRun() { - while (!shouldStop()) { - try { - WebOfTrustUpdateJob updateJob = updateJobs.take(); - if (shouldStop() || (updateJob == stopJob)) { - break; - } - logger.log(Level.FINE, "Running Trust Update Job: " + updateJob); - long startTime = System.currentTimeMillis(); - updateJob.run(); - long endTime = System.currentTimeMillis(); - logger.log(Level.FINE, "Trust Update Job finished, took " + (endTime - startTime) + " ms."); - } 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 WebOfTrust update jobs. - * - * @author David ‘Bombe’ Roden - */ - private class WebOfTrustUpdateJob { - - /** Object for synchronization. */ - @SuppressWarnings("hiding") - private final Object syncObject = new Object(); - - /** Whether the job has finished. */ - private boolean finished; - - // - // ACTIONS - // - - /** - * Performs the actual update operation. - *

- * The implementation of this class does nothing. - */ - public void run() { - /* does nothing. */ - } - - /** - * Waits for completion of this job or stopping of the WebOfTrust - * updater. - * - * @see WebOfTrustUpdater#stop() - */ - @SuppressWarnings("synthetic-access") - public void waitForCompletion() { - synchronized (syncObject) { - while (!finished && !shouldStop()) { - try { - syncObject.wait(); - } catch (InterruptedException ie1) { - /* we’re looping, ignore. */ - } - } - } - } - - // - // PROTECTED METHODS - // - - /** - * Signals that this job has finished. - */ - protected void finish() { - synchronized (syncObject) { - finished = true; - syncObject.notifyAll(); - } - } - - } - - /** - * Base class for WebOfTrust trust update jobs. - * - * @author David ‘Bombe’ Roden - */ - private class WebOfTrustTrustUpdateJob extends WebOfTrustUpdateJob { - - /** 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 - */ - @SuppressWarnings("synthetic-access") - public WebOfTrustTrustUpdateJob(OwnIdentity truster, Identity trustee) { - this.truster = truster; - this.trustee = trustee; - } - - // - // OBJECT METHODS - // - - /** - * {@inheritDoc} - */ - @Override - public boolean equals(Object object) { - if ((object == null) || !object.getClass().equals(getClass())) { - return false; - } - WebOfTrustTrustUpdateJob updateJob = (WebOfTrustTrustUpdateJob) object; - return ((truster == null) ? (updateJob.truster == null) : updateJob.truster.equals(truster)) && ((trustee == null) ? (updateJob.trustee == null) : updateJob.trustee.equals(trustee)); - } - - /** - * {@inheritDoc} - */ - @Override - public int hashCode() { - return getClass().hashCode() ^ ((truster == null) ? 0 : truster.hashCode()) ^ ((trustee == null) ? 0 : trustee.hashCode()); - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - return String.format("%s[truster=%s,trustee=%s]", getClass().getSimpleName(), (truster == null) ? null : truster.getId(), (trustee == null) ? null : trustee.getId()); - } - - } - - /** - * Update job that sets the trust relation between two identities. - * - * @author David ‘Bombe’ Roden - */ - private class SetTrustJob extends WebOfTrustTrustUpdateJob { - - /** 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) { - if (trustee instanceof DefaultIdentity) { - ((DefaultIdentity) trustee).setTrust(truster, new Trust(score, null, 0)); - } - webOfTrustConnector.setTrust(truster, trustee, score, comment); - } else { - if (trustee instanceof DefaultIdentity) { - ((DefaultIdentity) trustee).setTrust(truster, null); - } - webOfTrustConnector.removeTrust(truster, trustee); - } - } catch (WebOfTrustException wote1) { - logger.log(Level.WARNING, "Could not set Trust value for " + truster + " -> " + trustee + " to " + score + " (" + comment + ")!", wote1); - } - finish(); - } - - } - - /** - * Update job that retrieves the trust relation between two identities. - * - * @author David ‘Bombe’ Roden - */ - private class GetTrustJob extends WebOfTrustTrustUpdateJob { - - /** - * 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); - } +@ImplementedBy(WebOfTrustUpdaterImpl.class) +public interface WebOfTrustUpdater extends Service { - /** - * {@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); - } - finish(); - } - } + boolean addContextWait(OwnIdentity ownIdentity, String context); + void removeContext(OwnIdentity ownIdentity, String context); + void setProperty(OwnIdentity ownIdentity, String propertyName, String propertyValue); + void removeProperty(OwnIdentity ownIdentity, String propertyName); }