2 * Sone - TrustUpdater.java - Copyright © 2012 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.core;
20 import java.util.concurrent.BlockingQueue;
21 import java.util.concurrent.LinkedBlockingQueue;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
25 import net.pterodactylus.sone.freenet.plugin.PluginException;
26 import net.pterodactylus.sone.freenet.wot.DefaultIdentity;
27 import net.pterodactylus.sone.freenet.wot.Identity;
28 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
29 import net.pterodactylus.sone.freenet.wot.Trust;
30 import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector;
31 import net.pterodactylus.sone.freenet.wot.WebOfTrustException;
32 import net.pterodactylus.util.logging.Logging;
33 import net.pterodactylus.util.service.AbstractService;
36 * Updates identity’s trust in a background thread because getting updates from
37 * the WebOfTrust plugin can potentially last quite long.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class TrustUpdater extends AbstractService {
44 private static final Logger logger = Logging.getLogger(TrustUpdater.class);
47 private static final TrustUpdateJob stopJob = new TrustUpdateJob(null, null);
49 /** The web of trust connector. */
50 private final WebOfTrustConnector webOfTrustConnector;
52 /** The queue for jobs. */
53 private final BlockingQueue<TrustUpdateJob> updateJobs = new LinkedBlockingQueue<TrustUpdateJob>();
56 * Creates a new trust updater.
58 * @param webOfTrustConnector
59 * The web of trust connector
61 public TrustUpdater(WebOfTrustConnector webOfTrustConnector) {
62 super("Trust Updater");
63 this.webOfTrustConnector = webOfTrustConnector;
71 * Retrieves the trust relation between the truster and the trustee. This
72 * method will return immediately and perform a trust update in the
76 * The identity giving the trust
78 * The identity receiving the trust
80 public void getTrust(OwnIdentity truster, Identity trustee) {
81 GetTrustJob getTrustJob = new GetTrustJob(truster, trustee);
82 if (!updateJobs.contains(getTrustJob)) {
83 logger.log(Level.FINER, "Adding Trust Update Job: " + getTrustJob);
85 updateJobs.put(getTrustJob);
86 } catch (InterruptedException ie1) {
87 /* the queue is unbounded so it should never block. */
93 * Updates the trust relation between the truster and the trustee. This
94 * method will return immediately and perform a trust update in the
98 * The identity giving the trust
100 * The identity receiving the trust
102 * The new level of trust (from -100 to 100, may be {@code null}
103 * to remove the trust completely)
105 * The comment of the trust relation
107 public void setTrust(OwnIdentity truster, Identity trustee, Integer score, String comment) {
108 SetTrustJob setTrustJob = new SetTrustJob(truster, trustee, score, comment);
109 if (updateJobs.contains(setTrustJob)) {
110 updateJobs.remove(setTrustJob);
112 logger.log(Level.FINER, "Adding Trust Update Job: " + setTrustJob);
114 updateJobs.put(setTrustJob);
115 } catch (InterruptedException e) {
116 /* the queue is unbounded so it should never block. */
128 protected void serviceRun() {
129 while (!shouldStop()) {
131 TrustUpdateJob updateJob = updateJobs.take();
132 if (shouldStop() || (updateJob == stopJob)) {
136 } catch (InterruptedException ie1) {
137 /* happens, ignore, loop. */
146 protected void serviceStop() {
148 updateJobs.put(stopJob);
149 } catch (InterruptedException ie1) {
150 /* the queue is unbounded so it should never block. */
155 * Base class for trust update jobs.
157 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
159 private static class TrustUpdateJob {
161 /** The identity giving the trust. */
162 protected final OwnIdentity truster;
164 /** The identity receiving the trust. */
165 protected final Identity trustee;
168 * Creates a new trust update job.
171 * The identity giving the trust
173 * The identity receiving the trust
175 public TrustUpdateJob(OwnIdentity truster, Identity trustee) {
176 this.truster = truster;
177 this.trustee = trustee;
185 * Performs the actual update operation.
187 * The implementation of this class does nothing.
201 public boolean equals(Object object) {
202 if ((object == null) || !object.getClass().equals(getClass())) {
205 TrustUpdateJob updateJob = (TrustUpdateJob) object;
206 return ((truster == null) ? (updateJob.truster == null) : updateJob.truster.equals(truster)) && ((trustee == null) ? (updateJob.trustee == null) : updateJob.trustee.equals(trustee));
213 public int hashCode() {
214 return ((truster == null) ? 0 : truster.hashCode()) ^ ((trustee == null) ? 0 : trustee.hashCode());
221 public String toString() {
222 return String.format("%s[truster=%s,trustee=%s]", getClass().getSimpleName(), (truster == null) ? null : truster.getId(), (trustee == null) ? null : trustee.getId());
228 * Update job that sets the trust relation between two identities.
230 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
232 private class SetTrustJob extends TrustUpdateJob {
234 /** The score of the relation. */
235 private final Integer score;
237 /** The comment of the relation. */
238 private final String comment;
241 * Creates a new set trust job.
244 * The identity giving the trust
246 * The identity receiving the trust
248 * The score of the trust (from -100 to 100, may be
249 * {@code null} to remote the trust relation completely)
251 * The comment of the trust relation
253 public SetTrustJob(OwnIdentity truster, Identity trustee, Integer score, String comment) {
254 super(truster, trustee);
256 this.comment = comment;
263 @SuppressWarnings("synthetic-access")
267 if (trustee instanceof DefaultIdentity) {
268 ((DefaultIdentity) trustee).setTrust(truster, new Trust(score, null, 0));
270 webOfTrustConnector.setTrust(truster, trustee, score, comment);
272 if (trustee instanceof DefaultIdentity) {
273 ((DefaultIdentity) trustee).setTrust(truster, null);
275 webOfTrustConnector.removeTrust(truster, trustee);
277 } catch (WebOfTrustException wote1) {
278 logger.log(Level.WARNING, "Could not set Trust value for " + truster + " -> " + trustee + " to " + score + " (" + comment + ")!", wote1);
285 * Update job that retrieves the trust relation between two identities.
287 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
289 private class GetTrustJob extends TrustUpdateJob {
292 * Creates a new trust update job.
295 * The identity giving the trust
297 * The identity receiving the trust
299 public GetTrustJob(OwnIdentity truster, Identity trustee) {
300 super(truster, trustee);
307 @SuppressWarnings("synthetic-access")
310 Trust trust = webOfTrustConnector.getTrust(truster, trustee);
311 if (trustee instanceof DefaultIdentity) {
312 ((DefaultIdentity) trustee).setTrust(truster, trust);
314 } catch (PluginException pe1) {
315 logger.log(Level.WARNING, "Could not get Trust value for " + truster + " -> " + trustee + "!", pe1);