Use a single identifier generator in all commands
[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 import java.util.function.Supplier;
10
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;
16
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;
21
22 /**
23  * Default {@link ModifyPeerNoteCommand} implementation based on {@link FcpDialog}.
24  *
25  * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
26  */
27 public class ModifyPeerNoteCommandImpl implements ModifyPeerNoteCommand {
28
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<>();
35
36         public ModifyPeerNoteCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
37                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
38                 this.connectionSupplier = connectionSupplier;
39                 this.identifierGenerator = identifierGenerator;
40         }
41
42         @Override
43         public ModifyPeerNoteCommand darknetComment(String text) {
44                 darknetComment.set(text);
45                 return this;
46         }
47
48         @Override
49         public Executable<Boolean> byName(String name) {
50                 nodeIdentifier.set(name);
51                 return this::execute;
52         }
53
54         @Override
55         public Executable<Boolean> byIdentifier(String identifier) {
56                 nodeIdentifier.set(identifier);
57                 return this::execute;
58         }
59
60         @Override
61         public Executable<Boolean> byHostAndPort(String host, int port) {
62                 nodeIdentifier.set(String.format("%s:%d", host, port));
63                 return this::execute;
64         }
65
66         private ListenableFuture<Boolean> execute() {
67                 if (darknetComment.get() == null) {
68                         return Futures.immediateFuture(false);
69                 }
70                 return threadPool.submit(this::executeDialog);
71         }
72
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();
80                 }
81         }
82
83         private class ModifyPeerNoteDialog extends FcpDialog<Boolean> {
84
85                 private final AtomicBoolean finished = new AtomicBoolean();
86                 private final AtomicBoolean successful = new AtomicBoolean();
87
88                 public ModifyPeerNoteDialog() throws IOException {
89                         super(threadPool, connectionSupplier.get());
90                 }
91
92                 @Override
93                 protected boolean isFinished() {
94                         return finished.get();
95                 }
96
97                 @Override
98                 protected Boolean getResult() {
99                         return successful.get();
100                 }
101
102                 @Override
103                 protected void consumePeerNote(PeerNote peerNote) {
104                         successful.set(true);
105                         finished.set(true);
106                 }
107
108                 @Override
109                 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
110                         finished.set(true);
111                 }
112
113         }
114
115 }