Add command to modify peer settings
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 13 Jul 2015 17:34:26 +0000 (19:34 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 13 Jul 2015 17:34:59 +0000 (19:34 +0200)
src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java
src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java
src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommand.java [new file with mode: 0644]
src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommandImpl.java [new file with mode: 0644]
src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java

index 09b154b..3dd0b76 100644 (file)
@@ -1,12 +1,14 @@
 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;
@@ -85,6 +87,11 @@ public class DefaultFcpClient implements FcpClient {
        }
 
        @Override
+       public ModifyPeerCommand modifyPeer() {
+               return new ModifyPeerCommandImpl(threadPool, this::connect);
+       }
+
+       @Override
        public ListPeerNotesCommand listPeerNotes() {
                return new ListPeerNotesCommandImpl(threadPool, this::connect);
        }
index 6864e5d..5ae1f0c 100644 (file)
@@ -15,6 +15,7 @@ public interface FcpClient {
        ListPeerCommand listPeer();
        ListPeersCommand listPeers();
        AddPeerCommand addPeer();
+       ModifyPeerCommand modifyPeer();
        ListPeerNotesCommand listPeerNotes();
 
 }
diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommand.java
new file mode 100644 (file)
index 0000000..a608fc2
--- /dev/null
@@ -0,0 +1,17 @@
+package net.pterodactylus.fcp.quelaton;
+
+import java.util.Optional;
+
+import net.pterodactylus.fcp.Peer;
+
+/**
+ * Command that modifies certain settings of a peer.
+ *
+ * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
+ */
+public interface ModifyPeerCommand {
+
+       ModifyPeerCommand enable();
+       Executable<Optional<Peer>> byName(String name);
+
+}
diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommandImpl.java
new file mode 100644 (file)
index 0000000..71fda3d
--- /dev/null
@@ -0,0 +1,91 @@
+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);
+               }
+
+       }
+
+}
index 9eef9ce..1c5b2d7 100644 (file)
@@ -1242,4 +1242,27 @@ public class DefaultFcpClientTest {
                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"));
+       }
+
 }