--- /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.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);
+ }
+
+ }
+
+}
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"));
+ }
+
}