1 package net.pterodactylus.fcp.quelaton;
3 import java.io.IOException;
4 import java.util.Optional;
5 import java.util.concurrent.ExecutionException;
6 import java.util.concurrent.ExecutorService;
7 import java.util.concurrent.atomic.AtomicReference;
8 import java.util.function.Supplier;
10 import net.pterodactylus.fcp.FcpConnection;
11 import net.pterodactylus.fcp.Peer;
13 import com.google.common.util.concurrent.ListeningExecutorService;
14 import com.google.common.util.concurrent.MoreExecutors;
17 * Default {@link FcpClient} implementation.
19 * @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
21 public class DefaultFcpClient implements FcpClient {
23 private final ListeningExecutorService threadPool;
24 private final String hostname;
25 private final int port;
26 private final AtomicReference<FcpConnection> fcpConnection = new AtomicReference<>();
27 private final Supplier<String> clientName;
29 public DefaultFcpClient(ExecutorService threadPool, String hostname, int port, Supplier<String> clientName) {
30 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
31 this.hostname = hostname;
33 this.clientName = clientName;
36 private FcpConnection connect() throws IOException {
37 FcpConnection fcpConnection = this.fcpConnection.get();
38 if ((fcpConnection != null) && !fcpConnection.isClosed()) {
41 fcpConnection = createConnection();
42 this.fcpConnection.set(fcpConnection);
46 private FcpConnection createConnection() throws IOException {
48 return new ClientHelloImpl(threadPool, hostname, port).withName(clientName.get()).execute().get();
49 } catch (InterruptedException | ExecutionException e) {
50 throw new IOException(e);
55 public GetNodeCommand getNode() {
56 return new GetNodeCommandImpl(threadPool, this::connect);
60 public GenerateKeypairCommand generateKeypair() {
61 return new GenerateKeypairCommandImpl(threadPool, this::connect);
65 public ClientGetCommand clientGet() {
66 return new ClientGetCommandImpl(threadPool, this::connect);
70 public ClientPutCommand clientPut() {
71 return new ClientPutCommandImpl(threadPool, this::connect);
75 public ListPeerCommand listPeer() {
76 return new ListPeerCommandImpl(threadPool, this::connect);
80 public ListPeersCommand listPeers() {
81 return new ListPeersCommandImpl(threadPool, this::connect);
85 public AddPeerCommand addPeer() {
86 return new AddPeerCommandImpl(threadPool, this::connect);
90 public ModifyPeerCommand modifyPeer() {
91 return new ModifyPeerCommandImpl(threadPool, this::connect);
95 public ListPeerNotesCommand listPeerNotes() {
96 return new ListPeerNotesCommandImpl(threadPool, this::connect);