Example usage for io.netty.channel ChannelOption IP_MULTICAST_IF

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

Introduction

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

Prototype

ChannelOption IP_MULTICAST_IF

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

Click Source Link

Usage

From source file:org.codice.alliance.video.stream.mpegts.UdpStreamMonitor.java

License:Open Source License

private void runMulticastServer(Bootstrap bootstrap, NetworkInterface networkInterface,
        InetAddress inetAddress) {

    bootstrap.group(eventLoopGroup).channelFactory(() -> new NioDatagramChannel(InternetProtocolFamily.IPv4))
            .handler(new Pipeline(udpStreamProcessor)).localAddress(inetAddress, monitoredPort)
            .option(ChannelOption.IP_MULTICAST_IF, networkInterface).option(ChannelOption.SO_REUSEADDR, true);

    try {//from   w ww  .jav a2  s.c o  m
        channelFuture = bootstrap.bind(monitoredPort).sync();
        NioDatagramChannel ch = (NioDatagramChannel) channelFuture.channel();

        ch.joinGroup(new InetSocketAddress(monitoredAddress, monitoredPort), networkInterface).sync();
    } catch (InterruptedException e) {
        LOGGER.debug("interrupted while waiting for shutdown", e);
    }
}

From source file:org.hawkular.metrics.clients.ptrans.PTrans.java

License:Apache License

/**
 * Starts this PTrans instance. the calling thread will be blocked until another thread calls {@link #stop()}.
 *///from  w  w w.  j  av  a2 s  . co m
public void start() {
    LOG.info("Starting ptrans...");

    Set<Service> services = configuration.getServices();
    List<ChannelFuture> closeFutures = new ArrayList<>(services.size());

    if (services.contains(Service.TCP)) {
        ServerBootstrap serverBootstrap = new ServerBootstrap().group(group, workerGroup)
                .channel(NioServerSocketChannel.class).localAddress(configuration.getTcpPort())
                .childHandler(new TcpChannelInitializer(configuration, forwardingHandler));
        ChannelFuture tcpBindFuture = serverBootstrap.bind().syncUninterruptibly();
        LOG.info("Server listening on TCP {}", tcpBindFuture.channel().localAddress());
        closeFutures.add(tcpBindFuture.channel().closeFuture());
    }

    if (services.contains(Service.UDP)) {
        Bootstrap udpBootstrap = new Bootstrap().group(group).channel(NioDatagramChannel.class)
                .localAddress(configuration.getUdpPort()).handler(new UdpChannelInitializer(forwardingHandler));
        ChannelFuture udpBindFuture = udpBootstrap.bind().syncUninterruptibly();
        LOG.info("Syslogd listening on UDP {}", udpBindFuture.channel().localAddress());
        closeFutures.add(udpBindFuture.channel().closeFuture());
    }

    if (services.contains(Service.GANGLIA)) {
        NetworkInterface mcIf;
        try {
            String multicastIfOverride = configuration.getMulticastIfOverride();
            if (multicastIfOverride == null) {
                Inet4Address hostAddr = (Inet4Address) InetAddress.getLocalHost();
                mcIf = NetworkInterface.getByInetAddress(hostAddr);
            } else {
                mcIf = NetworkInterface.getByName(multicastIfOverride);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        InetSocketAddress gangliaSocket = new InetSocketAddress(configuration.getGangliaGroup(),
                configuration.getGangliaPort());
        Bootstrap gangliaBootstrap = new Bootstrap().group(group).channel(NioDatagramChannel.class)
                .option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.IP_MULTICAST_IF, mcIf)
                .localAddress(gangliaSocket)
                .handler(new GangliaChannelInitializer(configuration, forwardingHandler));
        LOG.trace("Ganglia bootstrap is {}", gangliaBootstrap);
        ChannelFuture gangliaBindFuture = gangliaBootstrap.bind().syncUninterruptibly();
        LOG.info("Ganglia listening on UDP {}", gangliaBindFuture.channel().localAddress());
        DatagramChannel gangliaChannel = (DatagramChannel) gangliaBindFuture.channel();
        gangliaChannel.joinGroup(gangliaSocket, mcIf);
        LOG.trace("Joined the Ganglia group");
        closeFutures.add(gangliaChannel.closeFuture());
    }

    if (services.contains(Service.STATSD)) {
        Bootstrap statsdBootstrap = new Bootstrap().group(group).channel(NioDatagramChannel.class)
                .localAddress(configuration.getStatsDport())
                .handler(new StatsdChannelInitializer(configuration, forwardingHandler));
        ChannelFuture statsdBindFuture = statsdBootstrap.bind().syncUninterruptibly();
        LOG.info("Statsd listening on UDP {}", statsdBindFuture.channel().localAddress());
        closeFutures.add(statsdBindFuture.channel().closeFuture());
    }

    if (services.contains(Service.COLLECTD)) {
        Bootstrap collectdBootstrap = new Bootstrap().group(group).channel(NioDatagramChannel.class)
                .localAddress(configuration.getCollectdPort())
                .handler(new CollectdChannelInitializer(configuration, forwardingHandler));
        ChannelFuture collectdBindFuture = collectdBootstrap.bind().syncUninterruptibly();
        LOG.info("Collectd listening on UDP {}", collectdBindFuture.channel().localAddress());
        closeFutures.add(collectdBindFuture.channel().closeFuture());
    }

    LOG.info("ptrans started");

    closeFutures.forEach(ChannelFuture::syncUninterruptibly);
}

From source file:reactor.io.net.impl.netty.udp.NettyDatagramServer.java

License:Apache License

public NettyDatagramServer(@Nonnull Environment env, @Nonnull Dispatcher dispatcher,
        @Nullable InetSocketAddress listenAddress, @Nullable final NetworkInterface multicastInterface,
        @Nonnull final ServerSocketOptions options, @Nullable Codec<Buffer, IN, OUT> codec) {
    super(env, dispatcher, listenAddress, multicastInterface, options, codec);

    if (options instanceof NettyServerSocketOptions) {
        this.nettyOptions = (NettyServerSocketOptions) options;
    } else {/*from  w  w  w .  j  ava2 s . c om*/
        this.nettyOptions = null;
    }

    if (null != nettyOptions && null != nettyOptions.eventLoopGroup()) {
        this.ioGroup = nettyOptions.eventLoopGroup();
    } else {
        int ioThreadCount = getDefaultEnvironment().getIntProperty("reactor.udp.ioThreadCount",
                Environment.PROCESSORS);
        this.ioGroup = new NioEventLoopGroup(ioThreadCount, new NamedDaemonThreadFactory("reactor-udp-io"));
    }

    final InternetProtocolFamily family = toNettyFamily(options.protocolFamily());

    this.bootstrap = new Bootstrap().group(ioGroup).option(ChannelOption.SO_RCVBUF, options.rcvbuf())
            .option(ChannelOption.SO_SNDBUF, options.sndbuf())
            .option(ChannelOption.SO_REUSEADDR, options.reuseAddr()).option(ChannelOption.AUTO_READ, false)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, options.timeout())
            .channelFactory(new ChannelFactory<Channel>() {
                @Override
                public Channel newChannel() {
                    return new NioDatagramChannel(family);
                }
            });

    if (null != listenAddress) {
        bootstrap.localAddress(listenAddress);
    } else {
        bootstrap.localAddress(NetUtil.LOCALHOST, 3000);
    }
    if (null != multicastInterface) {
        bootstrap.option(ChannelOption.IP_MULTICAST_IF, multicastInterface);
    }
}

