import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
-import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Supplier;
import net.pterodactylus.fcp.ModifyPeer;
import net.pterodactylus.fcp.Peer;
private final ListeningExecutorService threadPool;
private final ConnectionSupplier connectionSupplier;
+ private final Supplier<String> identifierGenerator;
private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
private final AtomicReference<Boolean> enabled = new AtomicReference<>();
private final AtomicReference<Boolean> allowLocalAddresses = new AtomicReference<>();
private final AtomicReference<Boolean> listenOnly = new AtomicReference<>();
private final AtomicReference<Boolean> ignoreSource = new AtomicReference<>();
- public ModifyPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
+ public ModifyPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
this.threadPool = MoreExecutors.listeningDecorator(threadPool);
this.connectionSupplier = connectionSupplier;
+ this.identifierGenerator = identifierGenerator;
}
@Override
}
private ListenableFuture<Optional<Peer>> execute() {
- return threadPool.submit(this::executeSequence);
+ return threadPool.submit(this::executeDialog);
}
- private Optional<Peer> executeSequence() throws IOException, ExecutionException, InterruptedException {
- ModifyPeer modifyPeer = new ModifyPeer(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
+ private Optional<Peer> executeDialog() throws IOException, ExecutionException, InterruptedException {
+ ModifyPeer modifyPeer = new ModifyPeer(identifierGenerator.get(), nodeIdentifier.get());
Optional.ofNullable(enabled.get()).ifPresent(enabled -> modifyPeer.setEnabled(enabled));
Optional.ofNullable(allowLocalAddresses.get()).ifPresent(allowed -> modifyPeer.setAllowLocalAddresses(allowed));
Optional.ofNullable(burstOnly.get()).ifPresent(burstOnly -> modifyPeer.setBurstOnly(burstOnly));
private class ModifyPeerDialog extends FcpDialog<Optional<Peer>> {
- private final AtomicBoolean finished = new AtomicBoolean();
- private final AtomicReference<Peer> peer = new AtomicReference<>();
-
public ModifyPeerDialog() throws IOException {
- super(threadPool, connectionSupplier.get());
- }
-
- @Override
- protected boolean isFinished() {
- return finished.get();
- }
-
- @Override
- protected Optional<Peer> getResult() {
- return Optional.ofNullable(peer.get());
+ super(threadPool, connectionSupplier.get(), Optional.<Peer>empty());
}
@Override
protected void consumePeer(Peer peer) {
- this.peer.set(peer);
- finished.set(true);
+ setResult(Optional.ofNullable(peer));
}
@Override
protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
- finished.set(true);
+ finish();
}
}