1 package net.pterodactylus.fcp.quelaton;
3 import java.io.IOException;
4 import java.nio.charset.StandardCharsets;
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.FreenetBase64;
12 import net.pterodactylus.fcp.ModifyPeerNote;
13 import net.pterodactylus.fcp.PeerNote;
14 import net.pterodactylus.fcp.PeerNoteType;
15 import net.pterodactylus.fcp.UnknownNodeIdentifier;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import com.google.common.util.concurrent.ListeningExecutorService;
20 import com.google.common.util.concurrent.MoreExecutors;
23 * Default {@link ModifyPeerNoteCommand} implementation based on {@link FcpDialog}.
25 * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
27 public class ModifyPeerNoteCommandImpl implements ModifyPeerNoteCommand {
29 private static final FreenetBase64 BASE_64 = new FreenetBase64();
30 private final ListeningExecutorService threadPool;
31 private final ConnectionSupplier connectionSupplier;
32 private final Supplier<String> identifierGenerator;
33 private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
34 private final AtomicReference<String> darknetComment = new AtomicReference<>();
36 public ModifyPeerNoteCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
37 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
38 this.connectionSupplier = connectionSupplier;
39 this.identifierGenerator = identifierGenerator;
43 public ModifyPeerNoteCommand darknetComment(String text) {
44 darknetComment.set(text);
49 public Executable<Boolean> byName(String name) {
50 nodeIdentifier.set(name);
55 public Executable<Boolean> byIdentifier(String identifier) {
56 nodeIdentifier.set(identifier);
61 public Executable<Boolean> byHostAndPort(String host, int port) {
62 nodeIdentifier.set(String.format("%s:%d", host, port));
66 private ListenableFuture<Boolean> execute() {
67 if (darknetComment.get() == null) {
68 return Futures.immediateFuture(false);
70 return threadPool.submit(this::executeDialog);
73 private Boolean executeDialog() throws IOException, ExecutionException, InterruptedException {
74 ModifyPeerNote modifyPeerNote =
75 new ModifyPeerNote(identifierGenerator.get(), nodeIdentifier.get());
76 modifyPeerNote.setPeerNoteType(PeerNoteType.PRIVATE_DARKNET_COMMENT);
77 modifyPeerNote.setNoteText(BASE_64.encode(darknetComment.get().getBytes(StandardCharsets.UTF_8)));
78 try (ModifyPeerNoteDialog modifyPeerNoteDialog = new ModifyPeerNoteDialog()) {
79 return modifyPeerNoteDialog.send(modifyPeerNote).get();
83 private class ModifyPeerNoteDialog extends FcpDialog<Boolean> {
85 private final AtomicBoolean finished = new AtomicBoolean();
86 private final AtomicBoolean successful = new AtomicBoolean();
88 public ModifyPeerNoteDialog() throws IOException {
89 super(threadPool, connectionSupplier.get());
93 protected boolean isFinished() {
94 return finished.get();
98 protected Boolean getResult() {
99 return successful.get();
103 protected void consumePeerNote(PeerNote peerNote) {
104 successful.set(true);
109 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {