Use a single identifier generator in all commands
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / ClientGetCommandImpl.java
index b4e391a..d01b82e 100644 (file)
@@ -3,9 +3,10 @@ package net.pterodactylus.fcp.quelaton;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Optional;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Future;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Supplier;
 
 import net.pterodactylus.fcp.AllData;
 import net.pterodactylus.fcp.ClientGet;
@@ -14,6 +15,9 @@ import net.pterodactylus.fcp.GetFailed;
 import net.pterodactylus.fcp.Priority;
 import net.pterodactylus.fcp.ReturnType;
 
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
 /**
  * Implementation of the {@link ClientGetCommand}.
  *
@@ -21,10 +25,10 @@ import net.pterodactylus.fcp.ReturnType;
  */
 class ClientGetCommandImpl implements ClientGetCommand {
 
-       private final ExecutorService threadPool;
+       private final ListeningExecutorService threadPool;
        private final ConnectionSupplier connectionSupplier;
+       private final Supplier<String> identifierGenerator;
 
-       private String identifier;
        private boolean ignoreDataStore;
        private boolean dataStoreOnly;
        private Long maxSize;
@@ -32,15 +36,10 @@ class ClientGetCommandImpl implements ClientGetCommand {
        private boolean realTime;
        private boolean global;
 
-       public ClientGetCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
-               this.threadPool = threadPool;
+       public ClientGetCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
+               this.threadPool = MoreExecutors.listeningDecorator(threadPool);
                this.connectionSupplier = connectionSupplier;
-       }
-
-       @Override
-       public ClientGetCommand identifier(String identifier) {
-               this.identifier = identifier;
-               return this;
+               this.identifierGenerator = identifierGenerator;
        }
 
        @Override
@@ -80,12 +79,19 @@ class ClientGetCommandImpl implements ClientGetCommand {
        }
 
        @Override
-       public Future<Optional<Data>> uri(String uri) {
+       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 (ClientGetDialog clientGetDialog = new ClientGetDialog()) {
+                       return clientGetDialog.send(clientGet).get();
+               }
        }
 
        private ClientGet createClientGetCommand(String uri) {
+               String identifier = identifierGenerator.get();
                ClientGet clientGet = new ClientGet(uri, identifier, ReturnType.direct);
                if (ignoreDataStore) {
                        clientGet.setIgnoreDataStore(true);
@@ -108,18 +114,16 @@ class ClientGetCommandImpl implements ClientGetCommand {
                return clientGet;
        }
 
-       private class ClientGetReplySequence extends FcpReplySequence<Optional<Data>> {
+       private class ClientGetDialog extends FcpDialog<Optional<Data>> {
 
                private final AtomicBoolean finished = new AtomicBoolean();
                private final AtomicBoolean failed = new AtomicBoolean();
 
-               private final String identifier = ClientGetCommandImpl.this.identifier;
-
                private String contentType;
                private long dataLength;
                private InputStream payload;
 
-               public ClientGetReplySequence() throws IOException {
+               public ClientGetDialog() throws IOException {
                        super(ClientGetCommandImpl.this.threadPool, ClientGetCommandImpl.this.connectionSupplier.get());
                }
 
@@ -150,30 +154,21 @@ class ClientGetCommandImpl implements ClientGetCommand {
 
                @Override
                protected void consumeAllData(AllData allData) {
-                       if (allData.getIdentifier().equals(identifier)) {
-                               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);
-                                       }
+                       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) {
-                       if (getFailed.getIdentifier().equals(identifier)) {
-                               failed.set(true);
-                       }
-               }
-
-               @Override
-               protected void consumeConnectionClosed(Throwable throwable) {
                        failed.set(true);
                }