fbfb3142b6e9f8b90a38996b38fa46793a3edcb9
[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.Peer;
14 import net.pterodactylus.fcp.ProtocolError;
15
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.ListeningExecutorService;
18 import com.google.common.util.concurrent.MoreExecutors;
19
20 /**
21  * Default {@link AddPeerCommand} implementation based on {@link FcpReplySequence}.
22  *
23  * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
24  */
25 public class AddPeerCommandImpl implements AddPeerCommand {
26
27         private final ListeningExecutorService threadPool;
28         private final ConnectionSupplier connectionSupplier;
29         private final AtomicReference<File> file = new AtomicReference<>();
30         private final AtomicReference<URL> url = new AtomicReference<>();
31
32         public AddPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
33                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
34                 this.connectionSupplier = connectionSupplier;
35         }
36
37         @Override
38         public Executable<Optional<Peer>> withFile(File file) {
39                 this.file.set(file);
40                 return this::execute;
41         }
42
43         @Override
44         public Executable<Optional<Peer>> fromURL(URL url) {
45                 this.url.set(url);
46                 return this::execute;
47         }
48
49         private ListenableFuture<Optional<Peer>> execute() {
50                 return threadPool.submit(this::executeSequence);
51         }
52
53         private Optional<Peer> executeSequence() throws IOException, ExecutionException, InterruptedException {
54                 AddPeer addPeer = null;
55                 if (file.get() != null) {
56                         addPeer = new AddPeer(new RandomIdentifierGenerator().generate(), file.get().getPath());
57                 } else if (url.get() != null) {
58                         addPeer = new AddPeer(new RandomIdentifierGenerator().generate(), url.get());
59                 }
60                 try (AddPeerSequence addPeerSequence = new AddPeerSequence()) {
61                         return addPeerSequence.send(addPeer).get();
62                 }
63         }
64
65         private class AddPeerSequence extends FcpReplySequence<Optional<Peer>> {
66
67                 private final AtomicBoolean finished = new AtomicBoolean();
68                 private final AtomicReference<Peer> peer = new AtomicReference<>();
69
70                 public AddPeerSequence() throws IOException {
71                         super(threadPool, connectionSupplier.get());
72                 }
73
74                 @Override
75                 protected boolean isFinished() {
76                         return finished.get();
77                 }
78
79                 @Override
80                 protected Optional<Peer> getResult() {
81                         return Optional.ofNullable(peer.get());
82                 }
83
84                 @Override
85                 protected void consumePeer(Peer peer) {
86                         this.peer.set(peer);
87                         finished.set(true);
88                 }
89
90                 @Override
91                 protected void consumeProtocolError(ProtocolError protocolError) {
92                         finished.set(true);
93                 }
94
95         }
96
97 }