Refactor FCP dialog
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / ModifyPeerNoteCommandImpl.java
1 package net.pterodactylus.fcp.quelaton;
2
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.AtomicReference;
8 import java.util.function.Supplier;
9
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;
15
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;
20
21 /**
22  * Default {@link ModifyPeerNoteCommand} implementation based on {@link FcpDialog}.
23  *
24  * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
25  */
26 public class ModifyPeerNoteCommandImpl implements ModifyPeerNoteCommand {
27
28         private static final FreenetBase64 BASE_64 = new FreenetBase64();
29         private final ListeningExecutorService threadPool;
30         private final ConnectionSupplier connectionSupplier;
31         private final Supplier<String> identifierGenerator;
32         private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
33         private final AtomicReference<String> darknetComment = new AtomicReference<>();
34
35         public ModifyPeerNoteCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
36                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
37                 this.connectionSupplier = connectionSupplier;
38                 this.identifierGenerator = identifierGenerator;
39         }
40
41         @Override
42         public ModifyPeerNoteCommand darknetComment(String text) {
43                 darknetComment.set(text);
44                 return this;
45         }
46
47         @Override
48         public Executable<Boolean> byName(String name) {
49                 nodeIdentifier.set(name);
50                 return this::execute;
51         }
52
53         @Override
54         public Executable<Boolean> byIdentifier(String identifier) {
55                 nodeIdentifier.set(identifier);
56                 return this::execute;
57         }
58
59         @Override
60         public Executable<Boolean> byHostAndPort(String host, int port) {
61                 nodeIdentifier.set(String.format("%s:%d", host, port));
62                 return this::execute;
63         }
64
65         private ListenableFuture<Boolean> execute() {
66                 if (darknetComment.get() == null) {
67                         return Futures.immediateFuture(false);
68                 }
69                 return threadPool.submit(this::executeDialog);
70         }
71
72         private Boolean executeDialog() throws IOException, ExecutionException, InterruptedException {
73                 ModifyPeerNote modifyPeerNote =
74                         new ModifyPeerNote(identifierGenerator.get(), nodeIdentifier.get());
75                 modifyPeerNote.setPeerNoteType(PeerNoteType.PRIVATE_DARKNET_COMMENT);
76                 modifyPeerNote.setNoteText(BASE_64.encode(darknetComment.get().getBytes(StandardCharsets.UTF_8)));
77                 try (ModifyPeerNoteDialog modifyPeerNoteDialog = new ModifyPeerNoteDialog()) {
78                         return modifyPeerNoteDialog.send(modifyPeerNote).get();
79                 }
80         }
81
82         private class ModifyPeerNoteDialog extends FcpDialog<Boolean> {
83
84                 public ModifyPeerNoteDialog() throws IOException {
85                         super(threadPool, connectionSupplier.get(), false);
86                 }
87
88                 @Override
89                 protected void consumePeerNote(PeerNote peerNote) {
90                         setResult(true);
91                 }
92
93                 @Override
94                 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
95                         finish();
96                 }
97
98         }
99
100 }