Example usage for io.netty.channel ChannelOption IP_MULTICAST_ADDR

List of usage examples for io.netty.channel ChannelOption IP_MULTICAST_ADDR

Introduction

In this page you can find the example usage for io.netty.channel ChannelOption IP_MULTICAST_ADDR.

Prototype

ChannelOption IP_MULTICAST_ADDR

To view the source code for io.netty.channel ChannelOption IP_MULTICAST_ADDR.

Click Source Link

Usage

From source file:com.heliosapm.shorthand.caster.broadcast.BroadcastListener.java

License:Open Source License

/**
 * Starts a listener on the passed socket address
 * @param isa The socket address to listen on
 * @param nic The network interface to listen on
 *///from w ww .  j  a va 2 s  .co m
public void startListener(InetSocketAddress isa, NetworkInterface nic) {
    Channel channel = null;
    if (isa.getAddress().isMulticastAddress()) {
        channel = bootstrap.group(group).channel(NioDatagramChannel.class)
                //                 .option(ChannelOption.SO_BROADCAST, true)
                .option(ChannelOption.IP_MULTICAST_ADDR, isa.getAddress())
                .option(ChannelOption.SO_REUSEADDR, true)
                .option(ChannelOption.IP_MULTICAST_IF, NetUtil.LOOPBACK_IF)
                .handler(new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel channel) throws Exception {
                        ChannelPipeline pipeline = channel.pipeline();
                        pipeline.addLast(new LoggingHandler(BroadcastListener.class, LogLevel.DEBUG));
                        pipeline.addLast(router);
                    }
                }).localAddress(isa).bind(isa.getPort()).syncUninterruptibly().channel();
        ((NioDatagramChannel) channel).joinGroup(isa, NetUtil.LOOPBACK_IF).syncUninterruptibly();

        //.bind(isa.getPort()).syncUninterruptibly().channel();
        log("Bound to Multicast [%s]", isa);
    } else {
        channel = bootstrap.group(group).channel(NioDatagramChannel.class)
                .option(ChannelOption.SO_BROADCAST, true).handler(new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel channel) throws Exception {
                        ChannelPipeline pipeline = channel.pipeline();
                        pipeline.addLast(new LoggingHandler(BroadcastListener.class, LogLevel.DEBUG));
                        pipeline.addLast(router);
                    }
                }).localAddress(isa).bind(isa).syncUninterruptibly().channel();
        log("Bound to Broadcast UDP [%s]", isa);
    }
    boundChannels.add(channel);

    //.bind().syncUninterruptibly().channel();
    boundChannels.add(channel);
    log("Started Broadcast Listener on [%s]", isa);

}