189e2e2a0b3d6ac27026d99ab52409d121fc0fb4
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / ModifyPeerCommandImpl.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
10 import net.pterodactylus.fcp.ModifyPeer;
11 import net.pterodactylus.fcp.Peer;
12 import net.pterodactylus.fcp.UnknownNodeIdentifier;
13
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.ListeningExecutorService;
16 import com.google.common.util.concurrent.MoreExecutors;
17
18 /**
19  * Default {@link ModifyPeerCommand} implementation based on {@link FcpDialog}.
20  *
21  * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
22  */
23 public class ModifyPeerCommandImpl implements ModifyPeerCommand {
24
25         private final ListeningExecutorService threadPool;
26         private final ConnectionSupplier connectionSupplier;
27         private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
28         private final AtomicReference<Boolean> enabled = new AtomicReference<>();
29
30         public ModifyPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
31                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
32                 this.connectionSupplier = connectionSupplier;
33         }
34
35         @Override
36         public ModifyPeerCommand enable() {
37                 enabled.set(true);
38                 return this;
39         }
40
41         @Override
42         public ModifyPeerCommand disable() {
43                 enabled.set(false);
44                 return this;
45         }
46
47         @Override
48         public Executable<Optional<Peer>> byName(String name) {
49                 nodeIdentifier.set(name);
50                 return this::execute;
51         }
52
53         @Override
54         public Executable<Optional<Peer>> byIdentity(String nodeIdentity) {
55                 nodeIdentifier.set(nodeIdentity);
56                 return this::execute;
57         }
58
59         @Override
60         public Executable<Optional<Peer>> byHostAndPort(String host, int port) {
61                 nodeIdentifier.set(String.format("%s:%d", host, port));
62                 return this::execute;
63         }
64
65         private ListenableFuture<Optional<Peer>> execute() {
66                 return threadPool.submit(this::executeSequence);
67         }
68
69         private Optional<Peer> executeSequence() throws IOException, ExecutionException, InterruptedException {
70                 ModifyPeer modifyPeer = new ModifyPeer(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
71                 Optional.ofNullable(enabled.get()).ifPresent(enabled -> modifyPeer.setEnabled(enabled));
72                 try (ModifyPeerDialog modifyPeerDialog = new ModifyPeerDialog()) {
73                         return modifyPeerDialog.send(modifyPeer).get();
74                 }
75         }
76
77         private class ModifyPeerDialog extends FcpDialog<Optional<Peer>> {
78
79                 private final AtomicBoolean finished = new AtomicBoolean();
80                 private final AtomicReference<Peer> peer = new AtomicReference<>();
81
82                 public ModifyPeerDialog() throws IOException {
83                         super(threadPool, connectionSupplier.get());
84                 }
85
86                 @Override
87                 protected boolean isFinished() {
88                         return finished.get();
89                 }
90
91                 @Override
92                 protected Optional<Peer> getResult() {
93                         return Optional.ofNullable(peer.get());
94                 }
95
96                 @Override
97                 protected void consumePeer(Peer peer) {
98                         this.peer.set(peer);
99                         finished.set(true);
100                 }
101
102                 @Override
103                 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
104                         finished.set(true);
105                 }
106
107         }
108
109 }