Add command to retrieve the node’s config
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / DefaultFcpClient.java
1 package net.pterodactylus.fcp.quelaton;
2
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;
8
9 import net.pterodactylus.fcp.FcpConnection;
10
11 import com.google.common.util.concurrent.ListeningExecutorService;
12 import com.google.common.util.concurrent.MoreExecutors;
13
14 /**
15  * Default {@link FcpClient} implementation.
16  *
17  * @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
18  */
19 public class DefaultFcpClient implements FcpClient {
20
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;
26
27         public DefaultFcpClient(ExecutorService threadPool, String hostname, int port, Supplier<String> clientName) {
28                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
29                 this.hostname = hostname;
30                 this.port = port;
31                 this.clientName = clientName;
32         }
33
34         private FcpConnection connect() throws IOException {
35                 FcpConnection fcpConnection = this.fcpConnection.get();
36                 if ((fcpConnection != null) && !fcpConnection.isClosed()) {
37                         return fcpConnection;
38                 }
39                 fcpConnection = createConnection();
40                 this.fcpConnection.set(fcpConnection);
41                 return fcpConnection;
42         }
43
44         private FcpConnection createConnection() throws IOException {
45                 try {
46                         return new ClientHelloImpl(threadPool, hostname, port).withName(clientName.get()).execute().get();
47                 } catch (InterruptedException | ExecutionException e) {
48                         throw new IOException(e);
49                 }
50         }
51
52         @Override
53         public GetNodeCommand getNode() {
54                 return new GetNodeCommandImpl(threadPool, this::connect);
55         }
56
57         @Override
58         public GetConfigCommand getConfig() {
59                 return new GetConfigCommandImpl(threadPool, this::connect);
60         }
61
62         @Override
63         public GenerateKeypairCommand generateKeypair() {
64                 return new GenerateKeypairCommandImpl(threadPool, this::connect);
65         }
66
67         @Override
68         public ClientGetCommand clientGet() {
69                 return new ClientGetCommandImpl(threadPool, this::connect);
70         }
71
72         @Override
73         public ClientPutCommand clientPut() {
74                 return new ClientPutCommandImpl(threadPool, this::connect);
75         }
76
77         @Override
78         public ListPeerCommand listPeer() {
79                 return new ListPeerCommandImpl(threadPool, this::connect);
80         }
81
82         @Override
83         public ListPeersCommand listPeers() {
84                 return new ListPeersCommandImpl(threadPool, this::connect);
85         }
86
87         @Override
88         public AddPeerCommand addPeer() {
89                 return new AddPeerCommandImpl(threadPool, this::connect);
90         }
91
92         @Override
93         public ModifyPeerCommand modifyPeer() {
94                 return new ModifyPeerCommandImpl(threadPool, this::connect);
95         }
96
97         @Override
98         public RemovePeerCommand removePeer() {
99                 return new RemovePeerCommandImpl(threadPool, this::connect);
100         }
101
102         @Override
103         public ListPeerNotesCommand listPeerNotes() {
104                 return new ListPeerNotesCommandImpl(threadPool, this::connect);
105         }
106
107         @Override
108         public ModifyPeerNoteCommand modifyPeerNote() {
109                 return new ModifyPeerNoteCommandImpl(threadPool, this::connect);
110         }
111
112 }
113