setField("URL", String.valueOf(url));
}
+ public AddPeer(String identifier, URL url) {
+ this(url);
+ setField("Identifier", identifier);
+ }
+
/**
* Creates a new “AddPeer” request that adds the peer given by the noderef.
*
package net.pterodactylus.fcp.quelaton;
+import java.net.URL;
import java.util.Optional;
import net.pterodactylus.fcp.Peer;
*/
public interface AddPeerCommand extends WithFile<Executable<Optional<Peer>>> {
+ Executable<Optional<Peer>> fromURL(URL url);
+
}
import java.io.File;
import java.io.IOException;
+import java.net.URL;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
private final ListeningExecutorService threadPool;
private final ConnectionSupplier connectionSupplier;
private final AtomicReference<File> file = new AtomicReference<>();
+ private final AtomicReference<URL> url = new AtomicReference<>();
public AddPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
this.threadPool = MoreExecutors.listeningDecorator(threadPool);
return this::execute;
}
+ @Override
+ public Executable<Optional<Peer>> fromURL(URL url) {
+ this.url.set(url);
+ return this::execute;
+ }
+
private ListenableFuture<Optional<Peer>> execute() {
return threadPool.submit(this::executeSequence);
}
AddPeer addPeer = null;
if (file.get() != null) {
addPeer = new AddPeer(new RandomIdentifierGenerator().generate(), file.get().getPath());
+ } else if (url.get() != null) {
+ addPeer = new AddPeer(new RandomIdentifierGenerator().generate(), url.get());
}
try (AddPeerSequence addPeerSequence = new AddPeerSequence()) {
return addPeerSequence.send(addPeer).get();
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
+import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
assertThat(peer.get().get().getIdentity().toString(), is("id1"));
}
+ @Test
+ public void defaultFcpClientCanAddPeerFromURL() throws InterruptedException, ExecutionException, IOException {
+ Future<Optional<Peer>> peer = fcpClient.addPeer().fromURL(new URL("http://node.ref/")).execute();
+ connectNode();
+ List<String> lines = fcpServer.collectUntil(is("EndMessage"));
+ String identifier = extractIdentifier(lines);
+ assertThat(lines, matchesFcpMessage(
+ "AddPeer",
+ "Identifier=" + identifier,
+ "URL=http://node.ref/",
+ "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"));
+ }
+
}