Close FCP reply sequences after use
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / ClientHelloImpl.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.AtomicBoolean;
7 import java.util.concurrent.atomic.AtomicReference;
8
9 import net.pterodactylus.fcp.ClientHello;
10 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
11 import net.pterodactylus.fcp.FcpConnection;
12 import net.pterodactylus.fcp.NodeHello;
13
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.ListeningExecutorService;
16 import com.google.common.util.concurrent.MoreExecutors;
17
18 /**
19  * Internal <code>ClientHello</code> implementation based on {@link FcpReplySequence}.
20  *
21  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
22  */
23 public class ClientHelloImpl {
24
25         private final ListeningExecutorService threadPool;
26         private final String hostname;
27         private final int port;
28         private final AtomicReference<String> clientName = new AtomicReference<>();
29
30         public ClientHelloImpl(ExecutorService threadPool, String hostname, int port) {
31                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
32                 this.hostname = hostname;
33                 this.port = port;
34         }
35
36         public Executable<FcpConnection> withName(String name) {
37                 clientName.set(name);
38                 return this::execute;
39         }
40
41         private ListenableFuture<FcpConnection> execute() {
42                 return threadPool.submit(this::establishConnection);
43         }
44
45         private FcpConnection establishConnection() throws IOException {
46                 FcpConnection connection = new FcpConnection(hostname, port);
47                 connection.connect();
48                 ClientHello clientHello = new ClientHello(clientName.get(), "2.0");
49                 try (ClientHelloReplySequence nodeHelloSequence = new ClientHelloReplySequence(connection)) {
50                         if (nodeHelloSequence.send(clientHello).get()) {
51                                 return connection;
52                         }
53                 } catch (InterruptedException | ExecutionException e) {
54                         connection.close();
55                         throw new IOException(String.format("Could not connect to %s:%d.", hostname, port), e);
56                 }
57                 connection.close();
58                 throw new IOException(String.format("Could not connect to %s:%d.", hostname, port));
59         }
60
61         private class ClientHelloReplySequence extends FcpReplySequence<Boolean> {
62
63                 private final AtomicReference<NodeHello> receivedNodeHello = new AtomicReference<>();
64
65                 public ClientHelloReplySequence(FcpConnection connection) {
66                         super(ClientHelloImpl.this.threadPool, connection);
67                 }
68
69                 @Override
70                 protected boolean isFinished() {
71                         return receivedNodeHello.get() != null;
72                 }
73
74                 @Override
75                 protected Boolean getResult() {
76                         return receivedNodeHello.get() != null;
77                 }
78
79                 @Override
80                 protected void consumeNodeHello(NodeHello nodeHello) {
81                         receivedNodeHello.set(nodeHello);
82                 }
83
84         }
85
86 }