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.github.mrstampy.gameboot.otp.netty.OtpNettyTest.java

License:Open Source License

private void createEncryptedChannel() throws InterruptedException {
    ChannelFuture cf = encClient.connect(HOST, ENC_SERVER_PORT);
    cf.await(1, TimeUnit.SECONDS);

    assertTrue(cf.isSuccess());/*from  w w w . ja va  2s  .com*/
    encChannel = cf.channel();
}

From source file:com.github.mrstampy.kitchensync.netty.Bootstrapper.java

License:Open Source License

/**
 * Returns a channel using the bootstrap for the specified port. Should none
 * exist then the default bootstrap is used.
 *
 * @param <CHANNEL>/*from www . j av a 2  s.c o  m*/
 *          the generic type
 * @param port
 *          the port
 * @return the channel
 */
@SuppressWarnings("unchecked")
public <CHANNEL extends DatagramChannel> CHANNEL bind(int port) {
    boolean contains = containsBootstrap(port);
    if (!contains && !hasDefaultBootstrap()) {
        log.error("Bootstrap for port {} not initialized", port);
        return null;
    }

    Bootstrap b = channelBootstraps.get(contains ? port : DEFAULT_BOOTSTRAP_KEY);

    ChannelFuture cf = b.bind(port);

    CountDownLatch latch = new CountDownLatch(1);
    cf.addListener(getBindListener(port, latch));

    await(latch, "Channel creation timed out");

    return cf.isSuccess() ? (CHANNEL) cf.channel() : null;
}

From source file:com.github.mrstampy.kitchensync.netty.Bootstrapper.java

License:Open Source License

/**
 * Bind to the specified multicast address.
 *
 * @param <CHANNEL>/*from  w ww  .j av a2s .c om*/
 *          the generic type
 * @param multicast
 *          the multicast
 * @return the channel
 */
@SuppressWarnings("unchecked")
public <CHANNEL extends DatagramChannel> CHANNEL multicastBind(InetSocketAddress multicast) {
    String key = createMulticastKey(multicast);
    if (!containsMulticastBootstrap(key)) {
        log.error("Multicast bootstrap for {} not initialized", multicast);
        return null;
    }

    Bootstrap b = multicastBootstraps.get(key);

    ChannelFuture cf = b.bind();

    CountDownLatch latch = new CountDownLatch(1);
    cf.addListener(getMulticastBindListener(multicast, latch));

    await(latch, "Multicast channel creation timed out");

    return cf.isSuccess() ? (CHANNEL) cf.channel() : null;
}

From source file:com.github.mrstampy.kitchensync.netty.Bootstrapper.java

License:Open Source License

private GenericFutureListener<ChannelFuture> getBindListener(final int port, final CountDownLatch latch) {
    return new GenericFutureListener<ChannelFuture>() {

        @Override/*from w w w.  j a  va2s. c  o m*/
        public void operationComplete(ChannelFuture future) throws Exception {
            try {
                if (future.isSuccess()) {
                    log.debug("Channel creation successful for {}", future.channel());
                } else {
                    Throwable cause = future.cause();
                    if (cause == null) {
                        log.error("Could not create channel for {}", port);
                    } else {
                        log.error("Could not create channel for {}", port, cause);
                    }
                }
            } finally {
                latch.countDown();
            }
        }
    };
}

From source file:com.github.nettybook.ch4.EchoClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();

    try {/*  ww w.  j av a2  s  . com*/
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                p.addLast(new EchoClientHandler1());
                p.addLast(new LoggingHandler());
            }
        });

        ChannelFuture f = b.connect("localhost", 8888).sync();

        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.github.nettybook.ch7.junit.TelnetServerV3.java

License:Apache License

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

    try {//  w ww  . java 2s. com
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new TelnetServerInitializerV3());

        ChannelFuture future = b.bind(address).sync();

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

From source file:com.github.sinsinpub.pero.frontend.NettySocksServer.java

License:Apache License

public void run() throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1, ThreadFactoryRepository.BOSS_GORUP);
    EventLoopGroup workerGroup = new NioEventLoopGroup(getMaxWorkerThreads(),
            ThreadFactoryRepository.WORKER_GROUP);
    try {/* w w w  . jav  a2s.co  m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO)).childHandler(getSocksServerInitializer());
        ChannelFuture cf = b.bind(getPort()).sync();
        logger.info(
                String.format("Proxy server %s %s started.", ApplicationVersion.DEFAULT.getApplicationName(),
                        ApplicationVersion.DEFAULT.getApplicationVersion()));
        cf.channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.github.spapageo.jannel.client.JannelClient.java

License:Open Source License

protected Channel createConnectedChannel(String host, int port, long connectTimeoutMillis) {
    this.clientBootstrap.option(CONNECT_TIMEOUT_OPTION, connectTimeoutMillis);

    ChannelFuture connectFuture = this.clientBootstrap.connect(host, port).syncUninterruptibly();

    LOGGER.info("Successfully connected to bearer-box at: {}", connectFuture.channel().remoteAddress());
    return connectFuture.channel();
}

From source file:com.github.vitrifiedcode.javautilities.netty.DiscardServer.java

License:Open Source License

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*  w ww . j  a  va2s  .c o m*/
        ServerBootstrap b = new ServerBootstrap(); // (2)
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // (3)
                .childHandler(new ChannelInitializer<SocketChannel>() { // (4)
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new DiscardServerHandler());
                    }
                }).option(ChannelOption.SO_BACKLOG, 128) // (5)
                .childOption(ChannelOption.SO_KEEPALIVE, true); // (6)

        // Bind and start to accept incoming connections.
        ChannelFuture f = b.bind(port).sync(); // (7)

        // Wait until the server socket is closed.
        // In this example, this does not happen, but you can do that to gracefully
        // shut down your server.
        f.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

From source file:com.github.wangshuwei5.client.NettyClient.java

License:Apache License

public void connect(int port, String host) throws Exception {

    // ?NIO/*  w ww  . jav  a  2  s .  c o  m*/

    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new NettyClientInitializer(SSLMODE.CSA.toString()));
        // ??
        ChannelFuture future = b.connect(new InetSocketAddress(host, port),
                new InetSocketAddress(NettyConstant.LOCALIP, NettyConstant.LOCAL_PORT)).sync();
        future.channel().closeFuture().sync();
    } finally {
        // ????????
        executor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(1);
                    try {
                        connect(NettyConstant.PORT, NettyConstant.REMOTEIP);// ???
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}