From source file:reactor.io.net.netty.udp.NettyDatagramServer.java

License:Apache License

public NettyDatagramServer(@Nonnull Environment env, @Nonnull EventBus reactor,
        @Nullable InetSocketAddress listenAddress, @Nullable final NetworkInterface multicastInterface,
        @Nonnull final ServerSocketOptions options, @Nullable Codec<Buffer, IN, OUT> codec,
        Collection<Consumer<NetChannel<IN, OUT>>> consumers) {
    super(env, reactor, listenAddress, multicastInterface, options, codec, consumers);

    if (options instanceof NettyServerSocketOptions) {
        this.nettyOptions = (NettyServerSocketOptions) options;
    } else {//from ww  w  .  j  a va2 s. c  o m
        this.nettyOptions = null;
    }

    if (null != nettyOptions && null != nettyOptions.eventLoopGroup()) {
        this.ioGroup = nettyOptions.eventLoopGroup();
    } else {
        int ioThreadCount = env.getProperty("reactor.udp.ioThreadCount", Integer.class, Environment.PROCESSORS);
        this.ioGroup = new NioEventLoopGroup(ioThreadCount, new NamedDaemonThreadFactory("reactor-udp-io"));
    }

    final NettyNetChannelInboundHandler inboundHandler = new NettyNetChannelInboundHandler() {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            super.channelRead(ctx, ((DatagramPacket) msg).content());
        }
    };

    this.bootstrap = new Bootstrap().group(ioGroup).option(ChannelOption.SO_RCVBUF, options.rcvbuf())
            .option(ChannelOption.SO_SNDBUF, options.sndbuf())
            .option(ChannelOption.SO_REUSEADDR, options.reuseAddr())
            .channelFactory(new ChannelFactory<Channel>() {
                @Override
                public Channel newChannel() {
                    final NioDatagramChannel ch = new NioDatagramChannel();
                    DatagramChannelConfig config = ch.config();
                    config.setReceiveBufferSize(options.rcvbuf());
                    config.setSendBufferSize(options.sndbuf());
                    config.setReuseAddress(options.reuseAddr());

                    if (null != multicastInterface) {
                        config.setNetworkInterface(multicastInterface);
                    }

                    if (null != nettyOptions && null != nettyOptions.pipelineConfigurer()) {
                        nettyOptions.pipelineConfigurer().accept(ch.pipeline());
                    }

                    ch.closeFuture().addListener(new ChannelFutureListener() {
                        @Override
                        public void operationComplete(ChannelFuture future) throws Exception {
                            if (log.isInfoEnabled()) {
                                log.info("CLOSE {}", ch);
                            }
                            close(ch);
                        }
                    });

                    netChannel = (NettyNetChannel<IN, OUT>) select(ch);
                    inboundHandler.setNetChannel(netChannel);

                    ch.pipeline().addLast(new ChannelOutboundHandlerAdapter() {
                        @Override
                        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
                                throws Exception {
                            super.write(ctx, msg, promise);
                        }
                    });

                    return ch;
                }
            }).handler(inboundHandler);

    if (null != listenAddress) {
        bootstrap.localAddress(listenAddress);
    } else {
        bootstrap.localAddress(NetUtil.LOCALHOST, 3000);
    }
    if (null != multicastInterface) {
        bootstrap.option(ChannelOption.IP_MULTICAST_IF, multicastInterface);
    }
}