Add command to retrieve the node’s config
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Tue, 14 Jul 2015 19:54:39 +0000 (21:54 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Tue, 14 Jul 2015 19:54:39 +0000 (21:54 +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/GetConfigCommand.java [new file with mode: 0644]
src/main/java/net/pterodactylus/fcp/quelaton/GetConfigCommandImpl.java [new file with mode: 0644]
src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java

index 1fc03bb..e366142 100644 (file)
@@ -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);
        }
index dbd35f2..f943083 100644 (file)
@@ -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 (file)
index 0000000..2f36436
--- /dev/null
@@ -0,0 +1,12 @@
+package net.pterodactylus.fcp.quelaton;
+
+import net.pterodactylus.fcp.ConfigData;
+
+/**
+ * Command that retrieves the node’s configuration.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public interface GetConfigCommand extends Executable<ConfigData> {
+
+}
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 (file)
index 0000000..2876661
--- /dev/null
@@ -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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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<ConfigData> 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<ConfigData> {
+
+               private final AtomicReference<ConfigData> 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);
+               }
+
+       }
+
+}
index fbfff9e..664588c 100644 (file)
@@ -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> configData = fcpClient.getConfig().execute();
+               connectNode();
+               List<String> 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());
+       }
+
 }