Use a single identifier generator in all commands
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / ListPeersCommandImpl.java
index c084cb5..6a352d5 100644 (file)
@@ -3,9 +3,10 @@ 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 java.util.function.Supplier;
 
 import net.pterodactylus.fcp.EndListPeers;
 import net.pterodactylus.fcp.ListPeers;
@@ -16,7 +17,7 @@ import com.google.common.util.concurrent.ListeningExecutorService;
 import com.google.common.util.concurrent.MoreExecutors;
 
 /**
- * Default {@link ListPeersCommand} implementation based on {@link FcpReplySequence}.
+ * Default {@link ListPeersCommand} implementation based on {@link FcpDialog}.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
@@ -24,12 +25,14 @@ public class ListPeersCommandImpl implements ListPeersCommand {
 
        private final ListeningExecutorService threadPool;
        private final ConnectionSupplier connectionSupplier;
+       private final Supplier<String> identifierGenerator;
        private final AtomicBoolean includeMetadata = new AtomicBoolean(false);
        private final AtomicBoolean includeVolatile = new AtomicBoolean(false);
 
-       public ListPeersCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
+       public ListPeersCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
                this.threadPool = MoreExecutors.listeningDecorator(threadPool);
                this.connectionSupplier = connectionSupplier;
+               this.identifierGenerator = identifierGenerator;
        }
 
        @Override
@@ -46,17 +49,22 @@ public class ListPeersCommandImpl implements ListPeersCommand {
 
        @Override
        public ListenableFuture<Collection<Peer>> execute() {
-               String identifier = new RandomIdentifierGenerator().generate();
-               ListPeers listPeers = new ListPeers(identifier, includeMetadata.get(), includeVolatile.get());
-               return threadPool.submit(() -> new ListPeersReplySequence().send(listPeers).get());
+               return threadPool.submit(this::executeDialog);
        }
 
-       private class ListPeersReplySequence extends FcpReplySequence<Collection<Peer>> {
+       private Collection<Peer> executeDialog() throws InterruptedException, ExecutionException, IOException {
+               ListPeers listPeers = new ListPeers(identifierGenerator.get(), includeMetadata.get(), includeVolatile.get());
+               try (ListPeersDialog listPeersDialog = new ListPeersDialog()) {
+                       return listPeersDialog.send(listPeers).get();
+               }
+       }
+
+       private class ListPeersDialog extends FcpDialog<Collection<Peer>> {
 
                private final Collection<Peer> peers = new HashSet<>();
                private final AtomicBoolean finished = new AtomicBoolean(false);
 
-               public ListPeersReplySequence() throws IOException {
+               public ListPeersDialog() throws IOException {
                        super(threadPool, connectionSupplier.get());
                }