1 package net.pterodactylus.fcp.quelaton;
3 import java.io.IOException;
4 import java.util.concurrent.ExecutionException;
5 import java.util.concurrent.ExecutorService;
6 import java.util.concurrent.atomic.AtomicBoolean;
7 import java.util.concurrent.atomic.AtomicReference;
9 import net.pterodactylus.fcp.GetNode;
10 import net.pterodactylus.fcp.NodeData;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.ListeningExecutorService;
14 import com.google.common.util.concurrent.MoreExecutors;
17 * Default {@link GetNodeCommandImpl} implementation based on {@link FcpDialog}.
19 * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
21 public class GetNodeCommandImpl implements GetNodeCommand {
23 private final ListeningExecutorService threadPool;
24 private final ConnectionSupplier connectionSupplier;
25 private final AtomicBoolean giveOpennetRef = new AtomicBoolean(false);
26 private final AtomicBoolean includePrivate = new AtomicBoolean(false);
27 private final AtomicBoolean includeVolatile = new AtomicBoolean(false);
29 public GetNodeCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
30 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
31 this.connectionSupplier = connectionSupplier;
35 public GetNodeCommand opennetRef() {
36 giveOpennetRef.set(true);
41 public GetNodeCommand includePrivate() {
42 includePrivate.set(true);
47 public GetNodeCommand includeVolatile() {
48 includeVolatile.set(true);
53 public ListenableFuture<NodeData> execute() {
54 return threadPool.submit(this::executeSequence);
57 private NodeData executeSequence() throws InterruptedException, ExecutionException, IOException {
58 GetNode getNode = new GetNode(new RandomIdentifierGenerator().generate(), giveOpennetRef.get(),
59 includePrivate.get(), includeVolatile.get());
60 try (GetNodeDialog getNodeDialog = new GetNodeDialog()) {
61 return getNodeDialog.send(getNode).get();
65 private class GetNodeDialog extends FcpDialog<NodeData> {
67 private final AtomicReference<NodeData> nodeData = new AtomicReference<>();
69 public GetNodeDialog() throws IOException {
70 super(threadPool, connectionSupplier.get());
74 protected boolean isFinished() {
75 return nodeData.get() != null;
79 protected NodeData getResult() {
80 return nodeData.get();
84 protected void consumeNodeData(NodeData nodeData) {
85 this.nodeData.set(nodeData);