From 369d751e2d866f4263c2ef9652068b11ea515c7f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 27 Feb 2020 22:22:54 +0100 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Hide=20count-down=20latch?= =?utf8?q?=20for=20completion=20signalling?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/fcp/highlevel/FcpClient.java | 37 +++++++++++++--------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java b/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java index 6413dc2..65bc227 100644 --- a/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java @@ -301,7 +301,7 @@ public class FcpClient implements Closeable { @SuppressWarnings("synthetic-access") public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) { FcpClient.this.nodeHello = nodeHello; - completionLatch.countDown(); + complete(); } }.execute(); } @@ -367,11 +367,11 @@ public class FcpClient implements Closeable { fcpConnection.sendMessage(clientGet); } catch (IOException ioe1) { getResult.success(false).exception(ioe1); - completionLatch.countDown(); + complete(); } } else { getResult.success(false).errorCode(getFailed.getCode()); - completionLatch.countDown(); + complete(); } } @@ -381,7 +381,7 @@ public class FcpClient implements Closeable { return; } getResult.success(true).contentType(allData.getContentType()).contentLength(allData.getDataLength()).inputStream(allData.getPayloadInputStream()); - completionLatch.countDown(); + complete(); } }.execute(); @@ -473,7 +473,7 @@ public class FcpClient implements Closeable { @Override public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) { if (endListPeers.getIdentifier().equals(identifier)) { - completionLatch.countDown(); + complete(); } } }.execute(); @@ -640,7 +640,7 @@ public class FcpClient implements Closeable { */ @Override public void receivedPeer(FcpConnection fcpConnection, Peer peer) { - completionLatch.countDown(); + complete(); } }.execute(); } @@ -683,7 +683,7 @@ public class FcpClient implements Closeable { */ @Override public void receivedPeer(FcpConnection fcpConnection, Peer peer) { - completionLatch.countDown(); + complete(); } }.execute(); } @@ -715,7 +715,7 @@ public class FcpClient implements Closeable { */ @Override public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) { - completionLatch.countDown(); + complete(); } }.execute(); } @@ -763,7 +763,7 @@ public class FcpClient implements Closeable { */ @Override public void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) { - completionLatch.countDown(); + complete(); } }.execute(); return objectWrapper.get(); @@ -802,7 +802,7 @@ public class FcpClient implements Closeable { @Override public void receivedPeer(FcpConnection fcpConnection, Peer receivedPeer) { if (receivedPeer.getIdentity().equals(peer.getIdentity())) { - completionLatch.countDown(); + complete(); } } }.execute(); @@ -840,7 +840,7 @@ public class FcpClient implements Closeable { @Override public void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) { sskKeypairWrapper.set(sskKeypair); - completionLatch.countDown(); + complete(); } }.execute(); return sskKeypairWrapper.get(); @@ -1005,7 +1005,7 @@ public class FcpClient implements Closeable { */ @Override public void receivedEndListPersistentRequests(FcpConnection fcpConnection, EndListPersistentRequests endListPersistentRequests) { - completionLatch.countDown(); + complete(); } }.execute(); return requests.values(); @@ -1078,7 +1078,7 @@ public class FcpClient implements Closeable { return; } pluginReplies.putAll(fcpPluginReply.getReplies()); - completionLatch.countDown(); + complete(); } }.execute(); @@ -1121,7 +1121,7 @@ public class FcpClient implements Closeable { @Override public void receivedNodeData(FcpConnection fcpConnection, NodeData nodeData) { nodeDataWrapper.set(nodeData); - completionLatch.countDown(); + complete(); } }.execute(); return nodeDataWrapper.get(); @@ -1173,7 +1173,7 @@ public class FcpClient implements Closeable { private abstract class ExtendedFcpAdapter extends FcpAdapter { /** The count down latch used to wait for completion. */ - protected final CountDownLatch completionLatch = new CountDownLatch(1); + private final CountDownLatch completionLatch = new CountDownLatch(1); /** The FCP exception, if any. */ protected FcpException fcpException; @@ -1229,6 +1229,13 @@ public class FcpClient implements Closeable { public abstract void run() throws IOException; /** + * Signals completion of the command processing. + */ + protected void complete() { + completionLatch.countDown(); + } + + /** * {@inheritDoc} */ @Override -- 2.7.4