5dfef6f57594a6283373950591343641b3e3d08b
[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.net.URL;
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;
11
12 import net.pterodactylus.fcp.AddPeer;
13 import net.pterodactylus.fcp.NodeRef;
14 import net.pterodactylus.fcp.Peer;
15 import net.pterodactylus.fcp.ProtocolError;
16
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 AddPeerCommand} implementation based on {@link FcpReplySequence}.
23  *
24  * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
25  */
26 public class AddPeerCommandImpl implements AddPeerCommand {
27
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<>();
33
34         public AddPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
35                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
36                 this.connectionSupplier = connectionSupplier;
37         }
38
39         @Override
40         public Executable<Optional<Peer>> fromFile(File file) {
41                 this.file.set(file);
42                 return this::execute;
43         }
44
45         @Override
46         public Executable<Optional<Peer>> fromURL(URL url) {
47                 this.url.set(url);
48                 return this::execute;
49         }
50
51         @Override
52         public Executable<Optional<Peer>> fromNodeRef(NodeRef nodeRef) {
53                 this.nodeRef.set(nodeRef);
54                 return this::execute;
55         }
56
57         private ListenableFuture<Optional<Peer>> execute() {
58                 return threadPool.submit(this::executeSequence);
59         }
60
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());
67                 } else {
68                         addPeer = new AddPeer(new RandomIdentifierGenerator().generate(), nodeRef.get());
69                 }
70                 try (AddPeerSequence addPeerSequence = new AddPeerSequence()) {
71                         return addPeerSequence.send(addPeer).get();
72                 }
73         }
74
75         private class AddPeerSequence extends FcpReplySequence<Optional<Peer>> {
76
77                 private final AtomicBoolean finished = new AtomicBoolean();
78                 private final AtomicReference<Peer> peer = new AtomicReference<>();
79
80                 public AddPeerSequence() throws IOException {
81                         super(threadPool, connectionSupplier.get());
82                 }
83
84                 @Override
85                 protected boolean isFinished() {
86                         return finished.get();
87                 }
88
89                 @Override
90                 protected Optional<Peer> getResult() {
91                         return Optional.ofNullable(peer.get());
92                 }
93
94                 @Override
95                 protected void consumePeer(Peer peer) {
96                         this.peer.set(peer);
97                         finished.set(true);
98                 }
99
100                 @Override
101                 protected void consumeProtocolError(ProtocolError protocolError) {
102                         finished.set(true);
103                 }
104
105         }
106
107 }