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;
10 import net.pterodactylus.fcp.FreenetBase64;
11 import net.pterodactylus.fcp.ModifyPeerNote;
12 import net.pterodactylus.fcp.PeerNote;
13 import net.pterodactylus.fcp.PeerNoteType;
14 import net.pterodactylus.fcp.UnknownNodeIdentifier;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import com.google.common.util.concurrent.ListeningExecutorService;
19 import com.google.common.util.concurrent.MoreExecutors;
22 * Default {@link ModifyPeerNoteCommand} implementation based on {@link FcpDialog}.
24 * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
26 public class ModifyPeerNoteCommandImpl implements ModifyPeerNoteCommand {
28 private static final FreenetBase64 BASE_64 = new FreenetBase64();
29 private static final RandomIdentifierGenerator RANDOM_IDENTIFIER_GENERATOR = new RandomIdentifierGenerator();
30 private final ListeningExecutorService threadPool;
31 private final ConnectionSupplier connectionSupplier;
32 private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
33 private final AtomicReference<String> darknetComment = new AtomicReference<>();
35 public ModifyPeerNoteCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
36 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
37 this.connectionSupplier = connectionSupplier;
41 public ModifyPeerNoteCommand darknetComment(String text) {
42 darknetComment.set(text);
47 public Executable<Boolean> byName(String name) {
48 nodeIdentifier.set(name);
52 private ListenableFuture<Boolean> execute() {
53 if (darknetComment.get() == null) {
54 return Futures.immediateFuture(false);
56 return threadPool.submit(this::executeDialog);
59 private Boolean executeDialog() throws IOException, ExecutionException, InterruptedException {
60 ModifyPeerNote modifyPeerNote =
61 new ModifyPeerNote(RANDOM_IDENTIFIER_GENERATOR.generate(), nodeIdentifier.get());
62 modifyPeerNote.setPeerNoteType(PeerNoteType.PRIVATE_DARKNET_COMMENT);
63 modifyPeerNote.setNoteText(BASE_64.encode(darknetComment.get().getBytes(StandardCharsets.UTF_8)));
64 try (ModifyPeerNoteDialog modifyPeerNoteDialog = new ModifyPeerNoteDialog()) {
65 return modifyPeerNoteDialog.send(modifyPeerNote).get();
69 private class ModifyPeerNoteDialog extends FcpDialog<Boolean> {
71 private final AtomicBoolean finished = new AtomicBoolean();
72 private final AtomicBoolean successful = new AtomicBoolean();
74 public ModifyPeerNoteDialog() throws IOException {
75 super(threadPool, connectionSupplier.get());
79 protected boolean isFinished() {
80 return finished.get();
84 protected Boolean getResult() {
85 return successful.get();
89 protected void consumePeerNote(PeerNote peerNote) {
95 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {