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;
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 AtomicReference<String> nodeIdentifier = new AtomicReference<>();
28 private final AtomicReference<Boolean> enabled = new AtomicReference<>();
29 private final AtomicReference<Boolean> allowLocalAddresses = new AtomicReference<>();
31 public ModifyPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
32 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
33 this.connectionSupplier = connectionSupplier;
37 public ModifyPeerCommand enable() {
43 public ModifyPeerCommand disable() {
49 public ModifyPeerCommand allowLocalAddresses() {
50 allowLocalAddresses.set(true);
55 public ModifyPeerCommand disallowLocalAddresses() {
56 allowLocalAddresses.set(false);
61 public Executable<Optional<Peer>> byName(String name) {
62 nodeIdentifier.set(name);
67 public Executable<Optional<Peer>> byIdentity(String nodeIdentity) {
68 nodeIdentifier.set(nodeIdentity);
73 public Executable<Optional<Peer>> byHostAndPort(String host, int port) {
74 nodeIdentifier.set(String.format("%s:%d", host, port));
78 private ListenableFuture<Optional<Peer>> execute() {
79 return threadPool.submit(this::executeSequence);
82 private Optional<Peer> executeSequence() throws IOException, ExecutionException, InterruptedException {
83 ModifyPeer modifyPeer = new ModifyPeer(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
84 Optional.ofNullable(enabled.get()).ifPresent(enabled -> modifyPeer.setEnabled(enabled));
85 Optional.ofNullable(allowLocalAddresses.get()).ifPresent(allowed -> modifyPeer.setAllowLocalAddresses(allowed));
86 try (ModifyPeerDialog modifyPeerDialog = new ModifyPeerDialog()) {
87 return modifyPeerDialog.send(modifyPeer).get();
91 private class ModifyPeerDialog extends FcpDialog<Optional<Peer>> {
93 private final AtomicBoolean finished = new AtomicBoolean();
94 private final AtomicReference<Peer> peer = new AtomicReference<>();
96 public ModifyPeerDialog() throws IOException {
97 super(threadPool, connectionSupplier.get());
101 protected boolean isFinished() {
102 return finished.get();
106 protected Optional<Peer> getResult() {
107 return Optional.ofNullable(peer.get());
111 protected void consumePeer(Peer peer) {
117 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {