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;
}
@Override
+ public GetConfigCommand getConfig() {
+ return new GetConfigCommandImpl(threadPool, this::connect);
+ }
+
+ @Override
public GenerateKeypairCommand generateKeypair() {
return new GenerateKeypairCommandImpl(threadPool, this::connect);
}
public interface FcpClient {
GetNodeCommand getNode();
+ GetConfigCommand getConfig();
+
GenerateKeypairCommand generateKeypair();
ClientGetCommand clientGet();
ClientPutCommand clientPut();
--- /dev/null
+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> {
+
+}
--- /dev/null
+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);
+ }
+
+ }
+
+}
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;
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());
+ }
+
}