X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Ffcp%2Fquelaton%2FDefaultFcpClient.java;h=3dd0b7692ef25d5c7ba6ea9b1d7dbc6397d899ec;hb=e591c6488b692e3dfcf2efd1905d399f39c6067f;hp=9cb098155e33c0c68bf4f1f619b3775d0ef9a8e6;hpb=aa659bcdaa77efb1e902a279a3505964d26e09ff;p=jFCPlib.git diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java index 9cb0981..3dd0b76 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java @@ -1,20 +1,17 @@ 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.Future; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; -import net.pterodactylus.fcp.ClientHello; -import net.pterodactylus.fcp.CloseConnectionDuplicateClientName; import net.pterodactylus.fcp.FcpConnection; -import net.pterodactylus.fcp.FcpKeyPair; -import net.pterodactylus.fcp.GenerateSSK; -import net.pterodactylus.fcp.NodeHello; -import net.pterodactylus.fcp.SSKKeypair; +import net.pterodactylus.fcp.Peer; + +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.common.util.concurrent.MoreExecutors; /** * Default {@link FcpClient} implementation. @@ -23,90 +20,81 @@ import net.pterodactylus.fcp.SSKKeypair; */ public class DefaultFcpClient implements FcpClient { - private final ExecutorService threadPool; + private final ListeningExecutorService threadPool; private final String hostname; private final int port; private final AtomicReference fcpConnection = new AtomicReference<>(); private final Supplier clientName; - private final Supplier expectedVersion; - public DefaultFcpClient(ExecutorService threadPool, String hostname, int port, Supplier clientName, - Supplier expectedVersion) { - this.threadPool = threadPool; + public DefaultFcpClient(ExecutorService threadPool, String hostname, int port, Supplier clientName) { + this.threadPool = MoreExecutors.listeningDecorator(threadPool); this.hostname = hostname; this.port = port; this.clientName = clientName; - this.expectedVersion = expectedVersion; } - private void connect() throws IOException { - if (fcpConnection.get() != null) { - return; + private FcpConnection connect() throws IOException { + FcpConnection fcpConnection = this.fcpConnection.get(); + if ((fcpConnection != null) && !fcpConnection.isClosed()) { + return fcpConnection; } - fcpConnection.compareAndSet(null, createConnection()); + fcpConnection = createConnection(); + this.fcpConnection.set(fcpConnection); + return fcpConnection; } private FcpConnection createConnection() throws IOException { - FcpConnection connection = new FcpConnection(hostname, port); - connection.connect(); - AtomicReference receivedNodeHello = new AtomicReference<>(); - AtomicBoolean receivedClosed = new AtomicBoolean(); - FcpReplySequence nodeHelloSequence = new FcpReplySequence(threadPool, connection); - nodeHelloSequence - .handle(NodeHello.class) - .with((nodeHello) -> receivedNodeHello.set(nodeHello)); - nodeHelloSequence - .handle(CloseConnectionDuplicateClientName.class) - .with((closeConnection) -> receivedClosed.set(true)); - nodeHelloSequence.waitFor(() -> receivedNodeHello.get() != null || receivedClosed.get()); - ClientHello clientHello = new ClientHello(clientName.get(), expectedVersion.get()); try { - nodeHelloSequence.send(clientHello).get(); + return new ClientHelloImpl(threadPool, hostname, port).withName(clientName.get()).execute().get(); } catch (InterruptedException | ExecutionException e) { - connection.close(); - throw new IOException(String.format("Could not connect to %s:%d.", hostname, port), e); + throw new IOException(e); } - return connection; } @Override - public GenerateKeypairCommand generateKeypair() { - return new GenerateKeypairCommandImpl(); + public GetNodeCommand getNode() { + return new GetNodeCommandImpl(threadPool, this::connect); } - private class GenerateKeypairCommandImpl implements GenerateKeypairCommand { - - @Override - public Future execute() { - return threadPool.submit(() -> { - connect(); - Sequence sequence = new Sequence(); - FcpReplySequence replySequence = new FcpReplySequence(threadPool, fcpConnection.get()); - replySequence.handle(SSKKeypair.class).with(sequence::handleSSKKeypair); - replySequence.waitFor(sequence::isFinished); - replySequence.send(new GenerateSSK()).get(); - return sequence.getKeyPair(); - }); - } + @Override + public GenerateKeypairCommand generateKeypair() { + return new GenerateKeypairCommandImpl(threadPool, this::connect); + } - private class Sequence { + @Override + public ClientGetCommand clientGet() { + return new ClientGetCommandImpl(threadPool, this::connect); + } - private AtomicReference keyPair = new AtomicReference<>(); + @Override + public ClientPutCommand clientPut() { + return new ClientPutCommandImpl(threadPool, this::connect); + } - public void handleSSKKeypair(SSKKeypair sskKeypair) { - keyPair.set(new FcpKeyPair(sskKeypair.getRequestURI(), sskKeypair.getInsertURI())); - } + @Override + public ListPeerCommand listPeer() { + return new ListPeerCommandImpl(threadPool, this::connect); + } - public boolean isFinished() { - return keyPair.get() != null; - } + @Override + public ListPeersCommand listPeers() { + return new ListPeersCommandImpl(threadPool, this::connect); + } - public FcpKeyPair getKeyPair() { - return keyPair.get(); - } + @Override + public AddPeerCommand addPeer() { + return new AddPeerCommandImpl(threadPool, this::connect); + } - } + @Override + public ModifyPeerCommand modifyPeer() { + return new ModifyPeerCommandImpl(threadPool, this::connect); + } + @Override + public ListPeerNotesCommand listPeerNotes() { + return new ListPeerNotesCommandImpl(threadPool, this::connect); } } +