Add command that modifies the config of the node
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 15 Jul 2015 05:14:12 +0000 (07:14 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 15 Jul 2015 05:15:08 +0000 (07:15 +0200)
src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java
src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java
src/main/java/net/pterodactylus/fcp/quelaton/ModifyConfigCommand.java [new file with mode: 0644]
src/main/java/net/pterodactylus/fcp/quelaton/ModifyConfigCommandImpl.java [new file with mode: 0644]
src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java

index e366142..4cc7328 100644 (file)
@@ -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);
        }
index f943083..ab25d61 100644 (file)
@@ -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 (file)
index 0000000..6d718d7
--- /dev/null
@@ -0,0 +1,20 @@
+package net.pterodactylus.fcp.quelaton;
+
+import net.pterodactylus.fcp.ConfigData;
+
+/**
+ * Command to modify the configuration of a node.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public interface ModifyConfigCommand extends Executable<ConfigData> {
+
+       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 (file)
index 0000000..3de75c2
--- /dev/null
@@ -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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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<ConfigData> 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<ConfigData> {
+
+               private final AtomicBoolean finished = new AtomicBoolean();
+               private final AtomicReference<ConfigData> 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);
+               }
+
+       }
+
+}
index 88f25a8..3ea9570 100644 (file)
@@ -1964,4 +1964,25 @@ public class DefaultFcpClientTest {
                assertThat(configData.get().getDataType("foo"), is("number"));
        }
 
+       @Test
+       public void defaultFcpClientCanModifyConfigData() throws InterruptedException, ExecutionException, IOException {
+               Future<ConfigData> newConfigData = fcpClient.modifyConfig().set("foo.bar").to("baz").execute();
+               connectNode();
+               List<String> 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"));
+       }
+
 }