Only the get own identities from WoT when the key management dialog is opened.
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sun, 7 Sep 2014 11:02:52 +0000 (13:02 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sun, 7 Sep 2014 11:02:52 +0000 (13:02 +0200)
src/main/java/de/todesbaum/jsite/application/WebOfTrustInterface.java
src/main/java/de/todesbaum/jsite/main/Main.java

index eeee195..cf5435c 100644 (file)
@@ -18,6 +18,8 @@
 
 package de.todesbaum.jsite.application;
 
+import static java.util.Collections.emptyList;
+
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
@@ -38,7 +40,7 @@ import de.todesbaum.util.freenet.fcp2.wot.OwnIdentity;
  *
  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
  */
-public class WebOfTrustInterface implements Runnable {
+public class WebOfTrustInterface {
 
        /** The logger. */
        private static final Logger logger = Logging.getLogger(WebOfTrustInterface.class);
@@ -46,18 +48,9 @@ public class WebOfTrustInterface implements Runnable {
        /** Unique ID for the command identifier. */
        private static final AtomicLong commandCounter = new AtomicLong(System.nanoTime());
 
-       /** Object used for synchronization. */
-       private final Object syncObject = new Object();
-
        /** The freenet interface. */
        private final Freenet7Interface freenetInterface;
 
-       /** Whether the interface should stop. */
-       private boolean shouldStop;
-
-       /** The own identities. */
-       private final List<OwnIdentity> ownIdentities = new ArrayList<OwnIdentity>();
-
        /**
         * Creates a new web of trust interface.
         *
@@ -79,47 +72,72 @@ public class WebOfTrustInterface implements Runnable {
         * @return The list of own identities
         */
        public List<OwnIdentity> getOwnIdentities() {
-               synchronized (ownIdentities) {
-                       return new ArrayList<OwnIdentity>(ownIdentities);
-               }
-       }
+               try {
+
+                       /* connect. */
+                       Connection connection = freenetInterface.getConnection("jSite-WoT-Connector");
+                       logger.log(Level.INFO, String.format("Trying to connect to node at %s...", freenetInterface.getNode()));
+                       if (!connection.connect()) {
+                               logger.log(Level.WARNING, "Connection failed.");
+                               return emptyList();
+                       }
+                       Client client = new Client(connection);
 
-       //
-       // ACTIONS
-       //
+                       /* send FCP command to WebOfTrust plugin. */
+                       sendFcpCommandToWotPlugin(client);
 
-       /**
-        * Starts the web of trust interface.
-        */
-       public void start() {
-               Thread webOfTrustThread = new Thread(this, "WebOfTrust Interface");
-               webOfTrustThread.start();
-       }
+                       /* read a message. */
+                       Message message = null;
+                       while (!client.isDisconnected() && (message == null)) {
+                               message = client.readMessage(1000);
+                       }
+                       if (message == null) {
+                               return emptyList();
+                       }
 
-       /**
-        * Stops the web of trust interface
-        */
-       public void stop() {
-               synchronized (syncObject) {
-                       shouldStop = true;
-                       syncObject.notifyAll();
+                       /* evaluate message. */
+                       List<OwnIdentity> ownIdentities = parseOwnIdentitiesFromMessage(message);
+
+            /* disconnect. */
+                       logger.log(Level.INFO, "Disconnecting from Node.");
+                       connection.disconnect();
+                       return ownIdentities;
+               } catch (IOException ioe1) {
+                       logger.log(Level.WARNING, String.format("Communication with node at %s failed.", freenetInterface.getNode()), ioe1);
+                       return emptyList();
                }
        }
 
-       //
-       // PRIVATE METHODS
-       //
+       private void sendFcpCommandToWotPlugin(Client client) throws IOException {
+               String messageIdentifier = "jSite-WoT-Command-" + commandCounter.getAndIncrement();
+               FcpPluginMessage pluginMessage = new FcpPluginMessage(messageIdentifier);
+               pluginMessage.setPluginName("plugins.WebOfTrust.WebOfTrust");
+               pluginMessage.setParameter("Message", "GetOwnIdentities");
+               client.execute(pluginMessage);
+       }
 
-       /**
-        * Returns whether the web of trust interface should stop.
-        *
-        * @return {@code true} if the web of trust interface should stop,
-        *         {@code false} otherwise
-        */
-       private boolean shouldStop() {
-               synchronized (syncObject) {
-                       return shouldStop;
+       private List<OwnIdentity> parseOwnIdentitiesFromMessage(Message message) {
+               List<OwnIdentity> ownIdentities = new ArrayList<OwnIdentity>();
+               if (message.getName().equals("FCPPluginReply")) {
+                       logger.log(Level.FINE, "Got matching Reply from WebOfTrust.");
+                               /* parse identities. */
+                       int identityCounter = -1;
+                       while (message.get("Replies.Identity" + ++identityCounter) != null) {
+                               String id = message.get("Replies.Identity" + identityCounter);
+                               String nickname = message.get("Replies.Nickname" + identityCounter);
+                               String requestUri = shortenUri(message.get("Replies.RequestURI" + identityCounter));
+                               String insertUri = shortenUri(message.get("Replies.InsertURI" + identityCounter));
+                               DefaultOwnIdentity ownIdentity = new DefaultOwnIdentity(id, nickname, requestUri, insertUri);
+                               logger.log(Level.FINE, String.format("Parsed Own Identity %s.", ownIdentity));
+                               ownIdentities.add(ownIdentity);
+                       }
+                       logger.log(Level.INFO, String.format("Parsed %d Own Identities.", ownIdentities.size()));
+               } else if ("ProtocolError".equals(message.getName())) {
+                       logger.log(Level.WARNING, "WebOfTrust Plugin not found!");
+               } else if ("Error".equals(message.getName())) {
+                       logger.log(Level.WARNING, "WebOfTrust Plugin returned an error!");
                }
+               return ownIdentities;
        }
 
        /**
@@ -141,98 +159,4 @@ public class WebOfTrustInterface implements Runnable {
                return shortenedUri;
        }
 
-       //
-       // RUNNABLE METHODS
-       //
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public void run() {
-               boolean waitBeforeReconnect = false;
-               while (!shouldStop()) {
-
-                       /* wait a minute before reconnecting for another try. */
-                       if (waitBeforeReconnect) {
-                               logger.log(Level.FINE, "Waiting 60 seconds before reconnecting.");
-                               synchronized (syncObject) {
-                                       try {
-                                               syncObject.wait(60 * 1000);
-                                       } catch (InterruptedException ie1) {
-                                               /* ignore. */
-                                       }
-                               }
-                               if (shouldStop()) {
-                                       continue;
-                               }
-                       } else {
-                               waitBeforeReconnect = true;
-                       }
-
-                       try {
-
-                               /* connect. */
-                               Connection connection = freenetInterface.getConnection("jSite-WoT-Connector");
-                               logger.log(Level.INFO, String.format("Trying to connect to node at %s...", freenetInterface.getNode()));
-                               if (!connection.connect()) {
-                                       logger.log(Level.WARNING, "Connection failed.");
-                                       continue;
-                               }
-                               Client client = new Client(connection);
-
-                               /* send FCP command to WebOfTrust plugin. */
-                               String messageIdentifier = "jSite-WoT-Command-" + commandCounter.getAndIncrement();
-                               FcpPluginMessage pluginMessage = new FcpPluginMessage(messageIdentifier);
-                               pluginMessage.setPluginName("plugins.WebOfTrust.WebOfTrust");
-                               pluginMessage.setParameter("Message", "GetOwnIdentities");
-                               client.execute(pluginMessage);
-
-                               /* read a message. */
-                               Message message = null;
-                               while (!client.isDisconnected() && !shouldStop() && (message == null)) {
-                                       message = client.readMessage(1000);
-                               }
-                               if (message == null) {
-                                       continue;
-                               }
-
-                               /* evaluate message. */
-                               if (message.getName().equals("FCPPluginReply")) {
-                                       logger.log(Level.FINE, "Got matching Reply from WebOfTrust.");
-                                       /* parse identities. */
-                                       List<OwnIdentity> ownIdentities = new ArrayList<OwnIdentity>();
-                                       int identityCounter = -1;
-                                       while (message.get("Replies.Identity" + ++identityCounter) != null) {
-                                               String id = message.get("Replies.Identity" + identityCounter);
-                                               String nickname = message.get("Replies.Nickname" + identityCounter);
-                                               String requestUri = shortenUri(message.get("Replies.RequestURI" + identityCounter));
-                                               String insertUri = shortenUri(message.get("Replies.InsertURI" + identityCounter));
-                                               DefaultOwnIdentity ownIdentity = new DefaultOwnIdentity(id, nickname, requestUri, insertUri);
-                                               logger.log(Level.FINE, String.format("Parsed Own Identity %s.", ownIdentity));
-                                               ownIdentities.add(ownIdentity);
-                                       }
-                                       logger.log(Level.INFO, String.format("Parsed %d Own Identities.", ownIdentities.size()));
-
-                                       synchronized (this.ownIdentities) {
-                                               this.ownIdentities.clear();
-                                               this.ownIdentities.addAll(ownIdentities);
-                                       }
-                               } else if ("ProtocolError".equals(message.getName())) {
-                                       logger.log(Level.WARNING, "WebOfTrust Plugin not found!");
-                               } else if ("Error".equals(message.getName())) {
-                                       logger.log(Level.WARNING, "WebOfTrust Plugin returned an error!");
-                               }
-
-                               /* disconnect. */
-                               logger.log(Level.INFO, "Disconnecting from Node.");
-                               connection.disconnect();
-
-                       } catch (IOException ioe1) {
-                               logger.log(Level.WARNING, String.format("Communication with node at %s failed.", freenetInterface.getNode()), ioe1);
-                       }
-
-               }
-       }
-
 }
index 76866cf..5536543 100644 (file)
@@ -195,7 +195,6 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                updateChecker.start();
 
                webOfTrustInterface = new WebOfTrustInterface(freenetInterface);
-               webOfTrustInterface.start();
 
                initPages();
                showPage(PageType.PAGE_PROJECTS);
@@ -506,7 +505,6 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
         */
        private void quit() {
                updateChecker.stop();
-               webOfTrustInterface.stop();
                System.exit(0);
        }