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