fc9aeec7bacc771f45258243cadb2eb337dd55bc
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / ListPeerCommandImpl.java
1 package net.pterodactylus.fcp.quelaton;
2
3 import java.io.IOException;
4 import java.util.Optional;
5 import java.util.concurrent.ExecutionException;
6 import java.util.concurrent.ExecutorService;
7 import java.util.concurrent.atomic.AtomicBoolean;
8 import java.util.concurrent.atomic.AtomicReference;
9 import java.util.function.Supplier;
10
11 import net.pterodactylus.fcp.ListPeer;
12 import net.pterodactylus.fcp.Peer;
13 import net.pterodactylus.fcp.UnknownNodeIdentifier;
14
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.ListeningExecutorService;
17 import com.google.common.util.concurrent.MoreExecutors;
18
19 /**
20  * Default {@link ListPeerCommand} implementation based on {@link FcpDialog}.
21  *
22  * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
23  */
24 public class ListPeerCommandImpl implements ListPeerCommand {
25
26         private final ListeningExecutorService threadPool;
27         private final ConnectionSupplier connectionSupplier;
28         private final Supplier<String> identifierGenerator;
29         private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
30
31         public ListPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
32                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
33                 this.connectionSupplier = connectionSupplier;
34                 this.identifierGenerator = identifierGenerator;
35         }
36
37         @Override
38         public Executable<Optional<Peer>> byName(String name) {
39                 nodeIdentifier.set(name);
40                 return this::execute;
41         }
42
43         @Override
44         public Executable<Optional<Peer>> byIdentity(String identity) {
45                 nodeIdentifier.set(identity);
46                 return this::execute;
47         }
48
49         @Override
50         public Executable<Optional<Peer>> byHostAndPort(String host, int port) {
51                 nodeIdentifier.set(String.format("%s:%d", host, port));
52                 return this::execute;
53         }
54
55         private ListenableFuture<Optional<Peer>> execute() {
56                 return threadPool.submit(this::executeDialog);
57         }
58
59         private Optional<Peer> executeDialog() throws IOException, ExecutionException, InterruptedException {
60                 ListPeer listPeer = new ListPeer(identifierGenerator.get(), nodeIdentifier.get());
61                 try (ListPeerDialog listPeerDialog = new ListPeerDialog()) {
62                         return Optional.ofNullable(listPeerDialog.send(listPeer).get());
63                 }
64         }
65
66         private class ListPeerDialog extends FcpDialog<Peer> {
67
68                 private final AtomicBoolean finished = new AtomicBoolean();
69                 private final AtomicReference<Peer> peer = new AtomicReference<>();
70
71                 public ListPeerDialog() throws IOException {
72                         super(threadPool, connectionSupplier.get());
73                 }
74
75                 @Override
76                 protected boolean isFinished() {
77                         return finished.get();
78                 }
79
80                 @Override
81                 protected Peer getResult() {
82                         return peer.get();
83                 }
84
85                 @Override
86                 protected void consumePeer(Peer peer) {
87                         this.peer.set(peer);
88                         finished.set(true);
89                 }
90
91                 @Override
92                 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
93                         finished.set(true);
94                 }
95
96         }
97
98 }