X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Ffcp%2Fhighlevel%2FHighLevelClient.java;h=2c3ca7e4419e24de9bfdbb9b0714d9dcf694d76c;hb=fdef33f54a71c55f3c3af626a5daf4c2ae517471;hp=6ab962bfd7b4eec5a0b7033a5097cf8834bf3cda;hpb=93f4e8ef160a101863eb57beae7c84b381fa4bba;p=jFCPlib.git diff --git a/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java b/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java index 6ab962b..2c3ca7e 100644 --- a/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java +++ b/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java @@ -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 connectCallback; + /** Mapping from request identifiers to callbacks. */ + private Map> keyGenerationCallbacks = Collections.synchronizedMap(new HashMap>()); + /** * Creates a new high-level client that connects to a node on * localhost. @@ -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 generateKey() throws IOException { + String identifier = generateIdentifier("generateSSK"); + GenerateSSK generateSSK = new GenerateSSK(identifier); + HighLevelCallback keyGenerationCallback = new HighLevelCallback(); + 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 <bombe@freenetproject.org> @@ -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 keyGenerationCallback = keyGenerationCallbacks.remove(sskKeypair.getIdentifier()); + if (keyGenerationCallback == null) { + return; + } + KeyGenerationResult keyGenerationResult = new KeyGenerationResult(); + keyGenerationResult.setInsertURI(sskKeypair.getInsertURI()); + keyGenerationResult.setRequestURI(sskKeypair.getRequestURI()); + keyGenerationCallback.setResult(keyGenerationResult); } /**