1 package net.pterodactylus.fcp.quelaton;
4 import java.io.IOException;
6 import java.util.Optional;
7 import java.util.concurrent.ExecutionException;
8 import java.util.concurrent.ExecutorService;
9 import java.util.concurrent.atomic.AtomicBoolean;
10 import java.util.concurrent.atomic.AtomicReference;
12 import net.pterodactylus.fcp.AddPeer;
13 import net.pterodactylus.fcp.NodeRef;
14 import net.pterodactylus.fcp.Peer;
15 import net.pterodactylus.fcp.ProtocolError;
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 AddPeerCommand} implementation based on {@link FcpReplySequence}.
24 * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
26 public class AddPeerCommandImpl implements AddPeerCommand {
28 private final ListeningExecutorService threadPool;
29 private final ConnectionSupplier connectionSupplier;
30 private final AtomicReference<File> file = new AtomicReference<>();
31 private final AtomicReference<URL> url = new AtomicReference<>();
32 private final AtomicReference<NodeRef> nodeRef = new AtomicReference<>();
34 public AddPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
35 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
36 this.connectionSupplier = connectionSupplier;
40 public Executable<Optional<Peer>> fromFile(File file) {
46 public Executable<Optional<Peer>> fromURL(URL url) {
52 public Executable<Optional<Peer>> fromNodeRef(NodeRef nodeRef) {
53 this.nodeRef.set(nodeRef);
57 private ListenableFuture<Optional<Peer>> execute() {
58 return threadPool.submit(this::executeSequence);
61 private Optional<Peer> executeSequence() throws IOException, ExecutionException, InterruptedException {
62 AddPeer addPeer = null;
63 if (file.get() != null) {
64 addPeer = new AddPeer(new RandomIdentifierGenerator().generate(), file.get().getPath());
65 } else if (url.get() != null) {
66 addPeer = new AddPeer(new RandomIdentifierGenerator().generate(), url.get());
68 addPeer = new AddPeer(new RandomIdentifierGenerator().generate(), nodeRef.get());
70 try (AddPeerSequence addPeerSequence = new AddPeerSequence()) {
71 return addPeerSequence.send(addPeer).get();
75 private class AddPeerSequence extends FcpReplySequence<Optional<Peer>> {
77 private final AtomicBoolean finished = new AtomicBoolean();
78 private final AtomicReference<Peer> peer = new AtomicReference<>();
80 public AddPeerSequence() throws IOException {
81 super(threadPool, connectionSupplier.get());
85 protected boolean isFinished() {
86 return finished.get();
90 protected Optional<Peer> getResult() {
91 return Optional.ofNullable(peer.get());
95 protected void consumePeer(Peer peer) {
101 protected void consumeProtocolError(ProtocolError protocolError) {