Java Code Examples for io.netty.channel.udt.nio.NioUdtProvider#MESSAGE_PROVIDER

The following examples show how to use io.netty.channel.udt.nio.NioUdtProvider#MESSAGE_PROVIDER . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MsgEchoClient.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Configure the client.
        final ThreadFactory connectFactory = new DefaultThreadFactory("connect");
        final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1,
                connectFactory, NioUdtProvider.MESSAGE_PROVIDER);
        try {
            final Bootstrap boot = new Bootstrap();
            boot.group(connectGroup)
                    .channelFactory(NioUdtProvider.MESSAGE_CONNECTOR)
                    .handler(new ChannelInitializer<UdtChannel>() {
                        @Override
                        public void initChannel(final UdtChannel ch)
                                throws Exception {
                            ch.pipeline().addLast(
                                    new LoggingHandler(LogLevel.INFO),
                                    new MsgEchoClientHandler());
                        }
                    });
            // Start the client.
            final ChannelFuture f = boot.connect(HOST, PORT).sync();
            // Wait until the connection is closed.
            f.channel().closeFuture().sync();
        } finally {
            // Shut down the event loop to terminate all threads.
            connectGroup.shutdownGracefully();
        }
    }
 
Example 2
Source File: MsgEchoServer.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    final ThreadFactory acceptFactory = new DefaultThreadFactory("accept");
    final ThreadFactory connectFactory = new DefaultThreadFactory("connect");
    final NioEventLoopGroup acceptGroup =
            new NioEventLoopGroup(1, acceptFactory, NioUdtProvider.MESSAGE_PROVIDER);
    final NioEventLoopGroup connectGroup =
            new NioEventLoopGroup(1, connectFactory, NioUdtProvider.MESSAGE_PROVIDER);

    // Configure the server.
    try {
        final ServerBootstrap boot = new ServerBootstrap();
        boot.group(acceptGroup, connectGroup)
                .channelFactory(NioUdtProvider.MESSAGE_ACCEPTOR)
                .option(ChannelOption.SO_BACKLOG, 10)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<UdtChannel>() {
                    @Override
                    public void initChannel(final UdtChannel ch)
                            throws Exception {
                        ch.pipeline().addLast(
                                new LoggingHandler(LogLevel.INFO),
                                new MsgEchoServerHandler());
                    }
                });
        // Start the server.
        final ChannelFuture future = boot.bind(PORT).sync();
        // Wait until the server socket is closed.
        future.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        acceptGroup.shutdownGracefully();
        connectGroup.shutdownGracefully();
    }
}
 
Example 3
Source File: MsgEchoPeerBase.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
public void run() throws Exception {
    // Configure the peer.
    final ThreadFactory connectFactory = new DefaultThreadFactory("rendezvous");
    final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1,
            connectFactory, NioUdtProvider.MESSAGE_PROVIDER);
    try {
        final Bootstrap boot = new Bootstrap();
        boot.group(connectGroup)
                .channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS)
                .handler(new ChannelInitializer<UdtChannel>() {
                    @Override
                    public void initChannel(final UdtChannel ch)
                            throws Exception {
                        ch.pipeline().addLast(
                                new LoggingHandler(LogLevel.INFO),
                                new MsgEchoPeerHandler(messageSize));
                    }
                });
        // Start the peer.
        final ChannelFuture f = boot.connect(peer, self).sync();
        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        connectGroup.shutdownGracefully();
    }
}
 
Example 4
Source File: MsgEchoClient.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Configure the client.
        final ThreadFactory connectFactory = new DefaultThreadFactory("connect");
        final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1,
                connectFactory, NioUdtProvider.MESSAGE_PROVIDER);
        try {
            final Bootstrap boot = new Bootstrap();
            boot.group(connectGroup)
                    .channelFactory(NioUdtProvider.MESSAGE_CONNECTOR)
                    .handler(new ChannelInitializer<UdtChannel>() {
                        @Override
                        public void initChannel(final UdtChannel ch)
                                throws Exception {
                            ch.pipeline().addLast(
                                    new LoggingHandler(LogLevel.INFO),
                                    new MsgEchoClientHandler());
                        }
                    });
            // Start the client.
            final ChannelFuture f = boot.connect(HOST, PORT).sync();
            // Wait until the connection is closed.
            f.channel().closeFuture().sync();
        } finally {
            // Shut down the event loop to terminate all threads.
            connectGroup.shutdownGracefully();
        }
    }
 
