Add method to modify peer note by host and port
[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.AtomicBoolean;
8 import java.util.concurrent.atomic.AtomicReference;
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 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<>();
34
35         public ModifyPeerNoteCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
36                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
37                 this.connectionSupplier = connectionSupplier;
38         }
39
40         @Override
41         public ModifyPeerNoteCommand darknetComment(String text) {
42                 darknetComment.set(text);
43                 return this;
44         }
45
46         @Override
47         public Executable<Boolean> byName(String name) {
48                 nodeIdentifier.set(name);
49                 return this::execute;
50         }
51
52         @Override
53         public Executable<Boolean> byIdentifier(String identifier) {
54                 nodeIdentifier.set(identifier);
55                 return this::execute;
56         }
57
58         @Override
59         public Executable<Boolean> byHostAndPort(String host, int port) {
60                 nodeIdentifier.set(String.format("%s:%d", host, port));
61                 return this::execute;
62         }
63
64         private ListenableFuture<Boolean> execute() {
65                 if (darknetComment.get() == null) {
66                         return Futures.immediateFuture(false);
67                 }
68                 return threadPool.submit(this::executeDialog);
69         }
70
71         private Boolean executeDialog() throws IOException, ExecutionException, InterruptedException {
72                 ModifyPeerNote modifyPeerNote =
73                         new ModifyPeerNote(RANDOM_IDENTIFIER_GENERATOR.generate(), nodeIdentifier.get());
74                 modifyPeerNote.setPeerNoteType(PeerNoteType.PRIVATE_DARKNET_COMMENT);
75                 modifyPeerNote.setNoteText(BASE_64.encode(darknetComment.get().getBytes(StandardCharsets.UTF_8)));
76                 try (ModifyPeerNoteDialog modifyPeerNoteDialog = new ModifyPeerNoteDialog()) {
77                         return modifyPeerNoteDialog.send(modifyPeerNote).get();
78                 }
79         }
80
81         private class ModifyPeerNoteDialog extends FcpDialog<Boolean> {
82
83                 private final AtomicBoolean finished = new AtomicBoolean();
84                 private final AtomicBoolean successful = new AtomicBoolean();
85
86                 public ModifyPeerNoteDialog() throws IOException {
87                         super(threadPool, connectionSupplier.get());
88                 }
89
90                 @Override
91                 protected boolean isFinished() {
92                         return finished.get();
93                 }
94
95                 @Override
96                 protected Boolean getResult() {
97                         return successful.get();
98                 }
99
100                 @Override
101                 protected void consumePeerNote(PeerNote peerNote) {
102                         successful.set(true);
103                         finished.set(true);
104                 }
105
106                 @Override
107                 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
108                         finished.set(true);
109                 }
110
111         }
112
113 }