1 package net.pterodactylus.fcp.quelaton;
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;
11 import net.pterodactylus.fcp.ModifyPeer;
12 import net.pterodactylus.fcp.Peer;
13 import net.pterodactylus.fcp.UnknownNodeIdentifier;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.ListeningExecutorService;
17 import com.google.common.util.concurrent.MoreExecutors;
20 * Default {@link ModifyPeerCommand} implementation based on {@link FcpDialog}.
22 * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
24 public class ModifyPeerCommandImpl implements ModifyPeerCommand {
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<>();
36 public ModifyPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
37 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
38 this.connectionSupplier = connectionSupplier;
39 this.identifierGenerator = identifierGenerator;
43 public ModifyPeerCommand enable() {
49 public ModifyPeerCommand disable() {
55 public ModifyPeerCommand allowLocalAddresses() {
56 allowLocalAddresses.set(true);
61 public ModifyPeerCommand disallowLocalAddresses() {
62 allowLocalAddresses.set(false);
67 public ModifyPeerCommand setBurstOnly() {
73 public ModifyPeerCommand clearBurstOnly() {
79 public ModifyPeerCommand setListenOnly() {
85 public ModifyPeerCommand clearListenOnly() {
86 listenOnly.set(false);
91 public ModifyPeerCommand ignoreSource() {
92 ignoreSource.set(true);
97 public ModifyPeerCommand useSource() {
98 ignoreSource.set(false);
103 public Executable<Optional<Peer>> byName(String name) {
104 nodeIdentifier.set(name);
105 return this::execute;
109 public Executable<Optional<Peer>> byIdentity(String nodeIdentity) {
110 nodeIdentifier.set(nodeIdentity);
111 return this::execute;
115 public Executable<Optional<Peer>> byHostAndPort(String host, int port) {
116 nodeIdentifier.set(String.format("%s:%d", host, port));
117 return this::execute;
120 private ListenableFuture<Optional<Peer>> execute() {
121 return threadPool.submit(this::executeDialog);
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();
136 private class ModifyPeerDialog extends FcpDialog<Optional<Peer>> {
138 private final AtomicBoolean finished = new AtomicBoolean();
139 private final AtomicReference<Peer> peer = new AtomicReference<>();
141 public ModifyPeerDialog() throws IOException {
142 super(threadPool, connectionSupplier.get());
146 protected boolean isFinished() {
147 return finished.get();
151 protected Optional<Peer> getResult() {
152 return Optional.ofNullable(peer.get());
156 protected void consumePeer(Peer peer) {
162 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {