Use a single identifier generator in all commands
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / ClientGetCommandImpl.java
index 98c416f..d01b82e 100644 (file)
@@ -3,19 +3,21 @@ 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.concurrent.atomic.AtomicReference;
+import java.util.function.Supplier;
 
 import net.pterodactylus.fcp.AllData;
 import net.pterodactylus.fcp.ClientGet;
-import net.pterodactylus.fcp.FcpMessage;
 import net.pterodactylus.fcp.FcpUtils.TempInputStream;
 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}.
  *
@@ -23,8 +25,9 @@ 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 boolean ignoreDataStore;
        private boolean dataStoreOnly;
@@ -33,9 +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;
+               this.identifierGenerator = identifierGenerator;
        }
 
        @Override
@@ -75,13 +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 = new RandomIdentifierGenerator().generate();
+               String identifier = identifierGenerator.get();
                ClientGet clientGet = new ClientGet(uri, identifier, ReturnType.direct);
                if (ignoreDataStore) {
                        clientGet.setIgnoreDataStore(true);
@@ -104,9 +114,8 @@ class ClientGetCommandImpl implements ClientGetCommand {
                return clientGet;
        }
 
-       private class ClientGetReplySequence extends FcpReplySequence<Optional<Data>> {
+       private class ClientGetDialog extends FcpDialog<Optional<Data>> {
 
-               private final AtomicReference<String> identifier = new AtomicReference<>();
                private final AtomicBoolean finished = new AtomicBoolean();
                private final AtomicBoolean failed = new AtomicBoolean();
 
@@ -114,7 +123,7 @@ class ClientGetCommandImpl implements ClientGetCommand {
                private long dataLength;
                private InputStream payload;
 
-               public ClientGetReplySequence() throws IOException {
+               public ClientGetDialog() throws IOException {
                        super(ClientGetCommandImpl.this.threadPool, ClientGetCommandImpl.this.connectionSupplier.get());
                }
 
@@ -145,39 +154,24 @@ class ClientGetCommandImpl implements ClientGetCommand {
 
                @Override
                protected void consumeAllData(AllData allData) {
-                       if (allData.getIdentifier().equals(identifier.get())) {
-                               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.get())) {
-                               failed.set(true);
-                       }
-               }
-
-               @Override
-               protected void consumeConnectionClosed(Throwable throwable) {
                        failed.set(true);
                }
 
-               @Override
-               public Future<Optional<Data>> send(FcpMessage fcpMessage) throws IOException {
-                       identifier.set(fcpMessage.getField("Identifier"));
-                       return super.send(fcpMessage);
-               }
-
        }
 
 }