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