--- /dev/null
+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);
+ }
+
+ }
+
+}
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"));
+ }
+
}