Example usage for io.netty.channel ChannelFuture sync

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

Introduction

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

Prototype

@Override
    ChannelFuture sync() throws InterruptedException;

Source Link

Usage

From source file:com.witjit.game.client.Client.java

License:Apache License

public static void main(String[] args) throws Exception {

    EventLoopGroup group = new NioEventLoopGroup();
    try {//from  w  w w. j av  a2  s  .  c  o  m
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ClientInitializer());

        // Start the connection attempt.
        Channel ch = b.connect(HOST, PORT).sync().channel();

        // Read commands from the stdin.
        ChannelFuture lastWriteFuture = null;
        //            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        //            for (;;) {
        //                String line = in.readLine();
        //                if (line == null) {
        //                    break;
        //                }
        //
        //                // Sends the received line to the server.
        //                lastWriteFuture = ch.writeAndFlush(line + "\r\n");
        //
        //                // If user typed the 'bye' command, wait until the server closes
        //                // the connection.
        //                if ("bye".equals(line.toLowerCase())) {
        //                    ch.closeFuture().sync();
        //                    break;
        //                }
        //            }

        ByteBuf outb = Unpooled.buffer(200);
        outb.writeShort(127);
        outb.writeShort(10);
        outb.writeInt(10001);
        outb.writeByte(40);
        outb.writeByte(50);
        // Sends the received line to the server.
        lastWriteFuture = ch.writeAndFlush(outb);
        // Wait until all messages are flushed before closing the channel.
        if (lastWriteFuture != null) {
            lastWriteFuture.sync();
        }

        // Wait until the connection is closed.
        ch.closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.witjit.game.client.EchoClient.java

License:Apache License

private static void sendData(Channel ch) throws InterruptedException {
    ByteBuf outb = Unpooled.buffer(200);
    outb.writeShort(127);/*ww  w .j ava  2 s  .  co m*/
    outb.writeShort(10);
    outb.writeInt(10001);
    outb.writeByte(40);
    outb.writeByte(50);
    // Sends the received line to the server.
    ChannelFuture writeFuture = ch.writeAndFlush(outb);
    // Wait until all messages are flushed before closing the channel.
    if (writeFuture != null) {
        writeFuture.sync();
    }

}

From source file:com.xsecret.chat.client.ChatClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE)
            .build();//from   www. ja  va  2s.  co  m

    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChatClientInitializer(sslCtx));

        // Start the connection attempt.
        Channel ch = b.connect(HOST, PORT).sync().channel();

        // Read commands from the stdin.
        ChannelFuture lastWriteFuture = null;

        DataManager dataManager = new DataManager("shengxinlei", "pangff");
        dataManager.start();
        for (;;) {
            System.out.println("================");
            MsgProtocol.MsgContent baseMsg = msgLinkedBlockingQueue.take();
            if (baseMsg == null) {
                break;
            }
            if (MsgProtocol.MsgType.LOGIN.equals(baseMsg.getMsgType())) {
                System.out.println("=======login=========");
                // If user typed the 'bye' command, wait until the server closes
                // the connection.
            } else if (MsgProtocol.MsgType.LOGOUT.equals(baseMsg.getMsgType())) {
                System.out.println("=======logout=========");
                ch.closeFuture().sync();
                dataManager.stopThread();
                break;
            } else {
                // Sends the received line to the server.
                lastWriteFuture = ch.writeAndFlush(baseMsg);
                System.out.println(baseMsg.getMsgType() + " to " + baseMsg.getToClientId() + ": "
                        + baseMsg.getToClientMsg());
            }
        }

        // Wait until all messages are flushed before closing the channel.
        if (lastWriteFuture != null) {
            lastWriteFuture.sync();
        }
    } finally {
        // The connection is closed automatically on shutdown.
        group.shutdownGracefully();
    }
}

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

License:Apache License

