From: David ‘Bombe’ Roden Date: Wed, 12 Sep 2012 07:14:08 +0000 (+0200) Subject: Merge remote-tracking branch 'beak/next' into next X-Git-Tag: 0.8.3^2~39 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=d1e011bc62bede509d8482dfeea950823b974413;hp=77e1ba0c544701f3291f010808dd6e9c3e248054 Merge remote-tracking branch 'beak/next' into next --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index e3fdd19..9e1e6c6 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -53,7 +53,6 @@ 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; @@ -1042,26 +1041,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 @@ -1987,11 +1966,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 soneInserter : soneInserters.entrySet()) { + soneInserter.getValue().removeSoneInsertListener(this); + soneInserter.getValue().stop(); + saveSone(soneInserter.getKey()); } } + saveConfiguration(); webOfTrustUpdater.stop(); updateChecker.stop(); updateChecker.removeUpdateListener(this); @@ -2369,7 +2350,7 @@ 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; diff --git a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java index cb73297..bdfc1ae 100644 --- a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java +++ b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java @@ -70,28 +70,6 @@ public class WebOfTrustUpdater extends AbstractService { // /** - * 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. @@ -471,45 +449,6 @@ public class WebOfTrustUpdater extends AbstractService { } /** - * 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); - } - - /** - * {@inheritDoc} - */ - @Override - @SuppressWarnings("synthetic-access") - public void run() { - try { - Trust trust = webOfTrustConnector.getTrust(truster, trustee); - if (trustee instanceof DefaultIdentity) { - ((DefaultIdentity) trustee).setTrust(truster, trust); - } - finish(true); - } catch (PluginException pe1) { - logger.log(Level.WARNING, "Could not get Trust value for " + truster + " -> " + trustee + "!", pe1); - finish(false); - } - } - - } - - /** * Base class for context updates of an {@link OwnIdentity}. * * @author David ‘Bombe’ Roden @@ -684,8 +623,8 @@ public class WebOfTrustUpdater extends AbstractService { if ((object == null) || !object.getClass().equals(getClass())) { return false; } - WebOfTrustContextUpdateJob updateJob = (WebOfTrustContextUpdateJob) object; - return updateJob.ownIdentity.equals(ownIdentity) && updateJob.context.equals(propertyName); + WebOfTrustPropertyUpdateJob updateJob = (WebOfTrustPropertyUpdateJob) object; + return updateJob.ownIdentity.equals(ownIdentity) && updateJob.propertyName.equals(propertyName); } /** diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java index 8e0b124..3c3c9f9 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java @@ -19,6 +19,7 @@ package net.pterodactylus.sone.freenet.wot; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Map.Entry; import java.util.Set; @@ -26,8 +27,6 @@ import java.util.logging.Level; import java.util.logging.Logger; import net.pterodactylus.sone.freenet.plugin.PluginException; -import net.pterodactylus.util.collection.mapper.Mapper; -import net.pterodactylus.util.collection.mapper.Mappers; import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.service.AbstractService; @@ -155,27 +154,7 @@ public class IdentityManager extends AbstractService { * @return All own identities */ public Set getAllOwnIdentities() { - try { - Set ownIdentities = webOfTrustConnector.loadAllOwnIdentities(); - Map newOwnIdentities = new HashMap(); - for (OwnIdentity ownIdentity : ownIdentities) { - newOwnIdentities.put(ownIdentity.getId(), ownIdentity); - } - checkOwnIdentities(newOwnIdentities); - return Mappers.mappedSet(ownIdentities, new Mapper() { - - /** - * {@inheritDoc} - */ - @Override - public OwnIdentity map(OwnIdentity input) { - return new DefaultOwnIdentity(input); - } - }); - } catch (WebOfTrustException wote1) { - logger.log(Level.WARNING, "Could not load all own identities!", wote1); - return Collections.emptySet(); - } + return new HashSet(currentOwnIdentities.values()); } // diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java b/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java index 435f078..531275d 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java @@ -21,6 +21,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.concurrent.atomic.AtomicLong; import java.util.logging.Level; import java.util.logging.Logger; @@ -28,6 +29,7 @@ import net.pterodactylus.sone.freenet.plugin.ConnectorListener; import net.pterodactylus.sone.freenet.plugin.PluginConnector; import net.pterodactylus.sone.freenet.plugin.PluginException; import net.pterodactylus.util.logging.Logging; +import net.pterodactylus.util.number.Numbers; import freenet.support.SimpleFieldSet; import freenet.support.api.Bucket; @@ -36,7 +38,7 @@ import freenet.support.api.Bucket; * * @author David ‘Bombe’ Roden */ -public class WebOfTrustConnector implements ConnectorListener { +public class WebOfTrustConnector { /** The logger. */ private static final Logger logger = Logging.getLogger(WebOfTrustConnector.class); @@ -44,11 +46,8 @@ public class WebOfTrustConnector implements ConnectorListener { /** The name of the WoT plugin. */ private static final String WOT_PLUGIN_NAME = "plugins.WebOfTrust.WebOfTrust"; - /** A random connection identifier. */ - private static final String PLUGIN_CONNECTION_IDENTIFIER = "Sone-WoT-Connector-" + Math.abs(Math.random()); - - /** The current reply. */ - private Reply reply; + /** Counter for connection identifiers. */ + private final AtomicLong counter = new AtomicLong(); /** The plugin connector. */ private final PluginConnector pluginConnector; @@ -62,7 +61,6 @@ public class WebOfTrustConnector implements ConnectorListener { */ public WebOfTrustConnector(PluginConnector pluginConnector) { this.pluginConnector = pluginConnector; - pluginConnector.addConnectorListener(WOT_PLUGIN_NAME, PLUGIN_CONNECTION_IDENTIFIER, this); } // @@ -73,10 +71,7 @@ public class WebOfTrustConnector implements ConnectorListener { * Stops the web of trust connector. */ public void stop() { - pluginConnector.removeConnectorListener(WOT_PLUGIN_NAME, PLUGIN_CONNECTION_IDENTIFIER, this); - synchronized (reply) { - reply.notifyAll(); - } + /* does nothing. */ } /** @@ -134,7 +129,7 @@ public class WebOfTrustConnector implements ConnectorListener { * if an error occured talking to the Web of Trust plugin */ public Set loadTrustedIdentities(OwnIdentity ownIdentity, String context) throws PluginException { - Reply reply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.getId()).put("Selection", "+").put("Context", (context == null) ? "" : context).get()); + Reply reply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.getId()).put("Selection", "+").put("Context", (context == null) ? "" : context).put("WantTrustValues", "true").get()); SimpleFieldSet fields = reply.getFields(); Set identities = new HashSet(); int identityCounter = -1; @@ -148,6 +143,10 @@ public class WebOfTrustConnector implements ConnectorListener { DefaultIdentity identity = new DefaultIdentity(id, nickname, requestUri); identity.setContexts(parseContexts("Contexts" + identityCounter + ".", fields)); identity.setProperties(parseProperties("Properties" + identityCounter + ".", fields)); + Integer trust = Numbers.safeParseInteger(fields.get("Trust" + identityCounter), null); + int score = Numbers.safeParseInteger(fields.get("Score" + identityCounter)); + int rank = Numbers.safeParseInteger(fields.get("Rank" + identityCounter)); + identity.setTrust(ownIdentity, new Trust(trust, score, rank)); identities.add(identity); } return identities; @@ -318,7 +317,7 @@ public class WebOfTrustConnector implements ConnectorListener { * The fields to parse the contexts from * @return The parsed contexts */ - private Set parseContexts(String prefix, SimpleFieldSet fields) { + private static Set parseContexts(String prefix, SimpleFieldSet fields) { Set contexts = new HashSet(); int contextCounter = -1; while (true) { @@ -340,7 +339,7 @@ public class WebOfTrustConnector implements ConnectorListener { * The fields to parse the properties from * @return The parsed properties */ - private Map parseProperties(String prefix, SimpleFieldSet fields) { + private static Map parseProperties(String prefix, SimpleFieldSet fields) { Map properties = new HashMap(); int propertiesCounter = -1; while (true) { @@ -380,17 +379,34 @@ public class WebOfTrustConnector implements ConnectorListener { * @throws PluginException * if the request could not be sent */ - private synchronized Reply performRequest(SimpleFieldSet fields, Bucket data) throws PluginException { - reply = new Reply(); + private Reply performRequest(SimpleFieldSet fields, Bucket data) throws PluginException { + final String identifier = "FCP-Command-" + System.currentTimeMillis() + "-" + counter.getAndIncrement(); + final Reply reply = new Reply(); logger.log(Level.FINE, String.format("Sending FCP Request: %s", fields.get("Message"))); + ConnectorListener connectorListener = new ConnectorListener() { + + @Override + @SuppressWarnings("synthetic-access") + public void receivedReply(PluginConnector pluginConnector, SimpleFieldSet fields, Bucket data) { + String messageName = fields.get("Message"); + logger.log(Level.FINEST, String.format("Received Reply from Plugin: %s", messageName)); + synchronized (reply) { + reply.setFields(fields); + reply.setData(data); + reply.notify(); + } + } + }; + pluginConnector.addConnectorListener(WOT_PLUGIN_NAME, identifier, connectorListener); synchronized (reply) { - pluginConnector.sendRequest(WOT_PLUGIN_NAME, PLUGIN_CONNECTION_IDENTIFIER, fields, data); + pluginConnector.sendRequest(WOT_PLUGIN_NAME, identifier, fields, data); try { reply.wait(); } catch (InterruptedException ie1) { logger.log(Level.WARNING, String.format("Got interrupted while waiting for reply on %s.", fields.get("Message")), ie1); } } + pluginConnector.removeConnectorListener(WOT_PLUGIN_NAME, identifier, connectorListener); logger.log(Level.FINEST, String.format("Received FCP Response for %s: %s", fields.get("Message"), (reply.getFields() != null) ? reply.getFields().get("Message") : null)); if ((reply.getFields() == null) || "Error".equals(reply.getFields().get("Message"))) { throw new PluginException("Could not perform request for " + fields.get("Message")); @@ -398,24 +414,6 @@ public class WebOfTrustConnector implements ConnectorListener { return reply; } - // - // INTERFACE ConnectorListener - // - - /** - * {@inheritDoc} - */ - @Override - public void receivedReply(PluginConnector pluginConnector, SimpleFieldSet fields, Bucket data) { - String messageName = fields.get("Message"); - logger.log(Level.FINEST, String.format("Received Reply from Plugin: %s", messageName)); - synchronized (reply) { - reply.setFields(fields); - reply.setData(data); - reply.notify(); - } - } - /** * Container for the data of the reply from a plugin. * diff --git a/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java b/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java index 1f29b65..f004967 100644 --- a/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java @@ -21,6 +21,7 @@ import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.Sone.ShowCustomAvatars; +import net.pterodactylus.sone.freenet.wot.OwnIdentity; import net.pterodactylus.sone.freenet.wot.Trust; import net.pterodactylus.util.template.Accessor; import net.pterodactylus.util.template.ReflectionAccessor; @@ -83,7 +84,7 @@ public class ProfileAccessor extends ReflectionAccessor { if (showCustomAvatars == ShowCustomAvatars.FOLLOWED) { return currentSone.hasFriend(remoteSone.getId()) ? avatarId : null; } - Trust trust = core.getTrust(currentSone, remoteSone); + Trust trust = remoteSone.getIdentity().getTrust((OwnIdentity) currentSone.getIdentity()); if (trust == null) { return null; } diff --git a/src/main/java/net/pterodactylus/sone/template/SoneAccessor.java b/src/main/java/net/pterodactylus/sone/template/SoneAccessor.java index eac8a93..eca8654 100644 --- a/src/main/java/net/pterodactylus/sone/template/SoneAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/SoneAccessor.java @@ -24,6 +24,7 @@ import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.Sone.SoneStatus; +import net.pterodactylus.sone.freenet.wot.OwnIdentity; import net.pterodactylus.sone.freenet.wot.Trust; import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage; @@ -106,7 +107,7 @@ public class SoneAccessor extends ReflectionAccessor { if (currentSone == null) { return null; } - Trust trust = core.getTrust(currentSone, sone); + Trust trust = sone.getIdentity().getTrust((OwnIdentity) currentSone.getIdentity()); logger.log(Level.FINEST, String.format("Trust for %s by %s: %s", sone, currentSone, trust)); if (trust == null) { return new Trust(null, null, null);