35d6d571ed158ee99d6c086588306c1f7ed17cb5
[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 Executable<Optional<Peer>> byName(String name) {
56                 nodeIdentifier.set(name);
57                 return this::execute;
58         }
59
60         @Override
61         public Executable<Optional<Peer>> byIdentity(String nodeIdentity) {
62                 nodeIdentifier.set(nodeIdentity);
63                 return this::execute;
64         }
65
66         @Override
67         public Executable<Optional<Peer>> byHostAndPort(String host, int port) {
68                 nodeIdentifier.set(String.format("%s:%d", host, port));
69                 return this::execute;
70         }
71
72         private ListenableFuture<Optional<Peer>> execute() {
73                 return threadPool.submit(this::executeSequence);
74         }
75
76         private Optional<Peer> executeSequence() throws IOException, ExecutionException, InterruptedException {
77                 ModifyPeer modifyPeer = new ModifyPeer(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
78                 Optional.ofNullable(enabled.get()).ifPresent(enabled -> modifyPeer.setEnabled(enabled));
79                 Optional.ofNullable(allowLocalAddresses.get()).ifPresent(allowed -> modifyPeer.setAllowLocalAddresses(allowed));
80                 try (ModifyPeerDialog modifyPeerDialog = new ModifyPeerDialog()) {
81                         return modifyPeerDialog.send(modifyPeer).get();
82                 }
83         }
84
85         private class ModifyPeerDialog extends FcpDialog<Optional<Peer>> {
86
87                 private final AtomicBoolean finished = new AtomicBoolean();
88                 private final AtomicReference<Peer> peer = new AtomicReference<>();
89
90                 public ModifyPeerDialog() throws IOException {
91                         super(threadPool, connectionSupplier.get());
92                 }
93
94                 @Override
95                 protected boolean isFinished() {
96                         return finished.get();
97                 }
98
99                 @Override
100                 protected Optional<Peer> getResult() {
101                         return Optional.ofNullable(peer.get());
102                 }
103
104                 @Override
105                 protected void consumePeer(Peer peer) {
106                         this.peer.set(peer);
107                         finished.set(true);
108                 }
109
110                 @Override
111                 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
112                         finished.set(true);
113                 }
114
115         }
116
117 }