public static void main(String[] args) throws Exception {

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    try {/* www. j  a  v  a  2  s  .c om*/
        ServerBootstrap b = new ServerBootstrap();
        ChannelFuture bindFuture = b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new PortForwardProxyFrontendInitializer(LocalConfig.ins().getRemoteHost(),
                        LocalConfig.ins().getRemotePort()))
                .childOption(ChannelOption.AUTO_READ, false).bind(LocalConfig.ins().getLocalPort());

        bindFuture.sync().channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }

}

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

License:Apache License

public static void main(String[] args) throws Exception {

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    List<HexDumpForwardRule> ruleList = new ArrayList<HexDumpForwardRule>();
    HexDumpForwardRule r3 = new HexDumpForwardRule(9000, "imap.gmail.com", 993, true);
    ruleList.add(r3);/*from ww  w  . ja v a 2  s  . com*/
    HexDumpForwardRule r4 = new HexDumpForwardRule(9001, "smtp.gmail.com", 465, true);
    ruleList.add(r4);

    List<ChannelFuture> bindFutureList = new ArrayList<ChannelFuture>();

    try {
        for (HexDumpForwardRule r : ruleList) {

            ServerBootstrap b = new ServerBootstrap();
            ChannelFuture bindFuture = b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler("BYTE_LOGGER", LogLevel.DEBUG))
                    .childHandler(
                            new HexDumpProxyInitializer(r.getRemoteHost(), r.getRemotePort(), r.isRemoteSsl()))
                    .childOption(ChannelOption.AUTO_READ, false).bind(r.getLocalPort());

            bindFutureList.add(bindFuture);

        }
        for (ChannelFuture f : bindFutureList) {
            f.sync().channel().closeFuture().sync();
        }
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }

}

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/*  w w w.j ava 2 s. c  om*/
        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);
    }
}

From source file:darks.grid.network.GridMessageClient.java

License:Apache License

public Channel connect(SocketAddress address, boolean sync) {
    if (bootstrap == null)
        return null;
    try {//w ww. java  2  s  . c  om
        ChannelFuture f = bootstrap.connect(address);
        if (sync) {
            f.sync();
            Channel channel = f.channel();
            if (channel != null)
                log.info("Succeed to connect " + address);
            else
                log.error("Fail to connect " + address);
            return channel;
        } else
            return f.channel();
    } catch (Exception e) {
        if (e instanceof ConnectException)
            log.error("Fail to connect " + address);
        else
            log.error(e.getMessage(), e);
        return null;
    }
}

From source file:de.felix_klauke.pegasus.server.network.NettyServer.java

License:Apache License

/**
 * Start the most epic Server ever.//from w  w  w .j  a v a2 s  .c om
 */
public void start() {
    logger.info("Starting Netty Server...");

    serverBootstrap.channel(NioServerSocketChannel.class);
    serverBootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup());
    serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
    serverBootstrap.childHandler(channelInitializer);

    logger.info("NettyServer is configured. Server will be binded now...");

    ChannelFuture future = serverBootstrap.bind(27816);

    logger.info("Nettyserver is bound. Blocking Thread with the Server now...");

    try {
        future.sync().channel().closeFuture().sync();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:de.ocarthon.core.network.tcp.TCPClient.java

License:Apache License

public boolean connect(String host, int port) throws InterruptedException {
    if (bootstrap != null) {
        ChannelFuture cf = bootstrap.connect(host, port);
        cf.sync();

        if (cf.isSuccess()) {
            this.channel = cf.channel();
        }// w  w w .ja v  a  2 s  .  c  om

        return cf.isSuccess();
    } else {
        throw new IllegalStateException("TCPClient#setup() must be called first!");
    }
}

From source file:de.ocarthon.core.network.tcp.TCPServer.java

License:Apache License

public boolean bind(String host, int port) throws InterruptedException {
    ChannelFuture cf = this.bootstrap.bind(host, port);
    cf.sync();
    serverChannel = cf.channel();/*from   ww w. ja  v  a 2  s  .c  om*/
    return cf.isSuccess();
}