4b3057222b172335a206ec41e9dd0ba2b7257c88
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / ModifyPeerCommandImpl.java
1 package net.pterodactylus.fcp.quelaton;
2
3 import java.io.IOException;
4 import java.util.Optional;
5 import java.util.concurrent.ExecutionException;
6 import java.util.concurrent.ExecutorService;
7 import java.util.concurrent.atomic.AtomicBoolean;
8 import java.util.concurrent.atomic.AtomicReference;
9
10 import net.pterodactylus.fcp.ModifyPeer;
11 import net.pterodactylus.fcp.Peer;
12 import net.pterodactylus.fcp.UnknownNodeIdentifier;
13
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.ListeningExecutorService;
16 import com.google.common.util.concurrent.MoreExecutors;
17
18 /**
19  * Default {@link ModifyPeerCommand} implementation based on {@link FcpDialog}.
20  *
21  * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
22  */
23 public class ModifyPeerCommandImpl implements ModifyPeerCommand {
24
25         private final ListeningExecutorService threadPool;
26         private final ConnectionSupplier connectionSupplier;
27         private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
28         private final AtomicReference<Boolean> enabled = new AtomicReference<>();
29         private final AtomicReference<Boolean> allowLocalAddresses = new AtomicReference<>();
30
31         public ModifyPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
32                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
33                 this.connectionSupplier = connectionSupplier;
34         }
35
36         @Override
37         public ModifyPeerCommand enable() {
38                 enabled.set(true);
39                 return this;
40         }
41
42         @Override
43         public ModifyPeerCommand disable() {
44                 enabled.set(false);
45                 return this;
46         }
47
48         @Override
49         public ModifyPeerCommand allowLocalAddresses() {
50                 allowLocalAddresses.set(true);
51                 return this;
52         }
53
54         @Override
55         public ModifyPeerCommand disallowLocalAddresses() {
56                 allowLocalAddresses.set(false);
57                 return this;
58         }
59
60         @Override
61         public Executable<Optional<Peer>> byName(String name) {
62                 nodeIdentifier.set(name);
63                 return this::execute;
64         }
65
66         @Override
67         public Executable<Optional<Peer>> byIdentity(String nodeIdentity) {
68                 nodeIdentifier.set(nodeIdentity);
69                 return this::execute;
70         }
71
72         @Override
73         public Executable<Optional<Peer>> byHostAndPort(String host, int port) {
74                 nodeIdentifier.set(String.format("%s:%d", host, port));
75                 return this::execute;
76         }
77
78         private ListenableFuture<Optional<Peer>> execute() {
79                 return threadPool.submit(this::executeSequence);
80         }
81
82         private Optional<Peer> executeSequence() throws IOException, ExecutionException, InterruptedException {
83                 ModifyPeer modifyPeer = new ModifyPeer(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
84                 Optional.ofNullable(enabled.get()).ifPresent(enabled -> modifyPeer.setEnabled(enabled));
85                 Optional.ofNullable(allowLocalAddresses.get()).ifPresent(allowed -> modifyPeer.setAllowLocalAddresses(allowed));
86                 try (ModifyPeerDialog modifyPeerDialog = new ModifyPeerDialog()) {
87                         return modifyPeerDialog.send(modifyPeer).get();
88                 }
89         }
90
91         private class ModifyPeerDialog extends FcpDialog<Optional<Peer>> {
92
93                 private final AtomicBoolean finished = new AtomicBoolean();
94                 private final AtomicReference<Peer> peer = new AtomicReference<>();
95
96                 public ModifyPeerDialog() throws IOException {
97                         super(threadPool, connectionSupplier.get());
98                 }
99
100                 @Override
101                 protected boolean isFinished() {
102                         return finished.get();
103                 }
104
105                 @Override
106                 protected Optional<Peer> getResult() {
107                         return Optional.ofNullable(peer.get());
108                 }
109
110                 @Override
111                 protected void consumePeer(Peer peer) {
112                         this.peer.set(peer);
113                         finished.set(true);
114                 }
115
116                 @Override
117                 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
118                         finished.set(true);
119                 }
120
121         }
122
123 }