Example 5
Source File: MsgEchoServer.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    final ThreadFactory acceptFactory = new DefaultThreadFactory("accept");
    final ThreadFactory connectFactory = new DefaultThreadFactory("connect");
    final NioEventLoopGroup acceptGroup =
            new NioEventLoopGroup(1, acceptFactory, NioUdtProvider.MESSAGE_PROVIDER);
    final NioEventLoopGroup connectGroup =
            new NioEventLoopGroup(1, connectFactory, NioUdtProvider.MESSAGE_PROVIDER);

    // Configure the server.
    try {
        final ServerBootstrap boot = new ServerBootstrap();
        boot.group(acceptGroup, connectGroup)
                .channelFactory(NioUdtProvider.MESSAGE_ACCEPTOR)
                .option(ChannelOption.SO_BACKLOG, 10)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<UdtChannel>() {
                    @Override
                    public void initChannel(final UdtChannel ch)
                            throws Exception {
                        ch.pipeline().addLast(
                                new LoggingHandler(LogLevel.INFO),
                                new MsgEchoServerHandler());
                    }
                });
        // Start the server.
        final ChannelFuture future = boot.bind(PORT).sync();
        // Wait until the server socket is closed.
        future.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        acceptGroup.shutdownGracefully();
        connectGroup.shutdownGracefully();
    }
}
 
Example 6
Source File: MsgEchoPeerBase.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
public void run() throws Exception {
    // Configure the peer.
    final ThreadFactory connectFactory = new DefaultThreadFactory("rendezvous");
    final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1,
            connectFactory, NioUdtProvider.MESSAGE_PROVIDER);
    try {
        final Bootstrap boot = new Bootstrap();
        boot.group(connectGroup)
                .channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS)
                .handler(new ChannelInitializer<UdtChannel>() {
                    @Override
                    public void initChannel(final UdtChannel ch)
                            throws Exception {
                        ch.pipeline().addLast(
                                new LoggingHandler(LogLevel.INFO),
                                new MsgEchoPeerHandler(messageSize));
                    }
                });
        // Start the peer.
        final ChannelFuture f = boot.connect(peer, self).sync();
        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        connectGroup.shutdownGracefully();
    }
}
 
Example 7
Source File: NioUdtMessageRendezvousChannelTest.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
/**
 * verify basic echo message rendezvous
 *
 * FIXME: Re-enable after making it pass on Windows without unncessary tight loop.
 *        https://github.com/netty/netty/issues/2853
 */
@Test(timeout = 10 * 1000)
@Ignore
public void basicEcho() throws Exception {

    final int messageSize = 64 * 1024;
    final int transferLimit = messageSize * 16;

    final Meter rate1 = Metrics.newMeter(
            NioUdtMessageRendezvousChannelTest.class, "send rate", "bytes", TimeUnit.SECONDS);

    final Meter rate2 = Metrics.newMeter(
            NioUdtMessageRendezvousChannelTest.class, "send rate", "bytes", TimeUnit.SECONDS);

    final InetSocketAddress addr1 = UnitHelp.localSocketAddress();
    final InetSocketAddress addr2 = UnitHelp.localSocketAddress();

    final EchoMessageHandler handler1 = new EchoMessageHandler(rate1, messageSize);
    final EchoMessageHandler handler2 = new EchoMessageHandler(rate2, messageSize);

    final NioEventLoopGroup group1 = new NioEventLoopGroup(
            1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);
    final NioEventLoopGroup group2 = new NioEventLoopGroup(
            1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);

    final Bootstrap boot1 = new Bootstrap();
    boot1.group(group1)
         .channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS)
         .localAddress(addr1).remoteAddress(addr2).handler(handler1);

    final Bootstrap boot2 = new Bootstrap();
    boot2.group(group2)
         .channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS)
         .localAddress(addr2).remoteAddress(addr1).handler(handler2);

    final ChannelFuture connectFuture1 = boot1.connect();
    final ChannelFuture connectFuture2 = boot2.connect();

    while (handler1.meter().count() < transferLimit
            && handler2.meter().count() < transferLimit) {

        log.info("progress : {} {}", handler1.meter().count(), handler2
                .meter().count());

        Thread.sleep(1000);
    }

    connectFuture1.channel().close().sync();
    connectFuture2.channel().close().sync();

    log.info("handler1 : {}", handler1.meter().count());
    log.info("handler2 : {}", handler2.meter().count());

    assertTrue(handler1.meter().count() >= transferLimit);
    assertTrue(handler2.meter().count() >= transferLimit);

    assertEquals(handler1.meter().count(), handler2.meter().count());

    group1.shutdownGracefully();
    group2.shutdownGracefully();

    group1.terminationFuture().sync();
    group2.terminationFuture().sync();
}
 
Example 8
Source File: NioUdtMessageRendezvousChannelTest.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
/**
 * verify basic echo message rendezvous
 *
 * FIXME: Re-enable after making it pass on Windows without unncessary tight loop.
 *        https://github.com/netty/netty/issues/2853
 */
