package net.pterodactylus.fcp.quelaton;
import java.io.IOException;
+import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import net.pterodactylus.fcp.FcpConnection;
+import net.pterodactylus.fcp.Peer;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
}
@Override
+ public ModifyPeerCommand modifyPeer() {
+ return new ModifyPeerCommandImpl(threadPool, this::connect);
+ }
+
+ @Override
public ListPeerNotesCommand listPeerNotes() {
return new ListPeerNotesCommandImpl(threadPool, this::connect);
}
--- /dev/null
+package net.pterodactylus.fcp.quelaton;
+
+import java.io.IOException;
+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 net.pterodactylus.fcp.ModifyPeer;
+import net.pterodactylus.fcp.Peer;
+import net.pterodactylus.fcp.UnknownNodeIdentifier;
+
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
+/**
+ * Default {@link ModifyPeerCommand} implementation based on {@link FcpDialog}.
+ *
+ * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
+ */
+public class ModifyPeerCommandImpl implements ModifyPeerCommand {
+
+ private final ListeningExecutorService threadPool;
+ private final ConnectionSupplier connectionSupplier;
+ private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
+ private final AtomicReference<Boolean> enabled = new AtomicReference<>();
+
+ public ModifyPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
+ this.threadPool = MoreExecutors.listeningDecorator(threadPool);
+ this.connectionSupplier = connectionSupplier;
+ }
+
+ @Override
+ public ModifyPeerCommand enable() {
+ enabled.set(true);
+ return this;
+ }
+
+ @Override
+ public Executable<Optional<Peer>> byName(String name) {
+ nodeIdentifier.set(name);
+ return this::execute;
+ }
+
+ private ListenableFuture<Optional<Peer>> execute() {
+ return threadPool.submit(this::executeSequence);
+ }
+
+ private Optional<Peer> executeSequence() throws IOException, ExecutionException, InterruptedException {
+ ModifyPeer modifyPeer = new ModifyPeer(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
+ Optional.ofNullable(enabled.get()).ifPresent(enabled -> modifyPeer.setEnabled(enabled));
+ try (ModifyPeerDialog modifyPeerDialog = new ModifyPeerDialog()) {
+ return modifyPeerDialog.send(modifyPeer).get();
+ }
+ }
+
+ 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());
+ }
+
+ @Override
+ protected void consumePeer(Peer peer) {
+ this.peer.set(peer);
+ finished.set(true);
+ }
+
+ @Override
+ protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
+ finished.set(true);
+ }
+
+ }
+
+}
assertThat(peerNote.get().get().getPeerNoteType(), is(1));
}
+ @Test
+ public void defaultFcpClientCanEnablePeerByName() throws InterruptedException, ExecutionException, IOException {
+ Future<Optional<Peer>> peer = fcpClient.modifyPeer().enable().byName("Friend1").execute();
+ connectNode();
+ List<String> lines = fcpServer.collectUntil(is("EndMessage"));
+ String identifier = extractIdentifier(lines);
+ assertThat(lines, matchesFcpMessage(
+ "ModifyPeer",
+ "Identifier=" + identifier,
+ "NodeIdentifier=Friend1",
+ "IsDisabled=false",
+ "EndMessage"
+ ));
+ fcpServer.writeLine(
+ "Peer",
+ "Identifier=" + identifier,
+ "NodeIdentifier=Friend1",
+ "identity=id1",
+ "EndMessage"
+ );
+ assertThat(peer.get().get().getIdentity(), is("id1"));
+ }
+
}