From: David ‘Bombe’ Roden Date: Thu, 15 Oct 2015 19:20:25 +0000 (+0200) Subject: Refactor FCP dialog X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=c7b30c245806a4a67d2e924d6d4e2a9e97916c5b;hp=7a3be5ca037785f4f1acb225381578db12d15eb1;p=jFCPlib.git Refactor FCP dialog --- diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommandImpl.java index b34d2ba..9dca0dc 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommandImpl.java @@ -6,7 +6,6 @@ import java.net.URL; import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; @@ -77,32 +76,18 @@ public class AddPeerCommandImpl implements AddPeerCommand { private class AddPeerDialog extends FcpDialog> { - private final AtomicBoolean finished = new AtomicBoolean(); - private final AtomicReference peer = new AtomicReference<>(); - public AddPeerDialog() throws IOException { - super(threadPool, connectionSupplier.get()); - } - - @Override - protected boolean isFinished() { - return finished.get(); - } - - @Override - protected Optional getResult() { - return Optional.ofNullable(peer.get()); + super(threadPool, connectionSupplier.get(), Optional.empty()); } @Override protected void consumePeer(Peer peer) { - this.peer.set(peer); - finished.set(true); + setResult(Optional.ofNullable(peer)); } @Override protected void consumeProtocolError(ProtocolError protocolError) { - finished.set(true); + finish(); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java index d01b82e..7908f5c 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java @@ -5,7 +5,6 @@ import java.io.InputStream; import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Supplier; import net.pterodactylus.fcp.AllData; @@ -116,25 +115,27 @@ class ClientGetCommandImpl implements ClientGetCommand { private class ClientGetDialog extends FcpDialog> { - private final AtomicBoolean finished = new AtomicBoolean(); - private final AtomicBoolean failed = new AtomicBoolean(); - - private String contentType; - private long dataLength; - private InputStream payload; - public ClientGetDialog() throws IOException { - super(ClientGetCommandImpl.this.threadPool, ClientGetCommandImpl.this.connectionSupplier.get()); + super(ClientGetCommandImpl.this.threadPool, ClientGetCommandImpl.this.connectionSupplier.get(), Optional.empty()); } @Override - protected boolean isFinished() { - return finished.get() || failed.get(); + protected void consumeAllData(AllData allData) { + synchronized (this) { + String contentType = allData.getContentType(); + long dataLength = allData.getDataLength(); + try { + InputStream payload = new TempInputStream(allData.getPayloadInputStream(), dataLength); + setResult(Optional.of(createData(contentType, dataLength, payload))); + } catch (IOException e) { + // TODO – logging + finish(); + } + } } - @Override - protected Optional getResult() { - return failed.get() ? Optional.empty() : Optional.of(new Data() { + private Data createData(String contentType, long dataLength, InputStream payload) { + return new Data() { @Override public String getMimeType() { return contentType; @@ -149,27 +150,12 @@ class ClientGetCommandImpl implements ClientGetCommand { public InputStream getInputStream() { return payload; } - }); - } - - @Override - protected void consumeAllData(AllData allData) { - synchronized (this) { - contentType = allData.getContentType(); - dataLength = allData.getDataLength(); - try { - payload = new TempInputStream(allData.getPayloadInputStream(), dataLength); - finished.set(true); - } catch (IOException e) { - // TODO – logging - failed.set(true); - } - } + }; } @Override protected void consumeGetFailed(GetFailed getFailed) { - failed.set(true); + finish(); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientHelloImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientHelloImpl.java index cb85f60..551b52d 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientHelloImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientHelloImpl.java @@ -58,25 +58,13 @@ public class ClientHelloImpl { private class ClientHelloDialog extends FcpDialog { - private final AtomicReference receivedNodeHello = new AtomicReference<>(); - public ClientHelloDialog(FcpConnection connection) { - super(ClientHelloImpl.this.threadPool, connection); - } - - @Override - protected boolean isFinished() { - return receivedNodeHello.get() != null; - } - - @Override - protected Boolean getResult() { - return receivedNodeHello.get() != null; + super(ClientHelloImpl.this.threadPool, connection, false); } @Override protected void consumeNodeHello(NodeHello nodeHello) { - receivedNodeHello.set(nodeHello); + setResult(true); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java index 6eeddd9..982fd76 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java @@ -10,7 +10,6 @@ import java.util.Optional; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; @@ -140,21 +139,9 @@ class ClientPutCommandImpl implements ClientPutCommand { private final AtomicReference originalClientPut = new AtomicReference<>(); private final AtomicReference directory = new AtomicReference<>(); - private final AtomicReference finalKey = new AtomicReference<>(); - private final AtomicBoolean putFinished = new AtomicBoolean(); public ClientPutDialog() throws IOException { - super(ClientPutCommandImpl.this.threadPool, ClientPutCommandImpl.this.connectionSupplier.get()); - } - - @Override - protected boolean isFinished() { - return putFinished.get(); - } - - @Override - protected Optional getResult() { - return Optional.ofNullable(finalKey.get()); + super(ClientPutCommandImpl.this.threadPool, ClientPutCommandImpl.this.connectionSupplier.get(), Optional.empty()); } @Override @@ -176,13 +163,12 @@ class ClientPutCommandImpl implements ClientPutCommand { @Override protected void consumePutSuccessful(PutSuccessful putSuccessful) { - finalKey.set(new Key(putSuccessful.getURI())); - putFinished.set(true); + setResult(Optional.of(new Key(putSuccessful.getURI()))); } @Override protected void consumePutFailed(PutFailed putFailed) { - putFinished.set(true); + finish(); } @Override @@ -191,7 +177,7 @@ class ClientPutCommandImpl implements ClientPutCommand { setIdentifier(directory.get()); sendMessage(new TestDDARequest(directory.get(), true, false)); } else { - putFinished.set(true); + finish(); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/FcpDialog.java b/src/main/java/net/pterodactylus/fcp/quelaton/FcpDialog.java index f218610..5b030bd 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/FcpDialog.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/FcpDialog.java @@ -72,17 +72,31 @@ public abstract class FcpDialog implements AutoCloseable, FcpListener { private final AtomicReference identifier = new AtomicReference<>(); private final AtomicBoolean connectionClosed = new AtomicBoolean(); private final AtomicReference connectionFailureReason = new AtomicReference<>(); + private final AtomicBoolean finished = new AtomicBoolean(); + private final AtomicReference result = new AtomicReference<>(); - public FcpDialog(ExecutorService executorService, FcpConnection fcpConnection) { + public FcpDialog(ExecutorService executorService, FcpConnection fcpConnection, R initialResult) { this.executorService = MoreExecutors.listeningDecorator(executorService); this.fcpConnection = fcpConnection; + result.set(initialResult); } protected void setIdentifier(String identifier) { this.identifier.set(identifier); } - protected abstract boolean isFinished(); + public final boolean isFinished() { + return finished.get(); + } + + protected final void finish() { + finished.set(true); + } + + protected final void setResult(R result) { + this.result.set(result); + finish(); + } public ListenableFuture send(FcpMessage fcpMessage) throws IOException { setIdentifier(fcpMessage.getField("Identifier")); @@ -120,8 +134,8 @@ public abstract class FcpDialog implements AutoCloseable, FcpListener { } } - protected R getResult() { - return null; + protected final R getResult() { + return result.get(); } @Override diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java index 69fcf63..5a96450 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java @@ -3,7 +3,6 @@ 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.FcpKeyPair; import net.pterodactylus.fcp.GenerateSSK; @@ -41,25 +40,13 @@ class GenerateKeypairCommandImpl implements GenerateKeypairCommand { private class FcpKeyPairDialog extends FcpDialog { - private AtomicReference keyPair = new AtomicReference<>(); - public FcpKeyPairDialog() throws IOException { - super(GenerateKeypairCommandImpl.this.threadPool, GenerateKeypairCommandImpl.this.connectionSupplier.get()); - } - - @Override - protected boolean isFinished() { - return keyPair.get() != null; - } - - @Override - protected FcpKeyPair getResult() { - return keyPair.get(); + super(GenerateKeypairCommandImpl.this.threadPool, GenerateKeypairCommandImpl.this.connectionSupplier.get(), null); } @Override protected void consumeSSKKeypair(SSKKeypair sskKeypair) { - keyPair.set(new FcpKeyPair(sskKeypair.getRequestURI(), sskKeypair.getInsertURI())); + setResult(new FcpKeyPair(sskKeypair.getRequestURI(), sskKeypair.getInsertURI())); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/GetConfigCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/GetConfigCommandImpl.java index cac2535..1a5971c 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/GetConfigCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/GetConfigCommandImpl.java @@ -4,7 +4,6 @@ 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 java.util.function.Supplier; import net.pterodactylus.fcp.ConfigData; @@ -109,25 +108,13 @@ public class GetConfigCommandImpl implements GetConfigCommand { private class GetConfigDialog extends FcpDialog { - private final AtomicReference 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(); + super(threadPool, connectionSupplier.get(), null); } @Override protected void consumeConfigData(ConfigData configData) { - this.configData.set(configData); + setResult(configData); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommandImpl.java index 12f936d..d6999f2 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommandImpl.java @@ -4,7 +4,6 @@ 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 java.util.function.Supplier; import net.pterodactylus.fcp.GetNode; @@ -67,25 +66,13 @@ public class GetNodeCommandImpl implements GetNodeCommand { private class GetNodeDialog extends FcpDialog { - private final AtomicReference nodeData = new AtomicReference<>(); - public GetNodeDialog() throws IOException { - super(threadPool, connectionSupplier.get()); - } - - @Override - protected boolean isFinished() { - return nodeData.get() != null; - } - - @Override - protected NodeData getResult() { - return nodeData.get(); + super(threadPool, connectionSupplier.get(), null); } @Override protected void consumeNodeData(NodeData nodeData) { - this.nodeData.set(nodeData); + setResult(nodeData); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommandImpl.java index 49db826..e8a30ec 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommandImpl.java @@ -4,8 +4,6 @@ import java.io.IOException; import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; import net.pterodactylus.fcp.GetPluginInfo; @@ -57,32 +55,18 @@ public class GetPluginInfoCommandImpl implements GetPluginInfoCommand { private class GetPluginInfoDialog extends FcpDialog> { - private final AtomicBoolean finished = new AtomicBoolean(); - private final AtomicReference pluginInfo = new AtomicReference<>(); - public GetPluginInfoDialog() throws IOException { - super(threadPool, connectionSupplier.get()); - } - - @Override - protected boolean isFinished() { - return finished.get(); - } - - @Override - protected Optional getResult() { - return Optional.ofNullable(pluginInfo.get()); + super(threadPool, connectionSupplier.get(), Optional.empty()); } @Override protected void consumePluginInfo(PluginInfo pluginInfo) { - this.pluginInfo.set(pluginInfo); - finished.set(true); + setResult(Optional.ofNullable(pluginInfo)); } @Override protected void consumeProtocolError(ProtocolError protocolError) { - finished.set(true); + finish(); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeerCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeerCommandImpl.java index fc9aeec..4bcd2fa 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeerCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeerCommandImpl.java @@ -4,7 +4,6 @@ import java.io.IOException; import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; @@ -65,32 +64,18 @@ public class ListPeerCommandImpl implements ListPeerCommand { private class ListPeerDialog extends FcpDialog { - private final AtomicBoolean finished = new AtomicBoolean(); - private final AtomicReference peer = new AtomicReference<>(); - public ListPeerDialog() throws IOException { - super(threadPool, connectionSupplier.get()); - } - - @Override - protected boolean isFinished() { - return finished.get(); - } - - @Override - protected Peer getResult() { - return peer.get(); + super(threadPool, connectionSupplier.get(), null); } @Override protected void consumePeer(Peer peer) { - this.peer.set(peer); - finished.set(true); + setResult(peer); } @Override protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) { - finished.set(true); + finish(); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeerNotesCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeerNotesCommandImpl.java index 8e1df8c..95e885f 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeerNotesCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeerNotesCommandImpl.java @@ -4,7 +4,6 @@ import java.io.IOException; import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; @@ -66,36 +65,23 @@ public class ListPeerNotesCommandImpl implements ListPeerNotesCommand { private class ListPeerNotesDialog extends FcpDialog> { - private final AtomicReference peerNote = new AtomicReference<>(); - private final AtomicBoolean finished = new AtomicBoolean(); - public ListPeerNotesDialog() throws IOException { - super(threadPool, connectionSupplier.get()); - } - - @Override - protected boolean isFinished() { - return finished.get(); - } - - @Override - protected Optional getResult() { - return Optional.ofNullable(peerNote.get()); + super(threadPool, connectionSupplier.get(), Optional.empty()); } @Override protected void consumePeerNote(PeerNote peerNote) { - this.peerNote.set(peerNote); + setResult(Optional.ofNullable(peerNote)); } @Override protected void consumeEndListPeerNotes(EndListPeerNotes endListPeerNotes) { - finished.set(true); + finish(); } @Override protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) { - finished.set(true); + finish(); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java index 6a352d5..c628749 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java @@ -2,6 +2,7 @@ package net.pterodactylus.fcp.quelaton; import java.io.IOException; import java.util.Collection; +import java.util.Collections; import java.util.HashSet; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; @@ -62,20 +63,9 @@ public class ListPeersCommandImpl implements ListPeersCommand { private class ListPeersDialog extends FcpDialog> { private final Collection peers = new HashSet<>(); - private final AtomicBoolean finished = new AtomicBoolean(false); public ListPeersDialog() throws IOException { - super(threadPool, connectionSupplier.get()); - } - - @Override - protected boolean isFinished() { - return finished.get(); - } - - @Override - protected Collection getResult() { - return peers; + super(threadPool, connectionSupplier.get(), Collections.emptyList()); } @Override @@ -85,7 +75,7 @@ public class ListPeersCommandImpl implements ListPeersCommand { @Override protected void consumeEndListPeers(EndListPeers endListPeers) { - finished.set(true); + setResult(peers); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/LoadPluginCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/LoadPluginCommandImpl.java index 9c9e64e..fa6bd65 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/LoadPluginCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/LoadPluginCommandImpl.java @@ -4,8 +4,6 @@ import java.io.IOException; import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; import net.pterodactylus.fcp.LoadPlugin; @@ -90,32 +88,18 @@ public class LoadPluginCommandImpl implements LoadPluginCommand { private class LoadPluginDialog extends FcpDialog> { - private final AtomicBoolean finished = new AtomicBoolean(); - private final AtomicReference pluginInfo = new AtomicReference<>(); - public LoadPluginDialog() throws IOException { - super(threadPool, connectionSupplier.get()); - } - - @Override - protected boolean isFinished() { - return finished.get(); - } - - @Override - protected Optional getResult() { - return Optional.ofNullable(pluginInfo.get()); + super(threadPool, connectionSupplier.get(), Optional.empty()); } @Override protected void consumePluginInfo(PluginInfo pluginInfo) { - this.pluginInfo.set(pluginInfo); - finished.set(true); + setResult(Optional.ofNullable(pluginInfo)); } @Override protected void consumeProtocolError(ProtocolError protocolError) { - finished.set(true); + finish(); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ModifyConfigCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ModifyConfigCommandImpl.java index 6733d25..4e3b373 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ModifyConfigCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ModifyConfigCommandImpl.java @@ -3,8 +3,6 @@ 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 java.util.function.Supplier; import net.pterodactylus.fcp.ConfigData; @@ -54,27 +52,13 @@ public class ModifyConfigCommandImpl implements ModifyConfigCommand { private class ModifyConfigDialog extends FcpDialog { - private final AtomicBoolean finished = new AtomicBoolean(); - private final AtomicReference 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(); + super(threadPool, connectionSupplier.get(), null); } @Override protected void consumeConfigData(ConfigData configData) { - this.configData.set(configData); - finished.set(true); + setResult(configData); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommandImpl.java index 9fff845..d5f5840 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommandImpl.java @@ -4,7 +4,6 @@ import java.io.IOException; import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; @@ -135,32 +134,18 @@ public class ModifyPeerCommandImpl implements ModifyPeerCommand { private class ModifyPeerDialog extends FcpDialog> { - private final AtomicBoolean finished = new AtomicBoolean(); - private final AtomicReference peer = new AtomicReference<>(); - public ModifyPeerDialog() throws IOException { - super(threadPool, connectionSupplier.get()); - } - - @Override - protected boolean isFinished() { - return finished.get(); - } - - @Override - protected Optional getResult() { - return Optional.ofNullable(peer.get()); + super(threadPool, connectionSupplier.get(), Optional.empty()); } @Override protected void consumePeer(Peer peer) { - this.peer.set(peer); - finished.set(true); + setResult(Optional.ofNullable(peer)); } @Override protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) { - finished.set(true); + finish(); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerNoteCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerNoteCommandImpl.java index a3b9aa1..8421240 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerNoteCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerNoteCommandImpl.java @@ -4,7 +4,6 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; @@ -82,32 +81,18 @@ public class ModifyPeerNoteCommandImpl implements ModifyPeerNoteCommand { private class ModifyPeerNoteDialog extends FcpDialog { - private final AtomicBoolean finished = new AtomicBoolean(); - private final AtomicBoolean successful = new AtomicBoolean(); - public ModifyPeerNoteDialog() throws IOException { - super(threadPool, connectionSupplier.get()); - } - - @Override - protected boolean isFinished() { - return finished.get(); - } - - @Override - protected Boolean getResult() { - return successful.get(); + super(threadPool, connectionSupplier.get(), false); } @Override protected void consumePeerNote(PeerNote peerNote) { - successful.set(true); - finished.set(true); + setResult(true); } @Override protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) { - finished.set(true); + finish(); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ReloadPluginCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ReloadPluginCommandImpl.java index a05963c..7e53055 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ReloadPluginCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ReloadPluginCommandImpl.java @@ -4,8 +4,6 @@ import java.io.IOException; import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; import net.pterodactylus.fcp.PluginInfo; @@ -69,32 +67,18 @@ public class ReloadPluginCommandImpl implements ReloadPluginCommand { private class ReloadPluginDialog extends FcpDialog> { - private final AtomicBoolean finished = new AtomicBoolean(); - private final AtomicReference pluginInfo = new AtomicReference<>(); - public ReloadPluginDialog() throws IOException { - super(threadPool, connectionSupplier.get()); - } - - @Override - protected boolean isFinished() { - return finished.get(); - } - - @Override - protected Optional getResult() { - return Optional.ofNullable(pluginInfo.get()); + super(threadPool, connectionSupplier.get(), Optional.empty()); } @Override protected void consumePluginInfo(PluginInfo pluginInfo) { - this.pluginInfo.set(pluginInfo); - finished.set(true); + setResult(Optional.ofNullable(pluginInfo)); } @Override protected void consumeProtocolError(ProtocolError protocolError) { - finished.set(true); + finish(); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/RemovePeerCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/RemovePeerCommandImpl.java index e5083d7..b0113b8 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/RemovePeerCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/RemovePeerCommandImpl.java @@ -3,7 +3,6 @@ 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 java.util.function.Supplier; @@ -64,31 +63,18 @@ public class RemovePeerCommandImpl implements RemovePeerCommand { private class RemovePeerDialog extends FcpDialog { - private final AtomicBoolean finished = new AtomicBoolean(); - private final AtomicBoolean removed = new AtomicBoolean(); - public RemovePeerDialog() throws IOException { - super(threadPool, connectionSupplier.get()); - } - - @Override - protected boolean isFinished() { - return finished.get() || removed.get(); - } - - @Override - protected Boolean getResult() { - return removed.get(); + super(threadPool, connectionSupplier.get(), false); } @Override protected void consumePeerRemoved(PeerRemoved peerRemoved) { - removed.set(true); + setResult(true); } @Override protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) { - finished.set(true); + finish(); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/RemovePluginCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/RemovePluginCommandImpl.java index 2135e2e..1e9ed56 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/RemovePluginCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/RemovePluginCommandImpl.java @@ -3,7 +3,6 @@ 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.function.Supplier; import net.pterodactylus.fcp.PluginRemoved; @@ -61,32 +60,18 @@ public class RemovePluginCommandImpl implements RemovePluginCommand { private class RemovePluginDialog extends FcpDialog { - private final AtomicBoolean finished = new AtomicBoolean(); - private final AtomicBoolean pluginRemoved = new AtomicBoolean(); - public RemovePluginDialog() throws IOException { - super(threadPool, connectionSupplier.get()); - } - - @Override - protected boolean isFinished() { - return finished.get(); - } - - @Override - protected Boolean getResult() { - return pluginRemoved.get(); + super(threadPool, connectionSupplier.get(), false); } @Override protected void consumePluginRemoved(PluginRemoved pluginRemoved) { - this.pluginRemoved.set(true); - finished.set(true); + setResult(true); } @Override protected void consumeProtocolError(ProtocolError protocolError) { - finished.set(true); + finish(); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/SubscribeUskCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/SubscribeUskCommandImpl.java index f314247..9977163 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/SubscribeUskCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/SubscribeUskCommandImpl.java @@ -4,7 +4,6 @@ import java.io.IOException; import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Supplier; import net.pterodactylus.fcp.IdentifierCollision; @@ -58,32 +57,18 @@ public class SubscribeUskCommandImpl implements SubscribeUskCommand { private class SubscribeUskDialog extends FcpDialog { - private final AtomicBoolean finished = new AtomicBoolean(); - private final AtomicBoolean success = new AtomicBoolean(); - public SubscribeUskDialog() throws IOException { - super(threadPool, connectionSupplier.get()); - } - - @Override - protected boolean isFinished() { - return finished.get(); - } - - @Override - protected Boolean getResult() { - return success.get(); + super(threadPool, connectionSupplier.get(), false); } @Override protected void consumeSubscribedUSK(SubscribedUSK subscribedUSK) { - success.set(true); - finished.set(true); + setResult(true); } @Override protected void consumeIdentifierCollision(IdentifierCollision identifierCollision) { - finished.set(true); + finish(); } } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/UnsubscribeUskCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/UnsubscribeUskCommandImpl.java index 3755a05..288903f 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/UnsubscribeUskCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/UnsubscribeUskCommandImpl.java @@ -46,12 +46,8 @@ public class UnsubscribeUskCommandImpl implements UnsubscribeUskCommand { private class UnsubscribeUskDialog extends FcpDialog { public UnsubscribeUskDialog() throws IOException { - super(threadPool, connectionSupplier.get()); - } - - @Override - protected boolean isFinished() { - return true; + super(threadPool, connectionSupplier.get(), null); + finish(); } } diff --git a/src/test/java/net/pterodactylus/fcp/quelaton/FcpDialogTest.java b/src/test/java/net/pterodactylus/fcp/quelaton/FcpDialogTest.java index ee04e6f..7651840 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/FcpDialogTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/FcpDialogTest.java @@ -77,12 +77,11 @@ public class FcpDialogTest { } private FcpDialog createBasicDialog() { - return new FcpDialog(executorService, fcpConnection) { - @Override - protected boolean isFinished() { - return true; - } - }; + return new FcpDialog(executorService, fcpConnection, true) { + { + finish(); + } + }; } @Test @@ -329,24 +328,22 @@ public class FcpDialogTest { private final AtomicBoolean gotPutFailed = new AtomicBoolean(); private final AtomicBoolean gotGetFailed = new AtomicBoolean(); - @Override - protected boolean isFinished() { - return gotPutFailed.get() && gotGetFailed.get(); - } - - @Override - protected Boolean getResult() { - return isFinished(); + private void checkIfBothFailedsWereReceived() { + if (gotPutFailed.get() && gotGetFailed.get()) { + setResult(true); + } } @Override protected void consumePutFailed(PutFailed putFailed) { gotPutFailed.set(true); + checkIfBothFailedsWereReceived(); } @Override protected void consumeGetFailed(GetFailed getFailed) { gotGetFailed.set(true); + checkIfBothFailedsWereReceived(); } }; Future result = testFcpDialog.send(fcpMessage); @@ -383,210 +380,205 @@ public class FcpDialogTest { private static class TestFcpDialog extends FcpDialog { - private final AtomicReference gotMessage = new AtomicReference<>(); private final AtomicReference expectedMessage = new AtomicReference<>(); public TestFcpDialog(ExecutorService executorService, FcpConnection fcpConnection) { - super(executorService, fcpConnection); + super(executorService, fcpConnection, false); } public void setExpectedMessage(String expectedMessage) { this.expectedMessage.set(expectedMessage); } - @Override - protected boolean isFinished() { - return getResult(); - } - - @Override - protected Boolean getResult() { - return expectedMessage.get().equals(gotMessage.get()); + private void verifyReceivedMessage(String message) { + if (expectedMessage.get().equals(message)) { + setResult(true); + } } @Override protected void consumeNodeHello(NodeHello nodeHello) { - gotMessage.set(nodeHello.getName()); + verifyReceivedMessage(nodeHello.getName()); } @Override protected void consumeSSKKeypair(SSKKeypair sskKeypair) { - gotMessage.set(sskKeypair.getName()); + verifyReceivedMessage(sskKeypair.getName()); } @Override protected void consumePeer(Peer peer) { - gotMessage.set(peer.getName()); + verifyReceivedMessage(peer.getName()); } @Override protected void consumeEndListPeers(EndListPeers endListPeers) { - gotMessage.set(endListPeers.getName()); + verifyReceivedMessage(endListPeers.getName()); } @Override protected void consumePeerNote(PeerNote peerNote) { - gotMessage.set(peerNote.getName()); + verifyReceivedMessage(peerNote.getName()); } @Override protected void consumeEndListPeerNotes(EndListPeerNotes endListPeerNotes) { - gotMessage.set(endListPeerNotes.getName()); + verifyReceivedMessage(endListPeerNotes.getName()); } @Override protected void consumePeerRemoved(PeerRemoved peerRemoved) { - gotMessage.set(peerRemoved.getName()); + verifyReceivedMessage(peerRemoved.getName()); } @Override protected void consumeNodeData(NodeData nodeData) { - gotMessage.set(nodeData.getName()); + verifyReceivedMessage(nodeData.getName()); } @Override protected void consumeTestDDAReply(TestDDAReply testDDAReply) { - gotMessage.set(testDDAReply.getName()); + verifyReceivedMessage(testDDAReply.getName()); } @Override protected void consumeTestDDAComplete(TestDDAComplete testDDAComplete) { - gotMessage.set(testDDAComplete.getName()); + verifyReceivedMessage(testDDAComplete.getName()); } @Override protected void consumePersistentGet(PersistentGet persistentGet) { - gotMessage.set(persistentGet.getName()); + verifyReceivedMessage(persistentGet.getName()); } @Override protected void consumePersistentPut(PersistentPut persistentPut) { - gotMessage.set(persistentPut.getName()); + verifyReceivedMessage(persistentPut.getName()); } @Override protected void consumeEndListPersistentRequests(EndListPersistentRequests endListPersistentRequests) { - gotMessage.set(endListPersistentRequests.getName()); + verifyReceivedMessage(endListPersistentRequests.getName()); } @Override protected void consumeURIGenerated(URIGenerated uriGenerated) { - gotMessage.set(uriGenerated.getName()); + verifyReceivedMessage(uriGenerated.getName()); } @Override protected void consumeDataFound(DataFound dataFound) { - gotMessage.set(dataFound.getName()); + verifyReceivedMessage(dataFound.getName()); } @Override protected void consumeAllData(AllData allData) { - gotMessage.set(allData.getName()); + verifyReceivedMessage(allData.getName()); } @Override protected void consumeSimpleProgress(SimpleProgress simpleProgress) { - gotMessage.set(simpleProgress.getName()); + verifyReceivedMessage(simpleProgress.getName()); } @Override protected void consumeStartedCompression(StartedCompression startedCompression) { - gotMessage.set(startedCompression.getName()); + verifyReceivedMessage(startedCompression.getName()); } @Override protected void consumeFinishedCompression(FinishedCompression finishedCompression) { - gotMessage.set(finishedCompression.getName()); + verifyReceivedMessage(finishedCompression.getName()); } @Override protected void consumeUnknownPeerNoteType(UnknownPeerNoteType unknownPeerNoteType) { - gotMessage.set(unknownPeerNoteType.getName()); + verifyReceivedMessage(unknownPeerNoteType.getName()); } @Override protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) { - gotMessage.set(unknownNodeIdentifier.getName()); + verifyReceivedMessage(unknownNodeIdentifier.getName()); } @Override protected void consumeConfigData(ConfigData configData) { - gotMessage.set(configData.getName()); + verifyReceivedMessage(configData.getName()); } @Override protected void consumeGetFailed(GetFailed getFailed) { - gotMessage.set(getFailed.getName()); + verifyReceivedMessage(getFailed.getName()); } @Override protected void consumePutFailed(PutFailed putFailed) { - gotMessage.set(putFailed.getName()); + verifyReceivedMessage(putFailed.getName()); } @Override protected void consumeIdentifierCollision(IdentifierCollision identifierCollision) { - gotMessage.set(identifierCollision.getName()); + verifyReceivedMessage(identifierCollision.getName()); } @Override protected void consumePersistentPutDir(PersistentPutDir persistentPutDir) { - gotMessage.set(persistentPutDir.getName()); + verifyReceivedMessage(persistentPutDir.getName()); } @Override protected void consumePersistentRequestRemoved(PersistentRequestRemoved persistentRequestRemoved) { - gotMessage.set(persistentRequestRemoved.getName()); + verifyReceivedMessage(persistentRequestRemoved.getName()); } @Override protected void consumeSubscribedUSKUpdate(SubscribedUSKUpdate subscribedUSKUpdate) { - gotMessage.set(subscribedUSKUpdate.getName()); + verifyReceivedMessage(subscribedUSKUpdate.getName()); } @Override protected void consumePluginInfo(PluginInfo pluginInfo) { - gotMessage.set(pluginInfo.getName()); + verifyReceivedMessage(pluginInfo.getName()); } @Override protected void consumeFCPPluginReply(FCPPluginReply fcpPluginReply) { - gotMessage.set(fcpPluginReply.getName()); + verifyReceivedMessage(fcpPluginReply.getName()); } @Override protected void consumePersistentRequestModified(PersistentRequestModified persistentRequestModified) { - gotMessage.set(persistentRequestModified.getName()); + verifyReceivedMessage(persistentRequestModified.getName()); } @Override protected void consumePutSuccessful(PutSuccessful putSuccessful) { - gotMessage.set(putSuccessful.getName()); + verifyReceivedMessage(putSuccessful.getName()); } @Override protected void consumePutFetchable(PutFetchable putFetchable) { - gotMessage.set(putFetchable.getName()); + verifyReceivedMessage(putFetchable.getName()); } @Override protected void consumeSentFeed(SentFeed sentFeed) { - gotMessage.set(sentFeed.getName()); + verifyReceivedMessage(sentFeed.getName()); } @Override protected void consumeReceivedBookmarkFeed(ReceivedBookmarkFeed receivedBookmarkFeed) { - gotMessage.set(receivedBookmarkFeed.getName()); + verifyReceivedMessage(receivedBookmarkFeed.getName()); } @Override protected void consumeProtocolError(ProtocolError protocolError) { - gotMessage.set(protocolError.getName()); + verifyReceivedMessage(protocolError.getName()); } @Override protected void consumeUnknownMessage(FcpMessage fcpMessage) { - gotMessage.set(fcpMessage.getName()); + verifyReceivedMessage(fcpMessage.getName()); } }