From: David ‘Bombe’ Roden Date: Tue, 14 Jul 2015 19:54:39 +0000 (+0200) Subject: Add command to retrieve the node’s config X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=b94011134612003c596b9f8bf1b4a9d618c54d8d;p=jFCPlib.git Add command to retrieve the node’s config --- diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java index 1fc03bb..e366142 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java @@ -1,14 +1,12 @@ package net.pterodactylus.fcp.quelaton; import java.io.IOException; -import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; import net.pterodactylus.fcp.FcpConnection; -import net.pterodactylus.fcp.Peer; import com.google.common.util.concurrent.ListeningExecutorService; import com.google.common.util.concurrent.MoreExecutors; @@ -57,6 +55,11 @@ public class DefaultFcpClient implements FcpClient { } @Override + public GetConfigCommand getConfig() { + return new GetConfigCommandImpl(threadPool, this::connect); + } + + @Override public GenerateKeypairCommand generateKeypair() { return new GenerateKeypairCommandImpl(threadPool, this::connect); } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java b/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java index dbd35f2..f943083 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java @@ -8,6 +8,8 @@ package net.pterodactylus.fcp.quelaton; public interface FcpClient { GetNodeCommand getNode(); + GetConfigCommand getConfig(); + GenerateKeypairCommand generateKeypair(); ClientGetCommand clientGet(); ClientPutCommand clientPut(); diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/GetConfigCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/GetConfigCommand.java new file mode 100644 index 0000000..2f36436 --- /dev/null +++ b/src/main/java/net/pterodactylus/fcp/quelaton/GetConfigCommand.java @@ -0,0 +1,12 @@ +package net.pterodactylus.fcp.quelaton; + +import net.pterodactylus.fcp.ConfigData; + +/** + * Command that retrieves the node’s configuration. + * + * @author David ‘Bombe’ Roden + */ +public interface GetConfigCommand extends Executable { + +} diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/GetConfigCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/GetConfigCommandImpl.java new file mode 100644 index 0000000..2876661 --- /dev/null +++ b/src/main/java/net/pterodactylus/fcp/quelaton/GetConfigCommandImpl.java @@ -0,0 +1,67 @@ +package net.pterodactylus.fcp.quelaton; + +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.atomic.AtomicReference; + +import net.pterodactylus.fcp.ConfigData; +import net.pterodactylus.fcp.GetConfig; + +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.common.util.concurrent.MoreExecutors; + +/** + * Default {@link GetConfigCommand} implementation based on {@link FcpDialog}. + * + * @author David ‘Bombe’ Roden + */ +public class GetConfigCommandImpl implements GetConfigCommand { + + private final ListeningExecutorService threadPool; + private final ConnectionSupplier connectionSupplier; + + public GetConfigCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) { + this.threadPool = MoreExecutors.listeningDecorator(threadPool); + this.connectionSupplier = connectionSupplier; + } + + @Override + public ListenableFuture execute() { + return threadPool.submit(this::executeDialog); + } + + private ConfigData executeDialog() throws IOException, ExecutionException, InterruptedException { + GetConfig getConfig = new GetConfig(new RandomIdentifierGenerator().generate()); + try (GetConfigDialog getConfigDialog = new GetConfigDialog()) { + return getConfigDialog.send(getConfig).get(); + } + } + + private class GetConfigDialog extends FcpDialog { + + private final AtomicReference configData = new AtomicReference<>(); + + public GetConfigDialog() throws IOException { + super(threadPool, connectionSupplier.get()); + } + + @Override + protected boolean isFinished() { + return configData.get() != null; + } + + @Override + protected ConfigData getResult() { + return configData.get(); + } + + @Override + protected void consumeConfigData(ConfigData configData) { + this.configData.set(configData); + } + + } + +} diff --git a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java index fbfff9e..664588c 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java @@ -25,6 +25,7 @@ import java.util.concurrent.Future; import java.util.stream.Collectors; import net.pterodactylus.fcp.ARK; +import net.pterodactylus.fcp.ConfigData; import net.pterodactylus.fcp.DSAGroup; import net.pterodactylus.fcp.FcpKeyPair; import net.pterodactylus.fcp.Key; @@ -1767,4 +1768,24 @@ public class DefaultFcpClientTest { assertThat(noteUpdated.get(), is(true)); } + @Test + public void defaultFcpClientCanGetConfigWithoutDetails() + throws InterruptedException, ExecutionException, IOException { + Future configData = fcpClient.getConfig().execute(); + connectNode(); + List lines = fcpServer.collectUntil(is("EndMessage")); + String identifier = extractIdentifier(lines); + assertThat(lines, matchesFcpMessage( + "GetConfig", + "Identifier=" + identifier, + "EndMessage" + )); + fcpServer.writeLine( + "ConfigData", + "Identifier=" + identifier, + "EndMessage" + ); + assertThat(configData.get(), notNullValue()); + } + }