Beispiel #1
0
 private void connected(final Channel ch, final Handler<ClientConnection> connectHandler) {
   actualCtx.execute(
       ch.eventLoop(),
       new Runnable() {
         public void run() {
           createConn(ch, connectHandler);
         }
       });
 }
Beispiel #2
0
 @Override
 public void close() {
   checkClosed();
   pool.close();
   for (ClientConnection conn : connectionMap.values()) {
     conn.close();
   }
   actualCtx.removeCloseHook(closeHook);
   closed = true;
 }
Beispiel #3
0
 public void addHandler(Handler<T> handler, DefaultContext context) {
   EventLoop worker = context.getEventLoop();
   availableWorkers.addWorker(worker);
   Handlers<T> handlers = new Handlers<T>();
   Handlers<T> prev = handlerMap.putIfAbsent(worker, handlers);
   if (prev != null) {
     handlers = prev;
   }
   handlers.addHandler(new HandlerHolder<>(context, handler));
   handlerCount++;
 }
Beispiel #4
0
 @Override
 protected void channelRead(
     final C connection,
     final DefaultContext context,
     final ChannelHandlerContext chctx,
     final Object msg)
     throws Exception {
   if (connection != null) {
     // we are reading from the channel
     Channel ch = chctx.channel();
     // We need to do this since it's possible the server is being used from a worker context
     if (context.isOnCorrectWorker(ch.eventLoop())) {
       try {
         vertx.setContext(context);
         doMessageReceived(connection, chctx, msg);
       } catch (Throwable t) {
         context.reportException(t);
       }
     } else {
       context.execute(
           new Runnable() {
             public void run() {
               try {
                 doMessageReceived(connection, chctx, msg);
               } catch (Throwable t) {
                 context.reportException(t);
               }
             }
           });
     }
   } else {
     try {
       doMessageReceived(connection, chctx, msg);
     } catch (Throwable t) {
       chctx.pipeline().fireExceptionCaught(t);
     }
   }
 }
Beispiel #5
0
 public void removeHandler(Handler<T> handler, DefaultContext context) {
   EventLoop worker = context.getEventLoop();
   Handlers<T> handlers = handlerMap.get(worker);
   if (!handlers.removeHandler(new HandlerHolder<>(context, handler))) {
     throw new IllegalStateException("Can't find handler");
   }
   if (handlers.isEmpty()) {
     handlerMap.remove(worker);
   }
   handlerCount--;
   // Available workers does it's own reference counting -since workers can be shared across
   // different Handlers
   availableWorkers.removeWorker(worker);
 }
Beispiel #6
0
  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);
            }
          }
        });
  }
Beispiel #7
0
 public DefaultHttpClient(VertxInternal vertx) {
   this.vertx = vertx;
   actualCtx = vertx.getOrCreateContext();
   actualCtx.addCloseHook(closeHook);
 }
Beispiel #8
0
  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());
            }
          }
        });
  }