Refactor FCP dialog
[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.AtomicReference;
8 import java.util.function.Supplier;
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 Supplier<String> identifierGenerator;
28         private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
29         private final AtomicReference<Boolean> enabled = new AtomicReference<>();
30         private final AtomicReference<Boolean> allowLocalAddresses = new AtomicReference<>();
31         private final AtomicReference<Boolean> burstOnly = new AtomicReference<>();
32         private final AtomicReference<Boolean> listenOnly = new AtomicReference<>();
33         private final AtomicReference<Boolean> ignoreSource = new AtomicReference<>();
34
35         public ModifyPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
36                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
37                 this.connectionSupplier = connectionSupplier;
38                 this.identifierGenerator = identifierGenerator;
39         }
40
41         @Override
42         public ModifyPeerCommand enable() {
43                 enabled.set(true);
44                 return this;
45         }
46
47         @Override
48         public ModifyPeerCommand disable() {
49                 enabled.set(false);
50                 return this;
51         }
52
53         @Override
54         public ModifyPeerCommand allowLocalAddresses() {
55                 allowLocalAddresses.set(true);
56                 return this;
57         }
58
59         @Override
60         public ModifyPeerCommand disallowLocalAddresses() {
61                 allowLocalAddresses.set(false);
62                 return this;
63         }
64
65         @Override
66         public ModifyPeerCommand setBurstOnly() {
67                 burstOnly.set(true);
68                 return this;
69         }
70
71         @Override
72         public ModifyPeerCommand clearBurstOnly() {
73                 burstOnly.set(false);
74                 return this;
75         }
76
77         @Override
78         public ModifyPeerCommand setListenOnly() {
79                 listenOnly.set(true);
80                 return this;
81         }
82
83         @Override
84         public ModifyPeerCommand clearListenOnly() {
85                 listenOnly.set(false);
86                 return this;
87         }
88
89         @Override
90         public ModifyPeerCommand ignoreSource() {
91                 ignoreSource.set(true);
92                 return this;
93         }
94
95         @Override
96         public ModifyPeerCommand useSource() {
97                 ignoreSource.set(false);
98                 return this;
99         }
100
101         @Override
102         public Executable<Optional<Peer>> byName(String name) {
103                 nodeIdentifier.set(name);
104                 return this::execute;
105         }
106
107         @Override
108         public Executable<Optional<Peer>> byIdentity(String nodeIdentity) {
109                 nodeIdentifier.set(nodeIdentity);
110                 return this::execute;
111         }
112
113         @Override
114         public Executable<Optional<Peer>> byHostAndPort(String host, int port) {
115                 nodeIdentifier.set(String.format("%s:%d", host, port));
116                 return this::execute;
117         }
118
119         private ListenableFuture<Optional<Peer>> execute() {
120                 return threadPool.submit(this::executeDialog);
121         }
122
123         private Optional<Peer> executeDialog() throws IOException, ExecutionException, InterruptedException {
124                 ModifyPeer modifyPeer = new ModifyPeer(identifierGenerator.get(), nodeIdentifier.get());
125                 Optional.ofNullable(enabled.get()).ifPresent(enabled -> modifyPeer.setEnabled(enabled));
126                 Optional.ofNullable(allowLocalAddresses.get()).ifPresent(allowed -> modifyPeer.setAllowLocalAddresses(allowed));
127                 Optional.ofNullable(burstOnly.get()).ifPresent(burstOnly -> modifyPeer.setBurstOnly(burstOnly));
128                 Optional.ofNullable(listenOnly.get()).ifPresent(listenOnly -> modifyPeer.setListenOnly(listenOnly));
129                 Optional.ofNullable(ignoreSource.get()).ifPresent(ignoreSource -> modifyPeer.setIgnoreSource(ignoreSource));
130                 try (ModifyPeerDialog modifyPeerDialog = new ModifyPeerDialog()) {
131                         return modifyPeerDialog.send(modifyPeer).get();
132                 }
133         }
134
135         private class ModifyPeerDialog extends FcpDialog<Optional<Peer>> {
136
137                 public ModifyPeerDialog() throws IOException {
138                         super(threadPool, connectionSupplier.get(), Optional.<Peer>empty());
139                 }
140
141                 @Override
142                 protected void consumePeer(Peer peer) {
143                         setResult(Optional.ofNullable(peer));
144                 }
145
146                 @Override
147                 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
148                         finish();
149                 }
150
151         }
152
153 }