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