From f8f89b060a9d65fcfa430930fb22e7cff80c62b8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 15 Jul 2015 07:14:12 +0200 Subject: [PATCH] Add command that modifies the config of the node --- .../fcp/quelaton/DefaultFcpClient.java | 5 ++ .../net/pterodactylus/fcp/quelaton/FcpClient.java | 1 + .../fcp/quelaton/ModifyConfigCommand.java | 20 ++++++ .../fcp/quelaton/ModifyConfigCommandImpl.java | 80 ++++++++++++++++++++++ .../fcp/quelaton/DefaultFcpClientTest.java | 21 ++++++ 5 files changed, 127 insertions(+) create mode 100644 src/main/java/net/pterodactylus/fcp/quelaton/ModifyConfigCommand.java create mode 100644 src/main/java/net/pterodactylus/fcp/quelaton/ModifyConfigCommandImpl.java diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java index e366142..4cc7328 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java @@ -60,6 +60,11 @@ public class DefaultFcpClient implements FcpClient { } @Override + public ModifyConfigCommand modifyConfig() { + return new ModifyConfigCommandImpl(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 f943083..ab25d61 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java @@ -9,6 +9,7 @@ public interface FcpClient { GetNodeCommand getNode(); GetConfigCommand getConfig(); + ModifyConfigCommand modifyConfig(); GenerateKeypairCommand generateKeypair(); ClientGetCommand clientGet(); diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ModifyConfigCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/ModifyConfigCommand.java new file mode 100644 index 0000000..6d718d7 --- /dev/null +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ModifyConfigCommand.java @@ -0,0 +1,20 @@ +package net.pterodactylus.fcp.quelaton; + +import net.pterodactylus.fcp.ConfigData; + +/** + * Command to modify the configuration of a node. + * + * @author David ‘Bombe’ Roden + */ +public interface ModifyConfigCommand extends Executable { + + WithValue set(String key); + + interface WithValue { + + ModifyConfigCommand to(String value); + + } + +} diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ModifyConfigCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ModifyConfigCommandImpl.java new file mode 100644 index 0000000..3de75c2 --- /dev/null +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ModifyConfigCommandImpl.java @@ -0,0 +1,80 @@ +package net.pterodactylus.fcp.quelaton; + +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; + +import net.pterodactylus.fcp.ConfigData; +import net.pterodactylus.fcp.ModifyConfig; + +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.common.util.concurrent.MoreExecutors; + +/** + * Default {@link ModifyConfigCommand} implementation based on {@link FcpDialog}. + * + * @author David ‘Bombe’ Roden + */ +public class ModifyConfigCommandImpl implements ModifyConfigCommand { + + private final ModifyConfig modifyConfig = new ModifyConfig(new RandomIdentifierGenerator().generate()); + private final ListeningExecutorService threadPool; + private final ConnectionSupplier connectionSupplier; + + public ModifyConfigCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) { + this.threadPool = MoreExecutors.listeningDecorator(threadPool); + this.connectionSupplier = connectionSupplier; + } + + @Override + public WithValue set(String key) { + return value -> to(key, value); + } + + private ModifyConfigCommand to(String key, String value) { + modifyConfig.setOption(key, value); + return this; + } + + @Override + public ListenableFuture execute() { + return threadPool.submit(this::executeDialog); + } + + private ConfigData executeDialog() throws IOException, ExecutionException, InterruptedException { + try (ModifyConfigDialog modifyConfigDialog = new ModifyConfigDialog()) { + return modifyConfigDialog.send(modifyConfig).get(); + } + } + + private class ModifyConfigDialog extends FcpDialog { + + private final AtomicBoolean finished = new AtomicBoolean(); + private final AtomicReference configData = new AtomicReference<>(); + + public ModifyConfigDialog() throws IOException { + super(threadPool, connectionSupplier.get()); + } + + @Override + protected boolean isFinished() { + return finished.get(); + } + + @Override + protected ConfigData getResult() { + return configData.get(); + } + + @Override + protected void consumeConfigData(ConfigData configData) { + this.configData.set(configData); + finished.set(true); + } + + } + +} diff --git a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java index 88f25a8..3ea9570 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java @@ -1964,4 +1964,25 @@ public class DefaultFcpClientTest { assertThat(configData.get().getDataType("foo"), is("number")); } + @Test + public void defaultFcpClientCanModifyConfigData() throws InterruptedException, ExecutionException, IOException { + Future newConfigData = fcpClient.modifyConfig().set("foo.bar").to("baz").execute(); + connectNode(); + List lines = fcpServer.collectUntil(is("EndMessage")); + String identifier = extractIdentifier(lines); + assertThat(lines, matchesFcpMessage( + "ModifyConfig", + "Identifier=" + identifier, + "foo.bar=baz", + "EndMessage" + )); + fcpServer.writeLine( + "ConfigData", + "Identifier=" + identifier, + "current.foo.bar=baz", + "EndMessage" + ); + assertThat(newConfigData.get().getCurrent("foo.bar"), is("baz")); + } + } -- 2.7.4