Close FCP reply sequences after use
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 10 Jul 2015 21:06:29 +0000 (23:06 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 10 Jul 2015 21:06:29 +0000 (23:06 +0200)
src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/ClientHelloImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java

index 5137c96..55d3e1c 100644 (file)
@@ -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<Optional<Data>> uri(String uri) {
+               return () -> threadPool.submit(() -> execute(uri));
+       }
+
+       private Optional<Data> 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) {
index c093e89..4e3b51c 100644 (file)
@@ -39,15 +39,14 @@ public class ClientHelloImpl {
        }
 
        private ListenableFuture<FcpConnection> 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;
                        }
index 02d020c..df345f3 100644 (file)
@@ -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<Optional<Key>> key(String uri) {
+               return () -> threadPool.submit(() -> execute(uri));
+       }
+
+       private Optional<Key> 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) {
index d5f30d6..564fa71 100644 (file)
@@ -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<FcpKeyPair> 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<FcpKeyPair> {
index 51ebd73..2447179 100644 (file)
@@ -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<NodeData> 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<NodeData> {
index c084cb5..3d3baed 100644 (file)
@@ -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<Collection<Peer>> execute() {
+               return threadPool.submit(this::executeSequence);
+       }
+
+       private Collection<Peer> 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<Collection<Peer>> {