Search in sources :

Example 1 with OioDatagramChannel

use of io.netty.channel.socket.oio.OioDatagramChannel in project netty by netty.

the class DatagramMulticastTest method testMulticast.

public void testMulticast(Bootstrap sb, Bootstrap cb) throws Throwable {
    MulticastTestHandler mhandler = new MulticastTestHandler();
    sb.handler(new SimpleChannelInboundHandler<Object>() {

        @Override
        public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        // Nothing will be sent.
        }
    });
    cb.handler(mhandler);
    sb.option(ChannelOption.IP_MULTICAST_IF, NetUtil.LOOPBACK_IF);
    sb.option(ChannelOption.SO_REUSEADDR, true);
    cb.option(ChannelOption.IP_MULTICAST_IF, NetUtil.LOOPBACK_IF);
    cb.option(ChannelOption.SO_REUSEADDR, true);
    cb.localAddress(addr.getPort());
    Channel sc = sb.bind().sync().channel();
    if (sc instanceof OioDatagramChannel) {
        // skip the test for OIO, as it fails because of
        // No route to host which makes no sense.
        // Maybe a JDK bug ?
        sc.close().awaitUninterruptibly();
        return;
    }
    DatagramChannel cc = (DatagramChannel) cb.bind().sync().channel();
    String group = "230.0.0.1";
    InetSocketAddress groupAddress = SocketUtils.socketAddress(group, addr.getPort());
    cc.joinGroup(groupAddress, NetUtil.LOOPBACK_IF).sync();
    sc.writeAndFlush(new DatagramPacket(Unpooled.copyInt(1), groupAddress)).sync();
    assertTrue(mhandler.await());
    // leave the group
    cc.leaveGroup(groupAddress, NetUtil.LOOPBACK_IF).sync();
    // sleep a second to make sure we left the group
    Thread.sleep(1000);
    // we should not receive a message anymore as we left the group before
    sc.writeAndFlush(new DatagramPacket(Unpooled.copyInt(1), groupAddress)).sync();
    mhandler.await();
    sc.close().awaitUninterruptibly();
    cc.close().awaitUninterruptibly();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) OioDatagramChannel(io.netty.channel.socket.oio.OioDatagramChannel) Channel(io.netty.channel.Channel) DatagramChannel(io.netty.channel.socket.DatagramChannel) OioDatagramChannel(io.netty.channel.socket.oio.OioDatagramChannel) DatagramPacket(io.netty.channel.socket.DatagramPacket) OioDatagramChannel(io.netty.channel.socket.oio.OioDatagramChannel) DatagramChannel(io.netty.channel.socket.DatagramChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext)

Aggregations

Channel (io.netty.channel.Channel)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 DatagramChannel (io.netty.channel.socket.DatagramChannel)1 DatagramPacket (io.netty.channel.socket.DatagramPacket)1 OioDatagramChannel (io.netty.channel.socket.oio.OioDatagramChannel)1 InetSocketAddress (java.net.InetSocketAddress)1