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.AtomicReference;
8 import java.util.function.Supplier;
10 import net.pterodactylus.fcp.ModifyPeer;
11 import net.pterodactylus.fcp.Peer;
12 import net.pterodactylus.fcp.UnknownNodeIdentifier;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.ListeningExecutorService;
16 import com.google.common.util.concurrent.MoreExecutors;
19 * Default {@link ModifyPeerCommand} implementation based on {@link FcpDialog}.
21 * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
23 public class ModifyPeerCommandImpl implements ModifyPeerCommand {
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<>();
35 public ModifyPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
36 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
37 this.connectionSupplier = connectionSupplier;
38 this.identifierGenerator = identifierGenerator;
42 public ModifyPeerCommand enable() {
48 public ModifyPeerCommand disable() {
54 public ModifyPeerCommand allowLocalAddresses() {
55 allowLocalAddresses.set(true);
60 public ModifyPeerCommand disallowLocalAddresses() {
61 allowLocalAddresses.set(false);
66 public ModifyPeerCommand setBurstOnly() {
72 public ModifyPeerCommand clearBurstOnly() {
78 public ModifyPeerCommand setListenOnly() {
84 public ModifyPeerCommand clearListenOnly() {
85 listenOnly.set(false);
90 public ModifyPeerCommand ignoreSource() {
91 ignoreSource.set(true);
96 public ModifyPeerCommand useSource() {
97 ignoreSource.set(false);
102 public Executable<Optional<Peer>> byName(String name) {
103 nodeIdentifier.set(name);
104 return this::execute;
108 public Executable<Optional<Peer>> byIdentity(String nodeIdentity) {
109 nodeIdentifier.set(nodeIdentity);
110 return this::execute;
114 public Executable<Optional<Peer>> byHostAndPort(String host, int port) {
115 nodeIdentifier.set(String.format("%s:%d", host, port));
116 return this::execute;
119 private ListenableFuture<Optional<Peer>> execute() {
120 return threadPool.submit(this::executeDialog);
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();
135 private class ModifyPeerDialog extends FcpDialog<Optional<Peer>> {
137 public ModifyPeerDialog() throws IOException {
138 super(threadPool, connectionSupplier.get(), Optional.<Peer>empty());
142 protected void consumePeer(Peer peer) {
143 setResult(Optional.ofNullable(peer));
147 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {