Example usage for io.netty.channel ChannelOption IP_MULTICAST_TTL

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

Introduction

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

Prototype

ChannelOption IP_MULTICAST_TTL

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

Click Source Link

Usage

From source file:com.andrewkroh.cicso.rtp.NettyRtpSession.java

License:Apache License

public NettyRtpSession(final InetSocketAddress bindAddress, final NetworkInterface multicastInterface,
        final InetAddress multicastGroup) {
    Preconditions.checkNotNull(bindAddress, "Must specify a bind address.");

    if (multicastGroup != null) {
        Preconditions.checkNotNull(multicastInterface,
                "When specifying the multicast group you must also " + "specify the multicast interface.");

        // Javadoc for Java 7 MulticastChannel states: The channel's
        // socket should be bound to the wildcard address. If the socket
        // is bound to a specific address, rather than the wildcard address
        // then it is implementation specific if multicast datagrams
        // are received by the socket.
        Preconditions.checkArgument(bindAddress.getAddress().isAnyLocalAddress(),
                "Must bind to wildcard address when using multicast.");
    }//  ww w  . jav  a  2s  . c om

    EventLoopGroup workerGroup = new NioEventLoopGroup(NUM_THREADS);

    bootstrap = new Bootstrap();
    bootstrap.group(workerGroup).channel(NioDatagramChannel.class).option(ChannelOption.SO_REUSEADDR, true)
            .localAddress(bindAddress).handler(new ChannelInitializer<Channel>() {
                @Override
                protected void initChannel(Channel ch) throws Exception {
                    ch.pipeline().addLast(new RtpPacketHandler(NettyRtpSession.this));
                }
            });

    if (multicastGroup != null) {
        bootstrap.option(ChannelOption.IP_MULTICAST_TTL, MULTICAST_TTL);

        // All multicast traffic generated from this channel will be
        // output from this interface. If not specified it will use the OS
        // default which may be unpredicatable:
        bootstrap.option(ChannelOption.IP_MULTICAST_IF, multicastInterface);
    }

    channel = (DatagramChannel) bootstrap.bind().syncUninterruptibly().channel();

    LOGGER.info("Session bound to: {}", channel.localAddress());

    if (multicastGroup != null) {
        channel.joinGroup(multicastGroup, multicastInterface, null).syncUninterruptibly();

        LOGGER.info("Session bound to multicast group {} on interface {}.", multicastGroup.getHostAddress(),
                multicastInterface.getDisplayName());
    } else {
        LOGGER.info("Session will not be a multicast listener because " + "no multicast group was specified.");
    }
}

From source file:com.barchart.netty.server.stream.MulticastTransceiver.java

License:BSD License

@Override
protected Bootstrap bootstrap() {

    final Bootstrap bootstrap = new Bootstrap() //
            .group(defaultGroup) //
            .channel(NioDatagramChannel.class) //
            .handler(new ServerChannelInitializer()) //
            .option(ChannelOption.SO_REUSEADDR, true) //
            .option(ChannelOption.IP_MULTICAST_TTL, 3) //
            .option(ChannelOption.SO_SNDBUF, 262144) //
            .option(ChannelOption.SO_RCVBUF, 262144);

    if (bootstrapInit != null) {
        bootstrapInit.initBootstrap(bootstrap);
    }//from   w ww  .jav a 2  s  .  co m

    return bootstrap;

}

From source file:com.barchart.netty.server.stream.UnicastTransceiver.java

License:BSD License

@Override
protected Bootstrap bootstrap() {

    final Bootstrap bootstrap = new Bootstrap() //
            .group(defaultGroup) //
            .channel(NioDatagramChannel.class) //
            .handler(new ServerChannelInitializer()) //
            .remoteAddress(remote) //
            .option(ChannelOption.SO_REUSEADDR, true) //
            .option(ChannelOption.IP_MULTICAST_TTL, 3) //
            .option(ChannelOption.SO_SNDBUF, 262144) //
            .option(ChannelOption.SO_RCVBUF, 262144);

    if (bootstrapInit != null) {
        bootstrapInit.initBootstrap(bootstrap);
    }//from   w w  w . ja  v a  2  s .co  m

    return bootstrap;

}

From source file:com.mpush.client.gateway.GatewayUDPConnector.java

License:Apache License

@Override
protected void initOptions(Bootstrap b) {
    super.initOptions(b);
    b.option(ChannelOption.IP_MULTICAST_LOOP_DISABLED, true);
    b.option(ChannelOption.IP_MULTICAST_TTL, 255);
    if (snd_buf.gateway_client > 0)
        b.option(ChannelOption.SO_SNDBUF, snd_buf.gateway_client);
    if (rcv_buf.gateway_client > 0)
        b.option(ChannelOption.SO_RCVBUF, rcv_buf.gateway_client);
}

From source file:com.mpush.core.server.GatewayUDPConnector.java

License:Apache License

@Override
protected void initOptions(Bootstrap b) {
    super.initOptions(b);
    b.option(ChannelOption.IP_MULTICAST_LOOP_DISABLED, true);//?????IP???IP_MULTICAST_LOOP????
    b.option(ChannelOption.IP_MULTICAST_TTL, 255);//IP_MULTICAST_TTL?TTL0255
    //b.option(ChannelOption.IP_MULTICAST_IF, null);//IP_MULTICAST_IF???????,?addr?IP?INADDR_ANY???
    //b.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(32 * 1024, 1024 * 1024));
    if (snd_buf.gateway_server > 0)
        b.option(ChannelOption.SO_SNDBUF, snd_buf.gateway_server);
    if (rcv_buf.gateway_server > 0)
        b.option(ChannelOption.SO_RCVBUF, rcv_buf.gateway_server);
}

From source file:io.hekate.cluster.seed.multicast.MulticastSeedNodeProvider.java

License:Apache License

@Override
public void startDiscovery(String cluster, InetSocketAddress address) throws HekateException {
    log.info("Starting seed nodes discovery [cluster={}, {}]", cluster, ToString.formatProperties(this));

    SeedNode thisNode = new SeedNode(address, cluster);

    try {//from   w  w  w .  jav  a  2  s  .  co m
        NetworkInterface nif = selectMulticastInterface(address);

        try {
            synchronized (mux) {
                if (isRegistered()) {
                    throw new IllegalStateException(
                            "Multicast seed node provider is already registered with another address "
                                    + "[existing=" + localNode + ']');
                }

                ByteBuf discoveryMsg = prepareDiscovery(thisNode);

                ByteBuf seedNodeInfoBytes = prepareSeedNodeInfo(thisNode);

                localNode = thisNode;

                seedNodes = new HashSet<>();

                eventLoop = new NioEventLoopGroup(1, new HekateThreadFactory("SeedNodeMulticast"));

                // Prepare common bootstrap options.
                Bootstrap bootstrap = new Bootstrap();

                bootstrap.option(ChannelOption.SO_REUSEADDR, true);
                bootstrap.option(ChannelOption.IP_MULTICAST_TTL, ttl);
                bootstrap.option(ChannelOption.IP_MULTICAST_IF, nif);

                if (loopBackDisabled) {
                    bootstrap.option(ChannelOption.IP_MULTICAST_LOOP_DISABLED, true);

                    if (DEBUG) {
                        log.debug("Setting {} option to true", ChannelOption.IP_MULTICAST_LOOP_DISABLED);
                    }
                }

                bootstrap.group(eventLoop);
                bootstrap.channelFactory(() -> new NioDatagramChannel(ipVer));

                // Create a sender channel (not joined to a multicast group).
                bootstrap.localAddress(0);
                bootstrap.handler(createSenderHandler(thisNode));

                ChannelFuture senderBind = bootstrap.bind();

                DatagramChannel localSender = (DatagramChannel) senderBind.channel();

                sender = localSender;

                senderBind.get();

                // Create a listener channel and join to a multicast group.
                bootstrap.localAddress(group.getPort());

                bootstrap.handler(createListenerHandler(thisNode, seedNodeInfoBytes));

                ChannelFuture listenerBind = bootstrap.bind();

                listener = (DatagramChannel) listenerBind.channel();

                listenerBind.get();

                log.info("Joining to a multicast group " + "[address={}, port={}, interface={}, ttl={}]",
                        AddressUtils.host(group), group.getPort(), nif.getName(), ttl);

                listener.joinGroup(group, nif).get();

                // Create a periodic task for discovery messages sending.
                discoveryFuture = eventLoop.scheduleWithFixedDelay(() -> {
                    if (DEBUG) {
                        log.debug("Sending discovery message [from={}]", thisNode);
                    }

                    DatagramPacket discovery = new DatagramPacket(discoveryMsg.copy(), group);

                    localSender.writeAndFlush(discovery);
                }, 0, interval, TimeUnit.MILLISECONDS);
            }
        } catch (ExecutionException e) {
            cleanup();

            throw new HekateException(
                    "Failed to start a multicast seed nodes discovery [node=" + thisNode + ']', e.getCause());
        }

        log.info("Will wait for seed nodes [timeout={}(ms)]", waitTime);

        Thread.sleep(waitTime);
    } catch (InterruptedException e) {
        cleanup();

        Thread.currentThread().interrupt();

        throw new HekateException(
                "Thread was interrupted while awaiting for multicast discovery [node=" + thisNode + ']', e);
    }

    log.info("Done waiting for seed nodes.");
}

From source file:picoview.collectd.CollectClient.java

License:Apache License

@Override
public void initialize() throws RuntimeException {
    NetworkInterface nic;/*from w  w w .j  a  v  a  2 s .c  om*/
    try {
        if (StringUtils.isBlank(nicName)) {
            nic = NetworkInterface.getByIndex(0);
        } else {
            nic = NetworkInterface.getByName(nicName);
        }
    } catch (SocketException exep) {
        throw new RuntimeException("unable to determine network interface to use", exep);
    }

    Bootstrap bs = new Bootstrap();
    bs.option(ChannelOption.SO_BROADCAST, true);
    bs.option(ChannelOption.SO_REUSEADDR, true);
    bs.option(ChannelOption.IP_MULTICAST_LOOP_DISABLED, false);
    bs.option(ChannelOption.SO_RCVBUF, 2048);
    bs.option(ChannelOption.IP_MULTICAST_TTL, 255);

    bs.group(new NioEventLoopGroup());

    bs.channelFactory(new ChannelFactory<Channel>() {
        public Channel newChannel() {
            return new NioDatagramChannel(InternetProtocolFamily.IPv4);
        }
    });
    bs.handler(new ChannelInitializer<DatagramChannel>() {
        @Override
        public void initChannel(DatagramChannel channel) throws Exception {
            channel.pipeline().addLast(new CollectChannelHandler());
        }
    });

    if (StringUtils.isBlank(multicastHost)) {
        multicastHost = "239.192.74.66";
    }
    if (multicastPort <= 0) {
        multicastPort = 25826;
    }

    try {
        DatagramChannel dch = (DatagramChannel) bs.bind(multicastPort).sync().channel();
        ChannelFuture cf = dch.joinGroup(new InetSocketAddress(multicastHost, multicastPort), nic).sync();
        if (!cf.isSuccess()) {
            throw new RuntimeException("unable to join multicast group");
        }
    } catch (InterruptedException exep) {
        throw new RuntimeException("unable to setup network for collect client", exep);
    }
}