Use a single identifier generator in all commands
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / ListPeerCommandImpl.java
index 3fb6b74..fc9aeec 100644 (file)
@@ -3,8 +3,10 @@ package net.pterodactylus.fcp.quelaton;
 import java.io.IOException;
 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.AtomicReference;
+import java.util.function.Supplier;
 
 import net.pterodactylus.fcp.ListPeer;
 import net.pterodactylus.fcp.Peer;
@@ -12,6 +14,7 @@ import net.pterodactylus.fcp.UnknownNodeIdentifier;
 
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
 
 /**
  * Default {@link ListPeerCommand} implementation based on {@link FcpDialog}.
@@ -22,11 +25,13 @@ public class ListPeerCommandImpl implements ListPeerCommand {
 
        private final ListeningExecutorService threadPool;
        private final ConnectionSupplier connectionSupplier;
+       private final Supplier<String> identifierGenerator;
        private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
 
-       public ListPeerCommandImpl(ListeningExecutorService threadPool, ConnectionSupplier connectionSupplier) {
-               this.threadPool = threadPool;
+       public ListPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
+               this.threadPool = MoreExecutors.listeningDecorator(threadPool);
                this.connectionSupplier = connectionSupplier;
+               this.identifierGenerator = identifierGenerator;
        }
 
        @Override
@@ -52,7 +57,7 @@ public class ListPeerCommandImpl implements ListPeerCommand {
        }
 
        private Optional<Peer> executeDialog() throws IOException, ExecutionException, InterruptedException {
-               ListPeer listPeer = new ListPeer(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
+               ListPeer listPeer = new ListPeer(identifierGenerator.get(), nodeIdentifier.get());
                try (ListPeerDialog listPeerDialog = new ListPeerDialog()) {
                        return Optional.ofNullable(listPeerDialog.send(listPeer).get());
                }