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<>();
30 private final AtomicReference<Boolean> burstOnly = new AtomicReference<>();
31 private final AtomicReference<Boolean> listenOnly = new AtomicReference<>();
33 public ModifyPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
34 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
35 this.connectionSupplier = connectionSupplier;
39 public ModifyPeerCommand enable() {
45 public ModifyPeerCommand disable() {
51 public ModifyPeerCommand allowLocalAddresses() {
52 allowLocalAddresses.set(true);
57 public ModifyPeerCommand disallowLocalAddresses() {
58 allowLocalAddresses.set(false);
63 public ModifyPeerCommand setBurstOnly() {
69 public ModifyPeerCommand clearBurstOnly() {
75 public ModifyPeerCommand setListenOnly() {
81 public ModifyPeerCommand clearListenOnly() {
82 listenOnly.set(false);
87 public Executable<Optional<Peer>> byName(String name) {
88 nodeIdentifier.set(name);
93 public Executable<Optional<Peer>> byIdentity(String nodeIdentity) {
94 nodeIdentifier.set(nodeIdentity);
99 public Executable<Optional<Peer>> byHostAndPort(String host, int port) {
100 nodeIdentifier.set(String.format("%s:%d", host, port));
101 return this::execute;
104 private ListenableFuture<Optional<Peer>> execute() {
105 return threadPool.submit(this::executeSequence);
108 private Optional<Peer> executeSequence() throws IOException, ExecutionException, InterruptedException {
109 ModifyPeer modifyPeer = new ModifyPeer(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
110 Optional.ofNullable(enabled.get()).ifPresent(enabled -> modifyPeer.setEnabled(enabled));
111 Optional.ofNullable(allowLocalAddresses.get()).ifPresent(allowed -> modifyPeer.setAllowLocalAddresses(allowed));
112 Optional.ofNullable(burstOnly.get()).ifPresent(burstOnly -> modifyPeer.setBurstOnly(burstOnly));
113 Optional.ofNullable(listenOnly.get()).ifPresent(listenOnly -> modifyPeer.setListenOnly(listenOnly));
114 try (ModifyPeerDialog modifyPeerDialog = new ModifyPeerDialog()) {
115 return modifyPeerDialog.send(modifyPeer).get();
119 private class ModifyPeerDialog extends FcpDialog<Optional<Peer>> {
121 private final AtomicBoolean finished = new AtomicBoolean();
122 private final AtomicReference<Peer> peer = new AtomicReference<>();
124 public ModifyPeerDialog() throws IOException {
125 super(threadPool, connectionSupplier.get());
129 protected boolean isFinished() {
130 return finished.get();
134 protected Optional<Peer> getResult() {
135 return Optional.ofNullable(peer.get());
139 protected void consumePeer(Peer peer) {
145 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {