add key generation
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 14 Apr 2008 21:50:53 +0000 (21:50 +0000)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 14 Apr 2008 21:50:53 +0000 (21:50 +0000)
git-svn-id: http://trooper/svn/projects/jFCPlib/branch/high-level-client@818 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/fcp/highlevel/ConnectResult.java
src/net/pterodactylus/fcp/highlevel/HighLevelClient.java
src/net/pterodactylus/fcp/highlevel/HighLevelResult.java
src/net/pterodactylus/fcp/highlevel/KeyGenerationResult.java [new file with mode: 0644]

index c0f3b68..d922292 100644 (file)
@@ -25,7 +25,7 @@ package net.pterodactylus.fcp.highlevel;
  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
  * @version $Id$
  */
-public class ConnectResult implements HighLevelResult {
+public class ConnectResult extends HighLevelResult {
 
        /** Whether the node is now connected. */
        private boolean connected;
index 6ab962b..2c3ca7e 100644 (file)
@@ -22,6 +22,9 @@ package net.pterodactylus.fcp.highlevel;
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
 
 import net.pterodactylus.fcp.AllData;
 import net.pterodactylus.fcp.ClientHello;
@@ -36,6 +39,7 @@ import net.pterodactylus.fcp.FcpConnection;
 import net.pterodactylus.fcp.FcpListener;
 import net.pterodactylus.fcp.FcpMessage;
 import net.pterodactylus.fcp.FinishedCompression;
+import net.pterodactylus.fcp.GenerateSSK;
 import net.pterodactylus.fcp.GetFailed;
 import net.pterodactylus.fcp.IdentifierCollision;
 import net.pterodactylus.fcp.NodeData;
@@ -93,6 +97,9 @@ public class HighLevelClient {
        /** The callback for {@link #connect()}. */
        private HighLevelCallback<ConnectResult> connectCallback;
 
+       /** Mapping from request identifiers to callbacks. */
+       private Map<String, HighLevelCallback<KeyGenerationResult>> keyGenerationCallbacks = Collections.synchronizedMap(new HashMap<String, HighLevelCallback<KeyGenerationResult>>());
+
        /**
         * Creates a new high-level client that connects to a node on
         * <code>localhost</code>.
@@ -186,6 +193,33 @@ public class HighLevelClient {
        }
 
        /**
+        * Generates a new SSK keypair.
+        * 
+        * @return A callback with the keypair
+        * @throws IOException
+        *             if an I/O error occurs communicating with the node
+        */
+       public HighLevelCallback<KeyGenerationResult> generateKey() throws IOException {
+               String identifier = generateIdentifier("generateSSK");
+               GenerateSSK generateSSK = new GenerateSSK(identifier);
+               HighLevelCallback<KeyGenerationResult> keyGenerationCallback = new HighLevelCallback<KeyGenerationResult>();
+               keyGenerationCallbacks.put(identifier, keyGenerationCallback);
+               fcpConnection.sendMessage(generateSSK);
+               return keyGenerationCallback;
+       }
+
+       /**
+        * Generates an identifier for the given function.
+        * 
+        * @param function
+        *            The name of the function
+        * @return An identifier
+        */
+       private String generateIdentifier(String function) {
+               return "jFCPlib-" + function + "-" + System.currentTimeMillis();
+       }
+
+       /**
         * FCP listener for {@link HighLevelClient}.
         * 
         * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
@@ -400,7 +434,19 @@ public class HighLevelClient {
                 * @see net.pterodactylus.fcp.FcpListener#receivedSSKKeypair(net.pterodactylus.fcp.FcpConnection,
                 *      net.pterodactylus.fcp.SSKKeypair)
                 */
+               @SuppressWarnings("synthetic-access")
                public void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) {
+                       if (fcpConnection != HighLevelClient.this.fcpConnection) {
+                               return;
+                       }
+                       HighLevelCallback<KeyGenerationResult> keyGenerationCallback = keyGenerationCallbacks.remove(sskKeypair.getIdentifier());
+                       if (keyGenerationCallback == null) {
+                               return;
+                       }
+                       KeyGenerationResult keyGenerationResult = new KeyGenerationResult();
+                       keyGenerationResult.setInsertURI(sskKeypair.getInsertURI());
+                       keyGenerationResult.setRequestURI(sskKeypair.getRequestURI());
+                       keyGenerationCallback.setResult(keyGenerationResult);
                }
 
                /**
index b61036e..3be5b2c 100644 (file)
 package net.pterodactylus.fcp.highlevel;
 
 /**
- * Marker interface for results of {@link HighLevelClient} operations.
+ * Base class for results of {@link HighLevelClient} operations.
  * 
  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
  * @version $Id$
  */
-public interface HighLevelResult {
+public abstract class HighLevelResult {
 
-       /* intentionally left blank. */
+       /** Whether the operation failed. */
+       private boolean failed;
+
+       /**
+        * Returns whether the operation failed.
+        * 
+        * @return <code>true</code> if the operation failed, <code>false</code>
+        *         otherwise
+        */
+       public boolean isFailed() {
+               return failed;
+       }
+
+       /**
+        * Sets whether the operation failed.
+        * 
+        * @param failed
+        *            <code>true</code> if the operation failed,
+        *            <code>false</code> otherwise
+        */
+       void setFailed(boolean failed) {
+               this.failed = failed;
+       }
 
 }
diff --git a/src/net/pterodactylus/fcp/highlevel/KeyGenerationResult.java b/src/net/pterodactylus/fcp/highlevel/KeyGenerationResult.java
new file mode 100644 (file)
index 0000000..eeaa4a9
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * jFCPlib-high-level-client - KeyGenerationResult.java -
+ * Copyright © 2008 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 2 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package net.pterodactylus.fcp.highlevel;
+
+/**
+ * Result of a {@link HighLevelClient#generateKey()} operation.
+ * 
+ * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
+ * @version $Id$
+ */
+public class KeyGenerationResult extends HighLevelResult {
+
+       /** The insert URI. */
+       private String insertURI;
+
+       /** The request URI. */
+       private String requestURI;
+
+       /**
+        * Package-private constructor.
+        */
+       KeyGenerationResult() {
+       }
+
+       /**
+        * Returns the insert URI.
+        * 
+        * @return The insert URI
+        */
+       public String getInsertURI() {
+               return insertURI;
+       }
+
+       /**
+        * Sets the insert URI.
+        * 
+        * @param insertURI
+        *            The insert URI
+        */
+       void setInsertURI(String insertURI) {
+               this.insertURI = insertURI;
+       }
+
+       /**
+        * Returns the request URI.
+        * 
+        * @return The request URI
+        */
+       public String getRequestURI() {
+               return requestURI;
+       }
+
+       /**
+        * Sets the request URI.
+        * 
+        * @param requestURI
+        *            The request URI
+        */
+       void setRequestURI(String requestURI) {
+               this.requestURI = requestURI;
+       }
+
+}