private void connected(final Channel ch, final Handler<ClientConnection> connectHandler) { actualCtx.execute( ch.eventLoop(), new Runnable() { public void run() { createConn(ch, connectHandler); } }); }
@Override public void close() { checkClosed(); pool.close(); for (ClientConnection conn : connectionMap.values()) { conn.close(); } actualCtx.removeCloseHook(closeHook); closed = true; }
private void failed( final Channel ch, final Handler<Throwable> connectionExceptionHandler, final Throwable t) { // If no specific exception handler is provided, fall back to the HttpClient's exception // handler. final Handler<Throwable> exHandler = connectionExceptionHandler == null ? exceptionHandler : connectionExceptionHandler; actualCtx.execute( ch.eventLoop(), new Runnable() { public void run() { pool.connectionClosed(); try { ch.close(); } catch (Exception ignore) { } if (exHandler != null) { exHandler.handle(t); } else { actualCtx.reportException(t); } } }); }
public DefaultHttpClient(VertxInternal vertx) { this.vertx = vertx; actualCtx = vertx.getOrCreateContext(); actualCtx.addCloseHook(closeHook); }
void internalConnect( final Handler<ClientConnection> connectHandler, final Handler<Throwable> connectErrorHandler) { if (bootstrap == null) { // Share the event loop thread to also serve the HttpClient's network traffic. VertxEventLoopGroup pool = new VertxEventLoopGroup(); pool.addWorker(actualCtx.getEventLoop()); bootstrap = new Bootstrap(); bootstrap.group(pool); bootstrap.channel(NioSocketChannel.class); tcpHelper.checkSSL(vertx); bootstrap.handler( new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("exceptionDispatcher", EXCEPTION_DISPATCH_HANDLER); if (tcpHelper.isSSL()) { SSLEngine engine = tcpHelper.getSSLContext().createSSLEngine(host, port); if (tcpHelper.isVerifyHost()) { SSLParameters sslParameters = engine.getSSLParameters(); sslParameters.setEndpointIdentificationAlgorithm("HTTPS"); engine.setSSLParameters(sslParameters); } engine.setUseClientMode(true); // We are on the client side of the connection pipeline.addLast("ssl", new SslHandler(engine)); } pipeline.addLast("codec", new HttpClientCodec()); pipeline.addLast("handler", new ClientHandler()); } }); } tcpHelper.applyConnectionOptions(bootstrap); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); future.addListener( new ChannelFutureListener() { public void operationComplete(ChannelFuture channelFuture) throws Exception { final Channel ch = channelFuture.channel(); if (channelFuture.isSuccess()) { if (tcpHelper.isSSL()) { // TCP connected, so now we must do the SSL handshake SslHandler sslHandler = ch.pipeline().get(SslHandler.class); Future<Channel> fut = sslHandler.handshakeFuture(); fut.addListener( new GenericFutureListener<Future<Channel>>() { @Override public void operationComplete(Future<Channel> future) throws Exception { if (future.isSuccess()) { connected(ch, connectHandler); } else { failed( ch, connectErrorHandler, new SSLHandshakeException("Failed to create SSL connection")); } } }); } else { connected(ch, connectHandler); } } else { failed(ch, connectErrorHandler, channelFuture.cause()); } } }); }