Skip to content

Commit d1d8009

Browse files
committed
Fix a stupid bug i introduced
1 parent 4e07dd1 commit d1d8009

1 file changed

Lines changed: 25 additions & 10 deletions

File tree

utils/src/com/cloud/utils/nio/NioClient.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class NioClient extends NioConnection {
3636

3737
protected String _host;
3838
protected String _bindAddress;
39+
protected SocketChannel _clientConnection;
3940

4041
public NioClient(String name, String host, int port, int workers, HandlerFactory factory) {
4142
super(name, port, workers, factory);
@@ -51,47 +52,50 @@ protected void init() throws IOException {
5152
_selector = Selector.open();
5253
Task task = null;
5354

54-
try (SocketChannel sch = SocketChannel.open()) {
55-
sch.configureBlocking(true);
55+
try {
56+
_clientConnection = SocketChannel.open();
57+
_clientConnection.configureBlocking(true);
5658
s_logger.info("Connecting to " + _host + ":" + _port);
5759

5860
if (_bindAddress != null) {
5961
s_logger.info("Binding outbound interface at " + _bindAddress);
6062

6163
InetSocketAddress bindAddr = new InetSocketAddress(_bindAddress, 0);
62-
sch.socket().bind(bindAddr);
64+
_clientConnection.socket().bind(bindAddr);
6365
}
6466

6567
InetSocketAddress peerAddr = new InetSocketAddress(_host, _port);
66-
sch.connect(peerAddr);
68+
_clientConnection.connect(peerAddr);
6769

6870
SSLEngine sslEngine = null;
6971
// Begin SSL handshake in BLOCKING mode
70-
sch.configureBlocking(true);
72+
_clientConnection.configureBlocking(true);
7173

7274
SSLContext sslContext = Link.initSSLContext(true);
7375
sslEngine = sslContext.createSSLEngine(_host, _port);
7476
sslEngine.setUseClientMode(true);
7577

76-
Link.doHandshake(sch, sslEngine, true);
78+
Link.doHandshake(_clientConnection, sslEngine, true);
7779
s_logger.info("SSL: Handshake done");
7880
s_logger.info("Connected to " + _host + ":" + _port);
7981

80-
81-
sch.configureBlocking(false);
82+
_clientConnection.configureBlocking(false);
8283
Link link = new Link(peerAddr, this);
8384
link.setSSLEngine(sslEngine);
84-
SelectionKey key = sch.register(_selector, SelectionKey.OP_READ);
85+
SelectionKey key = _clientConnection.register(_selector, SelectionKey.OP_READ);
8586
link.setKey(key);
8687
key.attach(link);
8788
// Notice we've already connected due to the handshake, so let's get the
8889
// remaining task done
8990
task = _factory.create(Task.Type.CONNECT, link, null);
9091
} catch (GeneralSecurityException e) {
92+
_selector.close();
9193
throw new IOException("Failed to initialise security", e);
92-
} finally {
94+
} catch (IOException e) {
9395
_selector.close();
96+
throw e;
9497
}
98+
9599
_executor.execute(task);
96100
}
97101

@@ -104,4 +108,15 @@ protected void registerLink(InetSocketAddress saddr, Link link) {
104108
protected void unregisterLink(InetSocketAddress saddr) {
105109
// don't do anything.
106110
}
111+
112+
@Override
113+
public void cleanUp() throws IOException {
114+
super.cleanUp();
115+
if (_clientConnection != null) {
116+
_clientConnection.close();
117+
}
118+
s_logger.info("NioClient connection closed");
119+
120+
}
121+
107122
}

0 commit comments

Comments
 (0)