Use a single identifier generator in all commands
[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 import java.util.function.Supplier;
10
11 import net.pterodactylus.fcp.ModifyPeer;
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 ModifyPeerCommand} implementation based on {@link FcpDialog}.
21  *
22  * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
23  */
24 public class ModifyPeerCommandImpl implements ModifyPeerCommand {
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         private final AtomicReference<Boolean> enabled = new AtomicReference<>();
31         private final AtomicReference<Boolean> allowLocalAddresses = new AtomicReference<>();
32         private final AtomicReference<Boolean> burstOnly = new AtomicReference<>();
33         private final AtomicReference<Boolean> listenOnly = new AtomicReference<>();
34         private final AtomicReference<Boolean> ignoreSource = new AtomicReference<>();
35
36         public ModifyPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
37                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
38                 this.connectionSupplier = connectionSupplier;
39                 this.identifierGenerator = identifierGenerator;
40         }
41
42         @Override
43         public ModifyPeerCommand enable() {
44                 enabled.set(true);
45                 return this;
46         }
47
48         @Override
49         public ModifyPeerCommand disable() {
50                 enabled.set(false);
51                 return this;
52         }
53
54         @Override
55         public ModifyPeerCommand allowLocalAddresses() {
56                 allowLocalAddresses.set(true);
57                 return this;
58         }
59
60         @Override
61         public ModifyPeerCommand disallowLocalAddresses() {
62                 allowLocalAddresses.set(false);
63                 return this;
64         }
65
66         @Override
67         public ModifyPeerCommand setBurstOnly() {
68                 burstOnly.set(true);
69                 return this;
70         }
71
72         @Override
73         public ModifyPeerCommand clearBurstOnly() {
74                 burstOnly.set(false);
75                 return this;
76         }
77
78         @Override
79         public ModifyPeerCommand setListenOnly() {
80                 listenOnly.set(true);
81                 return this;
82         }
83
84         @Override
85         public ModifyPeerCommand clearListenOnly() {
86                 listenOnly.set(false);
87                 return this;
88         }
89
90         @Override
91         public ModifyPeerCommand ignoreSource() {
92                 ignoreSource.set(true);
93                 return this;
94         }
95
96         @Override
97         public ModifyPeerCommand useSource() {
98                 ignoreSource.set(false);
99                 return this;
100         }
101
102         @Override
103         public Executable<Optional<Peer>> byName(String name) {
104                 nodeIdentifier.set(name);
105                 return this::execute;
106         }
107
108         @Override
109         public Executable<Optional<Peer>> byIdentity(String nodeIdentity) {
110                 nodeIdentifier.set(nodeIdentity);
111                 return this::execute;
112         }
113
114         @Override
115         public Executable<Optional<Peer>> byHostAndPort(String host, int port) {
116                 nodeIdentifier.set(String.format("%s:%d", host, port));
117                 return this::execute;
118         }
119
120         private ListenableFuture<Optional<Peer>> execute() {
121                 return threadPool.submit(this::executeDialog);
122         }
123
124         private Optional<Peer> executeDialog() throws IOException, ExecutionException, InterruptedException {
125                 ModifyPeer modifyPeer = new ModifyPeer(identifierGenerator.get(), nodeIdentifier.get());
126                 Optional.ofNullable(enabled.get()).ifPresent(enabled -> modifyPeer.setEnabled(enabled));
127                 Optional.ofNullable(allowLocalAddresses.get()).ifPresent(allowed -> modifyPeer.setAllowLocalAddresses(allowed));
128                 Optional.ofNullable(burstOnly.get()).ifPresent(burstOnly -> modifyPeer.setBurstOnly(burstOnly));
129                 Optional.ofNullable(listenOnly.get()).ifPresent(listenOnly -> modifyPeer.setListenOnly(listenOnly));
130                 Optional.ofNullable(ignoreSource.get()).ifPresent(ignoreSource -> modifyPeer.setIgnoreSource(ignoreSource));
131                 try (ModifyPeerDialog modifyPeerDialog = new ModifyPeerDialog()) {
132                         return modifyPeerDialog.send(modifyPeer).get();
133                 }
134         }
135
136         private class ModifyPeerDialog extends FcpDialog<Optional<Peer>> {
137
138                 private final AtomicBoolean finished = new AtomicBoolean();
139                 private final AtomicReference<Peer> peer = new AtomicReference<>();
140
141                 public ModifyPeerDialog() throws IOException {
142                         super(threadPool, connectionSupplier.get());
143                 }
144
145                 @Override
146                 protected boolean isFinished() {
147                         return finished.get();
148                 }
149
150                 @Override
151                 protected Optional<Peer> getResult() {
152                         return Optional.ofNullable(peer.get());
153                 }
154
155                 @Override
156                 protected void consumePeer(Peer peer) {
157                         this.peer.set(peer);
158                         finished.set(true);
159                 }
160
161                 @Override
162                 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
163                         finished.set(true);
164                 }
165
166         }
167
168 }