Add AddPeer command that adds a noderef from a file
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / AddPeerCommandImpl.java
1 package net.pterodactylus.fcp.quelaton;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Optional;
6 import java.util.concurrent.ExecutionException;
7 import java.util.concurrent.ExecutorService;
8 import java.util.concurrent.atomic.AtomicBoolean;
9 import java.util.concurrent.atomic.AtomicReference;
10
11 import net.pterodactylus.fcp.AddPeer;
12 import net.pterodactylus.fcp.Peer;
13 import net.pterodactylus.fcp.ProtocolError;
14
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.ListeningExecutorService;
17 import com.google.common.util.concurrent.MoreExecutors;
18
19 /**
20  * Default {@link AddPeerCommand} implementation based on {@link FcpReplySequence}.
21  *
22  * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
23  */
24 public class AddPeerCommandImpl implements AddPeerCommand {
25
26         private final ListeningExecutorService threadPool;
27         private final ConnectionSupplier connectionSupplier;
28         private final AtomicReference<File> file = new AtomicReference<>();
29
30         public AddPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
31                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
32                 this.connectionSupplier = connectionSupplier;
33         }
34
35         @Override
36         public Executable<Optional<Peer>> withFile(File file) {
37                 this.file.set(file);
38                 return this::execute;
39         }
40
41         private ListenableFuture<Optional<Peer>> execute() {
42                 return threadPool.submit(this::executeSequence);
43         }
44
45         private Optional<Peer> executeSequence() throws IOException, ExecutionException, InterruptedException {
46                 AddPeer addPeer = null;
47                 if (file.get() != null) {
48                         addPeer = new AddPeer(new RandomIdentifierGenerator().generate(), file.get().getPath());
49                 }
50                 try (AddPeerSequence addPeerSequence = new AddPeerSequence()) {
51                         return addPeerSequence.send(addPeer).get();
52                 }
53         }
54
55         private class AddPeerSequence extends FcpReplySequence<Optional<Peer>> {
56
57                 private final AtomicBoolean finished = new AtomicBoolean();
58                 private final AtomicReference<Peer> peer = new AtomicReference<>();
59
60                 public AddPeerSequence() throws IOException {
61                         super(threadPool, connectionSupplier.get());
62                 }
63
64                 @Override
65                 protected boolean isFinished() {
66                         return finished.get();
67                 }
68
69                 @Override
70                 protected Optional<Peer> getResult() {
71                         return Optional.ofNullable(peer.get());
72                 }
73
74                 @Override
75                 protected void consumePeer(Peer peer) {
76                         this.peer.set(peer);
77                         finished.set(true);
78                 }
79
80                 @Override
81                 protected void consumeProtocolError(ProtocolError protocolError) {
82                         finished.set(true);
83                 }
84
85         }
86
87 }