From b315e585ac78058d727eea98aca2ba9c7ed93c51 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 10 Jul 2015 23:06:29 +0200 Subject: [PATCH] Close FCP reply sequences after use --- .../net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java | 10 +++++++++- .../java/net/pterodactylus/fcp/quelaton/ClientHelloImpl.java | 5 ++--- .../net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java | 9 ++++++++- .../pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java | 10 ++++++++-- .../net/pterodactylus/fcp/quelaton/GetNodeCommandImpl.java | 9 ++++++++- .../net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java | 10 ++++++++-- 6 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java index 5137c96..55d3e1c 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java @@ -3,6 +3,8 @@ package net.pterodactylus.fcp.quelaton; import java.io.IOException; import java.io.InputStream; import java.util.Optional; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; @@ -79,8 +81,14 @@ class ClientGetCommandImpl implements ClientGetCommand { @Override public Executable> uri(String uri) { + return () -> threadPool.submit(() -> execute(uri)); + } + + private Optional execute(String uri) throws InterruptedException, ExecutionException, IOException { ClientGet clientGet = createClientGetCommand(uri); - return () -> threadPool.submit(() -> new ClientGetReplySequence().send(clientGet).get()); + try (ClientGetReplySequence clientGetReplySequence = new ClientGetReplySequence()) { + return clientGetReplySequence.send(clientGet).get(); + } } private ClientGet createClientGetCommand(String uri) { diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientHelloImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientHelloImpl.java index c093e89..4e3b51c 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientHelloImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientHelloImpl.java @@ -39,15 +39,14 @@ public class ClientHelloImpl { } private ListenableFuture execute() { - return threadPool.submit(() -> establishConnection()); + return threadPool.submit(this::establishConnection); } private FcpConnection establishConnection() throws IOException { FcpConnection connection = new FcpConnection(hostname, port); connection.connect(); - ClientHelloReplySequence nodeHelloSequence = new ClientHelloReplySequence(connection); ClientHello clientHello = new ClientHello(clientName.get(), "2.0"); - try { + try (ClientHelloReplySequence nodeHelloSequence = new ClientHelloReplySequence(connection)) { if (nodeHelloSequence.send(clientHello).get()) { return connection; } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java index 02d020c..df345f3 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java @@ -6,6 +6,7 @@ import java.io.InputStream; import java.nio.file.Files; import java.util.Objects; 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.AtomicLong; @@ -77,9 +78,15 @@ class ClientPutCommandImpl implements ClientPutCommand { } private Executable> key(String uri) { + return () -> threadPool.submit(() -> execute(uri)); + } + + private Optional execute(String uri) throws InterruptedException, ExecutionException, IOException { String identifier = new RandomIdentifierGenerator().generate(); ClientPut clientPut = createClientPutCommand(uri, identifier); - return () -> threadPool.submit(() -> new ClientPutReplySequence().send(clientPut).get()); + try (ClientPutReplySequence clientPutReplySequence = new ClientPutReplySequence()) { + return clientPutReplySequence.send(clientPut).get(); + } } private ClientPut createClientPutCommand(String uri, String identifier) { diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java index d5f30d6..564fa71 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java @@ -1,8 +1,8 @@ package net.pterodactylus.fcp.quelaton; import java.io.IOException; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicReference; import net.pterodactylus.fcp.FcpKeyPair; @@ -30,7 +30,13 @@ class GenerateKeypairCommandImpl implements GenerateKeypairCommand { @Override public ListenableFuture execute() { - return threadPool.submit(() -> new FcpKeyPairReplySequence().send(new GenerateSSK()).get()); + return threadPool.submit(this::executeSequence); + } + + private FcpKeyPair executeSequence() throws InterruptedException, ExecutionException, IOException { + try (FcpKeyPairReplySequence fcpKeyPairReplySequence = new FcpKeyPairReplySequence()) { + return fcpKeyPairReplySequence.send(new GenerateSSK()).get(); + } } private class FcpKeyPairReplySequence extends FcpReplySequence { diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommandImpl.java index 51ebd73..2447179 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommandImpl.java @@ -1,6 +1,7 @@ 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; @@ -50,9 +51,15 @@ public class GetNodeCommandImpl implements GetNodeCommand { @Override public ListenableFuture execute() { + return threadPool.submit(this::executeSequence); + } + + private NodeData executeSequence() throws InterruptedException, ExecutionException, IOException { GetNode getNode = new GetNode(new RandomIdentifierGenerator().generate(), giveOpennetRef.get(), includePrivate.get(), includeVolatile.get()); - return threadPool.submit(() -> new GetNodeReplySequence().send(getNode).get()); + try (GetNodeReplySequence getNodeReplySequence = new GetNodeReplySequence()) { + return getNodeReplySequence.send(getNode).get(); + } } private class GetNodeReplySequence extends FcpReplySequence { diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java index c084cb5..3d3baed 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java @@ -3,8 +3,8 @@ package net.pterodactylus.fcp.quelaton; import java.io.IOException; import java.util.Collection; import java.util.HashSet; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicBoolean; import net.pterodactylus.fcp.EndListPeers; @@ -46,9 +46,15 @@ public class ListPeersCommandImpl implements ListPeersCommand { @Override public ListenableFuture> execute() { + return threadPool.submit(this::executeSequence); + } + + private Collection executeSequence() throws InterruptedException, ExecutionException, IOException { String identifier = new RandomIdentifierGenerator().generate(); ListPeers listPeers = new ListPeers(identifier, includeMetadata.get(), includeVolatile.get()); - return threadPool.submit(() -> new ListPeersReplySequence().send(listPeers).get()); + try (ListPeersReplySequence listPeersReplySequence = new ListPeersReplySequence()) { + return listPeersReplySequence.send(listPeers).get(); + } } private class ListPeersReplySequence extends FcpReplySequence> { -- 2.7.4