Add method to clear burst only on peer
[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         private final AtomicReference<Boolean> burstOnly = new AtomicReference<>();
31
32         public ModifyPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
33                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
34                 this.connectionSupplier = connectionSupplier;
35         }
36
37         @Override
38         public ModifyPeerCommand enable() {
39                 enabled.set(true);
40                 return this;
41         }
42
43         @Override
44         public ModifyPeerCommand disable() {
45                 enabled.set(false);
46                 return this;
47         }
48
49         @Override
50         public ModifyPeerCommand allowLocalAddresses() {
51                 allowLocalAddresses.set(true);
52                 return this;
53         }
54
55         @Override
56         public ModifyPeerCommand disallowLocalAddresses() {
57                 allowLocalAddresses.set(false);
58                 return this;
59         }
60
61         @Override
62         public ModifyPeerCommand setBurstOnly() {
63                 burstOnly.set(true);
64                 return this;
65         }
66
67         @Override
68         public ModifyPeerCommand clearBurstOnly() {
69                 burstOnly.set(false);
70                 return this;
71         }
72
73         @Override
74         public Executable<Optional<Peer>> byName(String name) {
75                 nodeIdentifier.set(name);
76                 return this::execute;
77         }
78
79         @Override
80         public Executable<Optional<Peer>> byIdentity(String nodeIdentity) {
81                 nodeIdentifier.set(nodeIdentity);
82                 return this::execute;
83         }
84
85         @Override
86         public Executable<Optional<Peer>> byHostAndPort(String host, int port) {
87                 nodeIdentifier.set(String.format("%s:%d", host, port));
88                 return this::execute;
89         }
90
91         private ListenableFuture<Optional<Peer>> execute() {
92                 return threadPool.submit(this::executeSequence);
93         }
94
95         private Optional<Peer> executeSequence() throws IOException, ExecutionException, InterruptedException {
96                 ModifyPeer modifyPeer = new ModifyPeer(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
97                 Optional.ofNullable(enabled.get()).ifPresent(enabled -> modifyPeer.setEnabled(enabled));
98                 Optional.ofNullable(allowLocalAddresses.get()).ifPresent(allowed -> modifyPeer.setAllowLocalAddresses(allowed));
99                 Optional.ofNullable(burstOnly.get()).ifPresent(burstOnly -> modifyPeer.setBurstOnly(burstOnly));
100                 try (ModifyPeerDialog modifyPeerDialog = new ModifyPeerDialog()) {
101                         return modifyPeerDialog.send(modifyPeer).get();
102                 }
103         }
104
105         private class ModifyPeerDialog extends FcpDialog<Optional<Peer>> {
106
107                 private final AtomicBoolean finished = new AtomicBoolean();
108                 private final AtomicReference<Peer> peer = new AtomicReference<>();
109
110                 public ModifyPeerDialog() throws IOException {
111                         super(threadPool, connectionSupplier.get());
112                 }
113
114                 @Override
115                 protected boolean isFinished() {
116                         return finished.get();
117                 }
118
119                 @Override
120                 protected Optional<Peer> getResult() {
121                         return Optional.ofNullable(peer.get());
122                 }
123
124                 @Override
125                 protected void consumePeer(Peer peer) {
126                         this.peer.set(peer);
127                         finished.set(true);
128                 }
129
130                 @Override
131                 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
132                         finished.set(true);
133                 }
134
135         }
136
137 }