From f5ba10406d666197d448956e4340254c4f5593b6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 8 Jul 2015 20:01:11 +0200 Subject: [PATCH] Use listenable future instead of plain ones --- .../net/pterodactylus/fcp/quelaton/ClientGetCommand.java | 5 +++-- .../pterodactylus/fcp/quelaton/ClientGetCommandImpl.java | 13 ++++++++----- .../net/pterodactylus/fcp/quelaton/DefaultFcpClient.java | 7 +++++-- .../net/pterodactylus/fcp/quelaton/FcpReplySequence.java | 11 +++++++---- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommand.java index 280daae..7e039ba 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommand.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommand.java @@ -2,10 +2,11 @@ package net.pterodactylus.fcp.quelaton; import java.io.InputStream; import java.util.Optional; -import java.util.concurrent.Future; import net.pterodactylus.fcp.Priority; +import com.google.common.util.concurrent.ListenableFuture; + /** * Command that retrieves data from Freenet. * @@ -20,7 +21,7 @@ public interface ClientGetCommand { ClientGetCommand realTime(); ClientGetCommand global(); - Future> uri(String uri); + ListenableFuture> uri(String uri); interface Data { diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java index 98c416f..575d94b 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java @@ -4,7 +4,6 @@ import java.io.IOException; import java.io.InputStream; import java.util.Optional; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; @@ -16,6 +15,10 @@ import net.pterodactylus.fcp.GetFailed; import net.pterodactylus.fcp.Priority; import net.pterodactylus.fcp.ReturnType; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.common.util.concurrent.MoreExecutors; + /** * Implementation of the {@link ClientGetCommand}. * @@ -23,7 +26,7 @@ import net.pterodactylus.fcp.ReturnType; */ class ClientGetCommandImpl implements ClientGetCommand { - private final ExecutorService threadPool; + private final ListeningExecutorService threadPool; private final ConnectionSupplier connectionSupplier; private boolean ignoreDataStore; @@ -34,7 +37,7 @@ class ClientGetCommandImpl implements ClientGetCommand { private boolean global; public ClientGetCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) { - this.threadPool = threadPool; + this.threadPool = MoreExecutors.listeningDecorator(threadPool); this.connectionSupplier = connectionSupplier; } @@ -75,7 +78,7 @@ class ClientGetCommandImpl implements ClientGetCommand { } @Override - public Future> uri(String uri) { + public ListenableFuture> uri(String uri) { ClientGet clientGet = createClientGetCommand(uri); return threadPool.submit(() -> new ClientGetReplySequence().send(clientGet).get()); } @@ -173,7 +176,7 @@ class ClientGetCommandImpl implements ClientGetCommand { } @Override - public Future> send(FcpMessage fcpMessage) throws IOException { + public ListenableFuture> send(FcpMessage fcpMessage) throws IOException { identifier.set(fcpMessage.getField("Identifier")); return super.send(fcpMessage); } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java index 9c7a705..6d46ef7 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java @@ -12,6 +12,9 @@ import net.pterodactylus.fcp.CloseConnectionDuplicateClientName; import net.pterodactylus.fcp.FcpConnection; import net.pterodactylus.fcp.NodeHello; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.common.util.concurrent.MoreExecutors; + /** * Default {@link FcpClient} implementation. * @@ -19,7 +22,7 @@ import net.pterodactylus.fcp.NodeHello; */ public class DefaultFcpClient implements FcpClient { - private final ExecutorService threadPool; + private final ListeningExecutorService threadPool; private final String hostname; private final int port; private final AtomicReference fcpConnection = new AtomicReference<>(); @@ -28,7 +31,7 @@ public class DefaultFcpClient implements FcpClient { public DefaultFcpClient(ExecutorService threadPool, String hostname, int port, Supplier clientName, Supplier expectedVersion) { - this.threadPool = threadPool; + this.threadPool = MoreExecutors.listeningDecorator(threadPool); this.hostname = hostname; this.port = port; this.clientName = clientName; diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/FcpReplySequence.java b/src/main/java/net/pterodactylus/fcp/quelaton/FcpReplySequence.java index 25db847..03d1b0b 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/FcpReplySequence.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/FcpReplySequence.java @@ -2,7 +2,6 @@ package net.pterodactylus.fcp.quelaton; import java.io.IOException; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Future; import java.util.function.Consumer; import net.pterodactylus.fcp.AllData; @@ -46,6 +45,10 @@ import net.pterodactylus.fcp.URIGenerated; import net.pterodactylus.fcp.UnknownNodeIdentifier; import net.pterodactylus.fcp.UnknownPeerNoteType; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.common.util.concurrent.MoreExecutors; + /** * An FCP reply sequence enables you to conveniently wait for a specific set of FCP replies. * @@ -54,17 +57,17 @@ import net.pterodactylus.fcp.UnknownPeerNoteType; public abstract class FcpReplySequence implements AutoCloseable, FcpListener { private final Object syncObject = new Object(); - private final ExecutorService executorService; + private final ListeningExecutorService executorService; private final FcpConnection fcpConnection; public FcpReplySequence(ExecutorService executorService, FcpConnection fcpConnection) { - this.executorService = executorService; + this.executorService = MoreExecutors.listeningDecorator(executorService); this.fcpConnection = fcpConnection; } protected abstract boolean isFinished(); - public Future send(FcpMessage fcpMessage) throws IOException { + public ListenableFuture send(FcpMessage fcpMessage) throws IOException { try { fcpConnection.addFcpListener(this); -- 2.7.4