1 package net.pterodactylus.fcp.quelaton;
3 import java.io.IOException;
4 import java.util.concurrent.ExecutionException;
5 import java.util.concurrent.ExecutorService;
6 import java.util.concurrent.atomic.AtomicReference;
7 import java.util.function.Supplier;
9 import net.pterodactylus.fcp.FcpConnection;
11 import com.google.common.util.concurrent.ListeningExecutorService;
12 import com.google.common.util.concurrent.MoreExecutors;
15 * Default {@link FcpClient} implementation.
17 * @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
19 public class DefaultFcpClient implements FcpClient {
21 private final ListeningExecutorService threadPool;
22 private final String hostname;
23 private final int port;
24 private final AtomicReference<FcpConnection> fcpConnection = new AtomicReference<>();
25 private final Supplier<String> clientName;
27 public DefaultFcpClient(ExecutorService threadPool, String hostname, int port, Supplier<String> clientName) {
28 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
29 this.hostname = hostname;
31 this.clientName = clientName;
34 private FcpConnection connect() throws IOException {
35 FcpConnection fcpConnection = this.fcpConnection.get();
36 if ((fcpConnection != null) && !fcpConnection.isClosed()) {
39 fcpConnection = createConnection();
40 this.fcpConnection.set(fcpConnection);
44 private FcpConnection createConnection() throws IOException {
46 return new ClientHelloImpl(threadPool, hostname, port).withName(clientName.get()).execute().get();
47 } catch (InterruptedException | ExecutionException e) {
48 throw new IOException(e);
53 public GetNodeCommand getNode() {
54 return new GetNodeCommandImpl(threadPool, this::connect);
58 public GetConfigCommand getConfig() {
59 return new GetConfigCommandImpl(threadPool, this::connect);
63 public ModifyConfigCommand modifyConfig() {
64 return new ModifyConfigCommandImpl(threadPool, this::connect);
68 public GenerateKeypairCommand generateKeypair() {
69 return new GenerateKeypairCommandImpl(threadPool, this::connect);
73 public ClientGetCommand clientGet() {
74 return new ClientGetCommandImpl(threadPool, this::connect);
78 public ClientPutCommand clientPut() {
79 return new ClientPutCommandImpl(threadPool, this::connect);
83 public ListPeerCommand listPeer() {
84 return new ListPeerCommandImpl(threadPool, this::connect);
88 public ListPeersCommand listPeers() {
89 return new ListPeersCommandImpl(threadPool, this::connect);
93 public AddPeerCommand addPeer() {
94 return new AddPeerCommandImpl(threadPool, this::connect);
98 public ModifyPeerCommand modifyPeer() {
99 return new ModifyPeerCommandImpl(threadPool, this::connect);
103 public RemovePeerCommand removePeer() {
104 return new RemovePeerCommandImpl(threadPool, this::connect);
108 public ListPeerNotesCommand listPeerNotes() {
109 return new ListPeerNotesCommandImpl(threadPool, this::connect);
113 public ModifyPeerNoteCommand modifyPeerNote() {
114 return new ModifyPeerNoteCommandImpl(threadPool, this::connect);
118 public LoadPluginCommand loadPlugin() {
119 return new LoadPluginCommandImpl(threadPool, this::connect);
123 public ReloadPluginCommand reloadPlugin() {
124 return new ReloadPluginCommandImpl(threadPool, this::connect);
128 public RemovePluginCommand removePlugin() {
129 return new RemovePluginCommandImpl(threadPool, this::connect);