Search in sources :

Example 1 with SctpChannel

use of io.netty.channel.sctp.SctpChannel in project netty by netty.

the class SctpMultiHomingEchoClient 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());
            }
        });
        InetSocketAddress localAddress = SocketUtils.socketAddress(CLIENT_PRIMARY_HOST, CLIENT_PORT);
        InetAddress localSecondaryAddress = SocketUtils.addressByName(CLIENT_SECONDARY_HOST);
        InetSocketAddress remoteAddress = SocketUtils.socketAddress(SERVER_REMOTE_HOST, SERVER_REMOTE_PORT);
        // Bind the client channel.
        ChannelFuture bindFuture = b.bind(localAddress).sync();
        // Get the underlying sctp channel
        SctpChannel channel = (SctpChannel) bindFuture.channel();
        // Bind the secondary address.
        // Please note that, bindAddress in the client channel should be done before connecting if you have not
        // enable Dynamic Address Configuration. See net.sctp.addip_enable kernel param
        channel.bindAddress(localSecondaryAddress).sync();
        // Finish connect
        ChannelFuture connectFuture = channel.connect(remoteAddress).sync();
        // Wait until the connection is closed.
        connectFuture.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) SctpEchoClientHandler(io.netty.example.sctp.SctpEchoClientHandler) InetSocketAddress(java.net.InetSocketAddress) Bootstrap(io.netty.bootstrap.Bootstrap) InetAddress(java.net.InetAddress) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 2 with SctpChannel

use of io.netty.channel.sctp.SctpChannel in project netty by netty.

the class SctpEchoTest method testSimpleEcho0.

private static void testSimpleEcho0(ServerBootstrap sb, Bootstrap cb, final boolean unordered) throws Throwable {
    final EchoHandler sh = new EchoHandler();
    final EchoHandler ch = new EchoHandler();
    sb.childHandler(new ChannelInitializer<SctpChannel>() {

        @Override
        public void initChannel(SctpChannel c) throws Exception {
            c.pipeline().addLast(new SctpMessageCompletionHandler(), new SctpInboundByteStreamHandler(0, 0), new SctpOutboundByteStreamHandler(0, 0, unordered), sh);
        }
    });
    cb.handler(new ChannelInitializer<SctpChannel>() {

        @Override
        public void initChannel(SctpChannel c) throws Exception {
            c.pipeline().addLast(new SctpMessageCompletionHandler(), new SctpInboundByteStreamHandler(0, 0), new SctpOutboundByteStreamHandler(0, 0, unordered), ch);
        }
    });
    Channel sc = sb.bind().sync().channel();
    Channel cc = cb.connect().sync().channel();
    for (int i = 0; i < data.length; ) {
        int length = Math.min(random.nextInt(1024 * 64), data.length - i);
        cc.writeAndFlush(Unpooled.wrappedBuffer(data, i, length));
        i += length;
    }
    while (ch.counter < data.length) {
        if (sh.exception.get() != null) {
            break;
        }
        if (ch.exception.get() != null) {
            break;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // Ignore.
        }
    }
    while (sh.counter < data.length) {
        if (sh.exception.get() != null) {
            break;
        }
        if (ch.exception.get() != null) {
            break;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // Ignore.
        }
    }
    sh.channel.close().sync();
    ch.channel.close().sync();
    sc.close().sync();
    if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
        throw sh.exception.get();
    }
    if (ch.exception.get() != null && !(ch.exception.get() instanceof IOException)) {
        throw ch.exception.get();
    }
    if (sh.exception.get() != null) {
        throw sh.exception.get();
    }
    if (ch.exception.get() != null) {
        throw ch.exception.get();
    }
}
Also used : SctpChannel(io.netty.channel.sctp.SctpChannel) SctpMessageCompletionHandler(io.netty.handler.codec.sctp.SctpMessageCompletionHandler) SctpInboundByteStreamHandler(io.netty.handler.codec.sctp.SctpInboundByteStreamHandler) SctpChannel(io.netty.channel.sctp.SctpChannel) Channel(io.netty.channel.Channel) IOException(java.io.IOException) IOException(java.io.IOException) SctpOutboundByteStreamHandler(io.netty.handler.codec.sctp.SctpOutboundByteStreamHandler)

Example 3 with SctpChannel

use of io.netty.channel.sctp.SctpChannel 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)

Example 4 with SctpChannel

use of io.netty.channel.sctp.SctpChannel in project netty by netty.

the class SctpEchoServer method main.

public static void main(String[] args) throws Exception {
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioSctpServerChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SctpChannel>() {

            @Override
            public void initChannel(SctpChannel ch) throws Exception {
                ch.pipeline().addLast(//new LoggingHandler(LogLevel.INFO),
                new SctpEchoServerHandler());
            }
        });
        // Start the server.
        ChannelFuture f = b.bind(PORT).sync();
        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SctpChannel(io.netty.channel.sctp.SctpChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioSctpServerChannel(io.netty.channel.sctp.nio.NioSctpServerChannel)

Example 5 with SctpChannel

use of io.netty.channel.sctp.SctpChannel in project netty by netty.

the class SctpMultiHomingEchoServer method main.

public static void main(String[] args) throws Exception {
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioSctpServerChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SctpChannel>() {

            @Override
            public void initChannel(SctpChannel ch) throws Exception {
                ch.pipeline().addLast(//                             new LoggingHandler(LogLevel.INFO),
                new SctpEchoServerHandler());
            }
        });
        InetSocketAddress localAddress = SocketUtils.socketAddress(SERVER_PRIMARY_HOST, SERVER_PORT);
        InetAddress localSecondaryAddress = SocketUtils.addressByName(SERVER_SECONDARY_HOST);
        // Bind the server to primary address.
        ChannelFuture bindFuture = b.bind(localAddress).sync();
        //Get the underlying sctp channel
        SctpServerChannel channel = (SctpServerChannel) bindFuture.channel();
        //Bind the secondary address
        ChannelFuture connectFuture = channel.bindAddress(localSecondaryAddress).sync();
        // Wait until the connection is closed.
        connectFuture.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SctpChannel(io.netty.channel.sctp.SctpChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) InetSocketAddress(java.net.InetSocketAddress) NioSctpServerChannel(io.netty.channel.sctp.nio.NioSctpServerChannel) SctpServerChannel(io.netty.channel.sctp.SctpServerChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioSctpServerChannel(io.netty.channel.sctp.nio.NioSctpServerChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SctpEchoServerHandler(io.netty.example.sctp.SctpEchoServerHandler) InetAddress(java.net.InetAddress) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

SctpChannel (io.netty.channel.sctp.SctpChannel)5 ChannelFuture (io.netty.channel.ChannelFuture)4 EventLoopGroup (io.netty.channel.EventLoopGroup)4 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)4 Bootstrap (io.netty.bootstrap.Bootstrap)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)2 NioSctpChannel (io.netty.channel.sctp.nio.NioSctpChannel)2 NioSctpServerChannel (io.netty.channel.sctp.nio.NioSctpServerChannel)2 LoggingHandler (io.netty.handler.logging.LoggingHandler)2 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2 Channel (io.netty.channel.Channel)1 SctpServerChannel (io.netty.channel.sctp.SctpServerChannel)1 SctpEchoClientHandler (io.netty.example.sctp.SctpEchoClientHandler)1 SctpEchoServerHandler (io.netty.example.sctp.SctpEchoServerHandler)1 SctpInboundByteStreamHandler (io.netty.handler.codec.sctp.SctpInboundByteStreamHandler)1 SctpMessageCompletionHandler (io.netty.handler.codec.sctp.SctpMessageCompletionHandler)1 SctpOutboundByteStreamHandler (io.netty.handler.codec.sctp.SctpOutboundByteStreamHandler)1 IOException (java.io.IOException)1