Add AddPeer command that adds a noderef from a file
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sat, 11 Jul 2015 09:43:09 +0000 (11:43 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sat, 11 Jul 2015 09:43:09 +0000 (11:43 +0200)
src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommand.java [new file with mode: 0644]
src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommandImpl.java [new file with mode: 0644]
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/WithFile.java [new file with mode: 0644]
src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java

diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommand.java
new file mode 100644 (file)
index 0000000..2434106
--- /dev/null
@@ -0,0 +1,14 @@
+package net.pterodactylus.fcp.quelaton;
+
+import java.util.Optional;
+
+import net.pterodactylus.fcp.Peer;
+
+/**
+ * Command that adds a peer to the node.
+ *
+ * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
+ */
+public interface AddPeerCommand extends WithFile<Executable<Optional<Peer>>> {
+
+}
diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommandImpl.java
new file mode 100644 (file)
index 0000000..67aa563
--- /dev/null
@@ -0,0 +1,87 @@
+package net.pterodactylus.fcp.quelaton;
+
+import java.io.File;
+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.AddPeer;
+import net.pterodactylus.fcp.Peer;
+import net.pterodactylus.fcp.ProtocolError;
+
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
+/**
+ * Default {@link AddPeerCommand} implementation based on {@link FcpReplySequence}.
+ *
+ * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
+ */
+public class AddPeerCommandImpl implements AddPeerCommand {
+
+       private final ListeningExecutorService threadPool;
+       private final ConnectionSupplier connectionSupplier;
+       private final AtomicReference<File> file = new AtomicReference<>();
+
+       public AddPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
+               this.threadPool = MoreExecutors.listeningDecorator(threadPool);
+               this.connectionSupplier = connectionSupplier;
+       }
+
+       @Override
+       public Executable<Optional<Peer>> withFile(File file) {
+               this.file.set(file);
+               return this::execute;
+       }
+
+       private ListenableFuture<Optional<Peer>> execute() {
+               return threadPool.submit(this::executeSequence);
+       }
+
+       private Optional<Peer> executeSequence() throws IOException, ExecutionException, InterruptedException {
+               AddPeer addPeer = null;
+               if (file.get() != null) {
+                       addPeer = new AddPeer(new RandomIdentifierGenerator().generate(), file.get().getPath());
+               }
+               try (AddPeerSequence addPeerSequence = new AddPeerSequence()) {
+                       return addPeerSequence.send(addPeer).get();
+               }
+       }
+
+       private class AddPeerSequence extends FcpReplySequence<Optional<Peer>> {
+
+               private final AtomicBoolean finished = new AtomicBoolean();
+               private final AtomicReference<Peer> peer = new AtomicReference<>();
+
+               public AddPeerSequence() 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 consumeProtocolError(ProtocolError protocolError) {
+                       finished.set(true);
+               }
+
+       }
+
+}
index 6cefb46..574afc6 100644 (file)
@@ -79,5 +79,10 @@ public class DefaultFcpClient implements FcpClient {
                return new ListPeersCommandImpl(threadPool, this::connect);
        }
 
+       @Override
+       public AddPeerCommand addPeer() {
+               return new AddPeerCommandImpl(threadPool, this::connect);
+       }
+
 }
 
index c4c6d51..5b61979 100644 (file)
@@ -14,5 +14,6 @@ public interface FcpClient {
 
        ListPeerCommand listPeer();
        ListPeersCommand listPeers();
+       AddPeerCommand addPeer();
 
 }
diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/WithFile.java b/src/main/java/net/pterodactylus/fcp/quelaton/WithFile.java
new file mode 100644 (file)
index 0000000..85af12a
--- /dev/null
@@ -0,0 +1,16 @@
+package net.pterodactylus.fcp.quelaton;
+
+import java.io.File;
+
+/**
+ * An intermediary interface for FCP commands that require a file parameter.
+ *
+ * @param <R>
+ *     The type of the next command part
+ * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
+ */
+public interface WithFile<R> {
+
+       R withFile(File file);
+
+}
index 9108c17..06a8de0 100644 (file)
@@ -998,4 +998,31 @@ public class DefaultFcpClientTest {
                assertThat(peer.get().isPresent(), is(false));
        }
 
+       @Test
+       public void defaultFcpClientCanAddPeerFromFile() throws InterruptedException, ExecutionException, IOException {
+               Future<Optional<Peer>> peer = fcpClient.addPeer().withFile(new File("/tmp/ref.txt")).execute();
+               connectNode();
+               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
+               String identifier = extractIdentifier(lines);
+               assertThat(lines, matchesFcpMessage(
+                       "AddPeer",
+                       "Identifier=" + identifier,
+                       "File=/tmp/ref.txt",
+                       "EndMessage"
+               ));
+               fcpServer.writeLine(
+                       "Peer",
+                       "Identifier=" + identifier,
+                       "identity=id1",
+                       "opennet=false",
+                       "ark.pubURI=SSK@3YEf.../ark",
+                       "ark.number=78",
+                       "auth.negTypes=2",
+                       "version=Fred,0.7,1.0,1466",
+                       "lastGoodVersion=Fred,0.7,1.0,1466",
+                       "EndMessage"
+               );
+               assertThat(peer.get().get().getIdentity().toString(), is("id1"));
+       }
+
 }