From b94011134612003c596b9f8bf1b4a9d618c54d8d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 14 Jul 2015 21:54:39 +0200 Subject: [PATCH 1/1] =?utf8?q?Add=20command=20to=20retrieve=20the=20node?= =?utf8?q?=E2=80=99s=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../fcp/quelaton/DefaultFcpClient.java | 7 ++- .../net/pterodactylus/fcp/quelaton/FcpClient.java | 2 + .../fcp/quelaton/GetConfigCommand.java | 12 ++++ .../fcp/quelaton/GetConfigCommandImpl.java | 67 ++++++++++++++++++++++ .../fcp/quelaton/DefaultFcpClientTest.java | 21 +++++++ 5 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 src/main/java/net/pterodactylus/fcp/quelaton/GetConfigCommand.java create mode 100644 src/main/java/net/pterodactylus/fcp/quelaton/GetConfigCommandImpl.java 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()); + } + } -- 2.7.4