Search in sources :

Example 91 with EventLoopGroup

use of io.netty.channel.EventLoopGroup in project netty by netty.

the class QuoteOfTheMomentClient method main.

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true).handler(new QuoteOfTheMomentClientHandler());
        Channel ch = b.bind(0).sync().channel();
        // Broadcast the QOTM request to port 8080.
        ch.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8), SocketUtils.socketAddress("255.255.255.255", PORT))).sync();
        // print an error message and quit.
        if (!ch.closeFuture().await(5000)) {
            System.err.println("QOTM request timed out.");
        }
    } finally {
        group.shutdownGracefully();
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Channel(io.netty.channel.Channel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) DatagramPacket(io.netty.channel.socket.DatagramPacket) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 92 with EventLoopGroup

use of io.netty.channel.EventLoopGroup in project netty by netty.

the class QuoteOfTheMomentServer method main.

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true).handler(new QuoteOfTheMomentServerHandler());
        b.bind(PORT).sync().channel().closeFuture().await();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 93 with EventLoopGroup

use of io.netty.channel.EventLoopGroup in project netty by netty.

the class RedisClient method main.

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                p.addLast(new RedisDecoder());
                p.addLast(new RedisBulkStringAggregator());
                p.addLast(new RedisArrayAggregator());
                p.addLast(new RedisEncoder());
                p.addLast(new RedisClientHandler());
            }
        });
        // Start the connection attempt.
        Channel ch = b.connect(HOST, PORT).sync().channel();
        // Read commands from the stdin.
        System.out.println("Enter Redis commands (quit to end)");
        ChannelFuture lastWriteFuture = null;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        for (; ; ) {
            final String input = in.readLine();
            final String line = input != null ? input.trim() : null;
            if (line == null || "quit".equalsIgnoreCase(line)) {
                // EOF or "quit"
                ch.close().sync();
                break;
            } else if (line.isEmpty()) {
                // skip `enter` or `enter` with spaces.
                continue;
            }
            // Sends the received line to the server.
            lastWriteFuture = ch.writeAndFlush(line);
            lastWriteFuture.addListener(new GenericFutureListener<ChannelFuture>() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (!future.isSuccess()) {
                        System.err.print("write failed: ");
                        future.cause().printStackTrace(System.err);
                    }
                }
            });
        }
        // Wait until all messages are flushed before closing the channel.
        if (lastWriteFuture != null) {
            lastWriteFuture.sync();
        }
    } finally {
        group.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) InputStreamReader(java.io.InputStreamReader) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) SocketChannel(io.netty.channel.socket.SocketChannel) ChannelPipeline(io.netty.channel.ChannelPipeline) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) RedisEncoder(io.netty.handler.codec.redis.RedisEncoder) BufferedReader(java.io.BufferedReader) Bootstrap(io.netty.bootstrap.Bootstrap) RedisDecoder(io.netty.handler.codec.redis.RedisDecoder) RedisBulkStringAggregator(io.netty.handler.codec.redis.RedisBulkStringAggregator) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) RedisArrayAggregator(io.netty.handler.codec.redis.RedisArrayAggregator)

Example 94 with EventLoopGroup

use of io.netty.channel.EventLoopGroup in project netty by netty.

the class RxtxClient method main.

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new OioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(RxtxChannel.class).handler(new ChannelInitializer<RxtxChannel>() {

            @Override
            public void initChannel(RxtxChannel ch) throws Exception {
                ch.pipeline().addLast(new LineBasedFrameDecoder(32768), new StringEncoder(), new StringDecoder(), new RxtxClientHandler());
            }
        });
        ChannelFuture f = b.connect(new RxtxDeviceAddress(PORT)).sync();
        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) RxtxDeviceAddress(io.netty.channel.rxtx.RxtxDeviceAddress) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) StringEncoder(io.netty.handler.codec.string.StringEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) RxtxChannel(io.netty.channel.rxtx.RxtxChannel) Bootstrap(io.netty.bootstrap.Bootstrap)

Example 95 with EventLoopGroup

use of io.netty.channel.EventLoopGroup in project netty by netty.

the class SctpEchoClient method main.

public static void main(String[] args) throws Exception {
    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSctpChannel.class).option(SctpChannelOption.SCTP_NODELAY, true).handler(new ChannelInitializer<SctpChannel>() {

            @Override
            public void initChannel(SctpChannel ch) throws Exception {
                ch.pipeline().addLast(//new LoggingHandler(LogLevel.INFO),
                new SctpEchoClientHandler());
            }
        });
        // Start the client.
        ChannelFuture f = b.connect(HOST, PORT).sync();
        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        group.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSctpChannel(io.netty.channel.sctp.nio.NioSctpChannel) SctpChannel(io.netty.channel.sctp.SctpChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

EventLoopGroup (io.netty.channel.EventLoopGroup)134 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)92 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)64 Channel (io.netty.channel.Channel)64 Bootstrap (io.netty.bootstrap.Bootstrap)61 Test (org.junit.Test)36 ChannelFuture (io.netty.channel.ChannelFuture)35 SslContext (io.netty.handler.ssl.SslContext)35 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)31 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)28 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)27 LoggingHandler (io.netty.handler.logging.LoggingHandler)26 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)25 SocketChannel (io.netty.channel.socket.SocketChannel)22 ChannelPipeline (io.netty.channel.ChannelPipeline)20 LocalAddress (io.netty.channel.local.LocalAddress)18 LocalChannel (io.netty.channel.local.LocalChannel)18 LocalServerChannel (io.netty.channel.local.LocalServerChannel)18 InetSocketAddress (java.net.InetSocketAddress)18 ByteBuf (io.netty.buffer.ByteBuf)17