List of usage examples for io.netty.channel.socket DatagramChannel joinGroup
ChannelFuture joinGroup(InetSocketAddress multicastAddress, NetworkInterface networkInterface);
From source file:org.apache.camel.component.netty4.SingleUDPNettyServerBootstrapFactory.java
License:Apache License
protected void startServerBootstrap() throws Exception { // create non-shared worker pool EventLoopGroup wg = configuration.getWorkerGroup(); if (wg == null) { // create new pool which we should shutdown when stopping as its not shared workerGroup = new NettyWorkerPoolBuilder().withWorkerCount(configuration.getWorkerCount()) .withName("NettyServerTCPWorker").build(); wg = workerGroup;/*from w w w . j ava2 s. c om*/ } Bootstrap bootstrap = new Bootstrap(); bootstrap.group(wg).channel(NioDatagramChannel.class); // We cannot set the child option here bootstrap.option(ChannelOption.SO_REUSEADDR, configuration.isReuseAddress()); bootstrap.option(ChannelOption.SO_SNDBUF, configuration.getSendBufferSize()); bootstrap.option(ChannelOption.SO_RCVBUF, configuration.getReceiveBufferSize()); bootstrap.option(ChannelOption.SO_BROADCAST, configuration.isBroadcast()); bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, configuration.getConnectTimeout()); // TODO need to find the right setting of below option // only set this if user has specified /* if (configuration.getReceiveBufferSizePredictor() > 0) { bootstrap.setOption("receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(configuration.getReceiveBufferSizePredictor())); }*/ if (configuration.getBacklog() > 0) { bootstrap.option(ChannelOption.SO_BACKLOG, configuration.getBacklog()); } //TODO need to check the additional netty options /* if (configuration.getOptions() != null) { for (Map.Entry<String, Object> entry : configuration.getOptions().entrySet()) { connectionlessBootstrap.setOption(entry.getKey(), entry.getValue()); } }*/ LOG.debug("Created ConnectionlessBootstrap {}", bootstrap); // set the pipeline factory, which creates the pipeline for each newly created channels bootstrap.handler(pipelineFactory); InetSocketAddress hostAddress = new InetSocketAddress(configuration.getHost(), configuration.getPort()); SubnetUtils multicastSubnet = new SubnetUtils(MULTICAST_SUBNET); if (multicastSubnet.getInfo().isInRange(configuration.getHost())) { ChannelFuture channelFuture = bootstrap.bind(hostAddress); channelFuture.awaitUninterruptibly(); channel = channelFuture.channel(); DatagramChannel datagramChannel = (DatagramChannel) channel; String networkInterface = configuration.getNetworkInterface() == null ? LOOPBACK_INTERFACE : configuration.getNetworkInterface(); multicastNetworkInterface = NetworkInterface.getByName(networkInterface); ObjectHelper.notNull(multicastNetworkInterface, "No network interface found for '" + networkInterface + "'."); LOG.info("ConnectionlessBootstrap joining {}:{} using network interface: {}", new Object[] { configuration.getHost(), configuration.getPort(), multicastNetworkInterface.getName() }); datagramChannel.joinGroup(hostAddress, multicastNetworkInterface).syncUninterruptibly(); allChannels.add(datagramChannel); } else { LOG.info("ConnectionlessBootstrap binding to {}:{}", configuration.getHost(), configuration.getPort()); ChannelFuture channelFuture = bootstrap.bind(hostAddress); channelFuture.awaitUninterruptibly(); channel = channelFuture.channel(); allChannels.add(channel); } }
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()}. *//*w ww. j a v a 2s. c om*/ 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:picoview.collectd.CollectClient.java
License:Apache License
@Override public void initialize() throws RuntimeException { NetworkInterface nic;/* ww w . ja v a 2 s.c o m*/ 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); } }