1 package net.pterodactylus.fcp.quelaton;
3 import java.io.IOException;
4 import java.util.concurrent.ExecutionException;
5 import java.util.concurrent.ExecutorService;
6 import java.util.concurrent.atomic.AtomicReference;
8 import net.pterodactylus.fcp.ConfigData;
9 import net.pterodactylus.fcp.GetConfig;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import com.google.common.util.concurrent.ListeningExecutorService;
13 import com.google.common.util.concurrent.MoreExecutors;
16 * Default {@link GetConfigCommand} implementation based on {@link FcpDialog}.
18 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
20 public class GetConfigCommandImpl implements GetConfigCommand {
22 private final ListeningExecutorService threadPool;
23 private final ConnectionSupplier connectionSupplier;
25 public GetConfigCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
26 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
27 this.connectionSupplier = connectionSupplier;
31 public ListenableFuture<ConfigData> execute() {
32 return threadPool.submit(this::executeDialog);
35 private ConfigData executeDialog() throws IOException, ExecutionException, InterruptedException {
36 GetConfig getConfig = new GetConfig(new RandomIdentifierGenerator().generate());
37 try (GetConfigDialog getConfigDialog = new GetConfigDialog()) {
38 return getConfigDialog.send(getConfig).get();
42 private class GetConfigDialog extends FcpDialog<ConfigData> {
44 private final AtomicReference<ConfigData> configData = new AtomicReference<>();
46 public GetConfigDialog() throws IOException {
47 super(threadPool, connectionSupplier.get());
51 protected boolean isFinished() {
52 return configData.get() != null;
56 protected ConfigData getResult() {
57 return configData.get();
61 protected void consumeConfigData(ConfigData configData) {
62 this.configData.set(configData);