Example usage for io.netty.channel ChannelFuture channel

List of usage examples for io.netty.channel ChannelFuture channel

Introduction

In this page you can find the example usage for io.netty.channel ChannelFuture channel.

Prototype

Channel channel();

Source Link

Document

Returns a channel where the I/O operation associated with this future takes place.

Usage

From source file:com.xx_dev.apn.socks.local.PortForwardProxyFrontendHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    final Channel inboundChannel = ctx.channel();

    // Start the connection attempt.
    Bootstrap b = new Bootstrap();
    b.group(inboundChannel.eventLoop()).channel(ctx.channel().getClass())
            .handler(new PortForwardProxyBackendInitializer(inboundChannel))
            .option(ChannelOption.AUTO_READ, false);
    ChannelFuture f = b.connect(remoteHost, remotePort);
    outboundChannel = f.channel();
    f.addListener(new ChannelFutureListener() {
        @Override/*from  ww w.ja va  2  s  .co  m*/
        public void operationComplete(ChannelFuture future) {
            if (future.isSuccess()) {
                // connection complete start to read first data
                logger.info("C: " + remoteHost + ":" + remotePort + ", T");
                inboundChannel.read();
            } else {
                logger.info("C: " + remoteHost + ":" + remotePort + ", F");
                inboundChannel.close();
            }
        }
    });
}

From source file:com.xx_dev.port_forwared.HexDumpProxyFrontendHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    final Channel inboundChannel = ctx.channel();

    // Start the connection attempt.
    Bootstrap b = new Bootstrap();
    b.group(inboundChannel.eventLoop()).channel(ctx.channel().getClass())
            .handler(new HexDumpProxyBackendInitializer(inboundChannel, remoteSsl))
            .option(ChannelOption.AUTO_READ, false);
    ChannelFuture f = b.connect(remoteHost, remotePort);
    outboundChannel = f.channel();
    f.addListener(new ChannelFutureListener() {
        @Override/* ww  w  .j a v  a  2  s  .co m*/
        public void operationComplete(ChannelFuture future) {
            if (future.isSuccess()) {
                // connection complete start to read first data
                inboundChannel.read();
            } else {
                // Close the connection if the connection attempt has failed.
                inboundChannel.close();
            }
        }
    });
}

From source file:com.yahoo.pulsar.client.impl.ConnectionPool.java

License:Apache License

private CompletableFuture<ClientCnx> createConnection(InetSocketAddress address, int connectionKey) {
    if (log.isDebugEnabled()) {
        log.debug("Connection for {} not found in cache", address);
    }//from  ww w .j  a va  2s .  c  om

    final CompletableFuture<ClientCnx> cnxFuture = new CompletableFuture<ClientCnx>();

    // Trigger async connect to broker
    bootstrap.connect(address).addListener((ChannelFuture future) -> {
        if (!future.isSuccess()) {
            cnxFuture.completeExceptionally(new PulsarClientException(future.cause()));
            cleanupConnection(address, connectionKey, cnxFuture);
            return;
        }

        log.info("[{}] Connected to server", future.channel());

        future.channel().closeFuture().addListener(v -> {
            // Remove connection from pool when it gets closed
            cleanupConnection(address, connectionKey, cnxFuture);
        });

        // We are connected to broker, but need to wait until the connect/connected handshake is
        // complete
        final ClientCnx cnx = (ClientCnx) future.channel().pipeline().get("handler");
        cnx.connectionFuture().thenRun(() -> {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Connection handshake completed", cnx.channel());
            }
            cnxFuture.complete(cnx);
        }).exceptionally(exception -> {
            log.warn("[{}] Connection handshake failed: {}", cnx.channel(), exception.getMessage());
            cnxFuture.completeExceptionally(exception);
            cleanupConnection(address, connectionKey, cnxFuture);
            cnx.ctx().close();
            return null;
        });

    });

    return cnxFuture;
}

From source file:com.yeetor.androidcontrol.server.LocalServer.java

License:Open Source License

public void start() throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childOption(ChannelOption.SO_KEEPALIVE, true)
            .childHandler(new ChildChannel(new LocalServerWebsocketEventImp()));
    System.out.println("LocalServer will start at port: " + port);
    System.out.println("--------\r\n");
    ChannelFuture future = bootstrap.bind(port).sync();
    future.channel().closeFuture().sync();
}

From source file:com.yeetor.androidcontrol.server.RemoteServer.java

License:Open Source License

public void start() throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childOption(ChannelOption.SO_KEEPALIVE, true)
            .childHandler(new ChildChannel(new RemoteServerWebsocketEventImp()));
    System.out.println("RemoteServer will start at port: " + port);
    System.out.println("--------\r\n");
    ChannelFuture future = bootstrap.bind(port).sync();
    future.channel().closeFuture().sync();
}

From source file:com.yeetor.console.Console.java

License:Open Source License

/**
 * Console??//from   w w  w.  j a  va2  s .c  o  m
 * @param port
 */
public void listenOnTCP(int port) {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new Adapter());
                    }
                }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);

        ChannelFuture f = b.bind(port);

        f.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

From source file:com.yeetor.server.AndroidControlServer.java

License:Open Source License

public void listen(int port) throws InterruptedException {
    try {/*from ww  w .j  a  v a2s. co  m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast("tcp", new TCPHandler());
                        ch.pipeline().addLast("http-codec", new HttpServerCodec());
                        ch.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
                        ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
                        ch.pipeline().addLast("websocket", new WSHandler(new WSServer()));
                        ch.pipeline().addLast("http", new HTTPHandler(new HttpServer()));
                    }
                });
        ChannelFuture f = b.bind(port).sync();
        f.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

From source file:com.zaradai.distributor.messaging.netty.NettyServer.java

License:Apache License

private void bindComplete(ChannelFuture channelFuture) {
    if (channelFuture.isSuccess()) {
        LOGGER.info("Listening on {}", channelFuture.channel().localAddress());
        serverChannelGroup.add(channelFuture.channel());
    } else {/*from  ww  w .j  a va  2 s . c om*/
        LOGGER.warn("Unable to listen", channelFuture.cause());
    }
}

From source file:com.zextras.modules.chat.server.LocalXmppConnectionProviderImpl.java

License:Open Source License

@Override
public Channel openConnection(String host, int port, final ChannelHandler channelHandler) throws IOException {
    ChannelHandler handler = new ChannelInitializer<SocketChannel>() {
        @Override//ww  w  .j a  va 2  s. c o m
        protected void initChannel(SocketChannel socketChannel) throws Exception {
            SSLEngine sslEngine = mZimbraSSLContextProvider.get().createSSLEngine();
            sslEngine.setUseClientMode(true);
            SslHandler sslHandler = new SslHandler(sslEngine);
            socketChannel.pipeline().addFirst("ssl", sslHandler);
            socketChannel.pipeline().addLast("handler", channelHandler);
        }
    };
    ChannelFuture channelFuture = new Bootstrap().channel(NioSocketChannel.class).group(new NioEventLoopGroup())
            .handler(handler).connect(host, port);

    try {
        channelFuture.sync();
        if (!channelFuture.isSuccess()) {
            throw channelFuture.cause();
        }

        return channelFuture.channel();
    } catch (Throwable t) {
        throw new IOException(t);
    }
}