@Test(timeout = 10 * 1000)
@Ignore
public void basicEcho() throws Exception {

    final int messageSize = 64 * 1024;
    final int transferLimit = messageSize * 16;

    final Meter rate1 = Metrics.newMeter(
            NioUdtMessageRendezvousChannelTest.class, "send rate", "bytes", TimeUnit.SECONDS);

    final Meter rate2 = Metrics.newMeter(
            NioUdtMessageRendezvousChannelTest.class, "send rate", "bytes", TimeUnit.SECONDS);

    final InetSocketAddress addr1 = UnitHelp.localSocketAddress();
    final InetSocketAddress addr2 = UnitHelp.localSocketAddress();

    final EchoMessageHandler handler1 = new EchoMessageHandler(rate1, messageSize);
    final EchoMessageHandler handler2 = new EchoMessageHandler(rate2, messageSize);

    final NioEventLoopGroup group1 = new NioEventLoopGroup(
            1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);
    final NioEventLoopGroup group2 = new NioEventLoopGroup(
            1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);

    final Bootstrap boot1 = new Bootstrap();
    boot1.group(group1)
         .channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS)
         .localAddress(addr1).remoteAddress(addr2).handler(handler1);

    final Bootstrap boot2 = new Bootstrap();
    boot2.group(group2)
         .channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS)
         .localAddress(addr2).remoteAddress(addr1).handler(handler2);

    final ChannelFuture connectFuture1 = boot1.connect();
    final ChannelFuture connectFuture2 = boot2.connect();

    while (handler1.meter().count() < transferLimit
            && handler2.meter().count() < transferLimit) {

        log.info("progress : {} {}", handler1.meter().count(), handler2
                .meter().count());

        Thread.sleep(1000);
    }

    connectFuture1.channel().close().sync();
    connectFuture2.channel().close().sync();

    log.info("handler1 : {}", handler1.meter().count());
    log.info("handler2 : {}", handler2.meter().count());

    assertTrue(handler1.meter().count() >= transferLimit);
    assertTrue(handler2.meter().count() >= transferLimit);

    assertEquals(handler1.meter().count(), handler2.meter().count());

    group1.shutdownGracefully();
    group2.shutdownGracefully();

    group1.terminationFuture().sync();
    group2.terminationFuture().sync();
}
 
Example 9
Source File: UdtNetty.java    From netty-4.1.22 with Apache License 2.0 2 votes vote down vote up
public static void main(final String[] args) throws Exception {

        log.info("init");
        TrafficControl.delay(0);

        final AtomicBoolean isOn = new AtomicBoolean(true);

        final InetSocketAddress addr1 = UnitHelp.localSocketAddress();
        final InetSocketAddress addr2 = UnitHelp.localSocketAddress();

        final ChannelHandler handler1 = new EchoMessageHandler(rate, size);
        final ChannelHandler handler2 = new EchoMessageHandler(null, size);

        final NioEventLoopGroup group1 = new NioEventLoopGroup(
                1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);
        final NioEventLoopGroup group2 = new NioEventLoopGroup(
                1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);

        final Bootstrap peerBoot1 = new Bootstrap();
        peerBoot1.group(group1)
                 .channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS)
                 .localAddress(addr1).remoteAddress(addr2).handler(handler1);

        final Bootstrap peerBoot2 = new Bootstrap();
        peerBoot2.group(group2)
                 .channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS)
                 .localAddress(addr2).remoteAddress(addr1).handler(handler2);

        final ChannelFuture peerFuture1 = peerBoot1.connect();
        final ChannelFuture peerFuture2 = peerBoot2.connect();

        CustomReporter.enable(3, TimeUnit.SECONDS);

        Thread.sleep(time);

        isOn.set(false);

        Thread.sleep(1000);

        peerFuture1.channel().close().sync();
        peerFuture2.channel().close().sync();

        Thread.sleep(1000);

        group1.shutdownGracefully();
        group2.shutdownGracefully();

        Metrics.defaultRegistry().shutdown();

        TrafficControl.delay(0);
        log.info("done");
    }
 
Example 10
Source File: UdtNetty.java    From netty4.0.27Learn with Apache License 2.0 2 votes vote down vote up
public static void main(final String[] args) throws Exception {

        log.info("init");
        TrafficControl.delay(0);

        final AtomicBoolean isOn = new AtomicBoolean(true);

        final InetSocketAddress addr1 = UnitHelp.localSocketAddress();
        final InetSocketAddress addr2 = UnitHelp.localSocketAddress();

        final ChannelHandler handler1 = new EchoMessageHandler(rate, size);
        final ChannelHandler handler2 = new EchoMessageHandler(null, size);

        final NioEventLoopGroup group1 = new NioEventLoopGroup(
                1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);
        final NioEventLoopGroup group2 = new NioEventLoopGroup(
                1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);

        final Bootstrap peerBoot1 = new Bootstrap();
        peerBoot1.group(group1)
                 .channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS)
                 .localAddress(addr1).remoteAddress(addr2).handler(handler1);

        final Bootstrap peerBoot2 = new Bootstrap();
        peerBoot2.group(group2)
                 .channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS)
                 .localAddress(addr2).remoteAddress(addr1).handler(handler2);

        final ChannelFuture peerFuture1 = peerBoot1.connect();
        final ChannelFuture peerFuture2 = peerBoot2.connect();

        CustomReporter.enable(3, TimeUnit.SECONDS);

        Thread.sleep(time);

        isOn.set(false);

        Thread.sleep(1000);

        peerFuture1.channel().close().sync();
        peerFuture2.channel().close().sync();

        Thread.sleep(1000);

        group1.shutdownGracefully();
        group2.shutdownGracefully();

        Metrics.defaultRegistry().shutdown();

        TrafficControl.delay(0);
        log.info("done");
    }