()
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | protected void init() throws IOException { |
| 58 | Task task; |
| 59 | String hostLog = host + ":" + _port; |
| 60 | try { |
| 61 | logger.info("Connecting to {}", hostLog); |
| 62 | _selector = Selector.open(); |
| 63 | clientConnection = SocketChannel.open(); |
| 64 | final InetSocketAddress serverAddress = new InetSocketAddress(host, _port); |
| 65 | clientConnection.connect(serverAddress); |
| 66 | logger.info("Connected to {}", hostLog); |
| 67 | clientConnection.configureBlocking(false); |
| 68 | |
| 69 | final SSLContext sslContext = Link.initClientSSLContext(); |
| 70 | SSLEngine sslEngine = sslContext.createSSLEngine(host, _port); |
| 71 | sslEngine.setUseClientMode(true); |
| 72 | sslEngine.setEnabledProtocols(SSLUtils.getSupportedProtocols(sslEngine.getEnabledProtocols())); |
| 73 | sslEngine.beginHandshake(); |
| 74 | if (!Link.doHandshake(clientConnection, sslEngine, getSslHandshakeTimeout())) { |
| 75 | throw new IOException(String.format("SSL Handshake failed while connecting to host: %s", hostLog)); |
| 76 | } |
| 77 | logger.info("SSL: Handshake done"); |
| 78 | |
| 79 | final Link link = new Link(serverAddress, this); |
| 80 | link.setSSLEngine(sslEngine); |
| 81 | final SelectionKey key = clientConnection.register(_selector, SelectionKey.OP_READ); |
| 82 | link.setKey(key); |
| 83 | key.attach(link); |
| 84 | // Notice we've already connected due to the handshake, so let's get the |
| 85 | // remaining task done |
| 86 | task = _factory.create(Task.Type.CONNECT, link, null); |
| 87 | } catch (final GeneralSecurityException e) { |
| 88 | closeChannel(); |
| 89 | throw new IOException("Failed to initialise security", e); |
| 90 | } catch (final IOException e) { |
| 91 | closeChannel(); |
| 92 | logger.error("IOException while connecting to {}", hostLog, e); |
| 93 | throw e; |
| 94 | } |
| 95 | if (task != null) { |
| 96 | _executor.submit(task); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | protected void registerLink(final InetSocketAddress address, final Link link) { |
nothing calls this directly
no test coverage detected