Example usage for io.netty.channel ChannelOption SO_BROADCAST

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

Introduction

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

Prototype

ChannelOption SO_BROADCAST

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

Click Source Link

Usage

From source file:org.opendaylight.capwap.ODLCapwapACServer.java

License:Open Source License

@Override
public void start() throws Exception {

    final Bootstrap b = new Bootstrap();
    b.group(group);// w  w  w . j a v a 2 s .  c o  m
    b.channel(NioDatagramChannel.class);
    b.option(ChannelOption.SO_BROADCAST, true);
    b.handler(new ChannelInitializer<NioDatagramChannel>() {
        @Override
        public void initChannel(final NioDatagramChannel ch) throws Exception {

            ChannelPipeline p = ch.pipeline();
            if (security == SecurityType.DTLS) {
                p.addLast(new LoggingHandler("CapwapACServer Log 2", LogLevel.TRACE));
                ChannelHandler dtlsHandler = UscPluginUdp.getSecureServerHandler(ch);
                p.addLast(dtlsHandler);
            }
            p.addLast(new LoggingHandler("CapwapACServer Log 1", LogLevel.TRACE));
            p.addLast(new CapwapPacketHandler());
        }
    });
    b.bind(port).sync().channel().closeFuture().await();
}

From source file:org.opendaylight.dhcp.server.DhcpServer.java

License:Open Source License

public void start(EventLoopGroup eventloopGroup) throws IOException, InterruptedException {
    super.start();
    Bootstrap b = new Bootstrap();
    b.group(eventloopGroup);// ww  w.  j  a v a  2s .com
    b.channel(NioDatagramChannel.class);
    b.option(ChannelOption.SO_BROADCAST, true);
    b.handler(new DhcpHandler(service, this));
    channel = b.bind(port).sync().channel();
}

From source file:org.opendaylight.openflowjava.protocol.impl.clients.UdpSimpleClient.java

License:Open Source License

/**
 * Starting class of {@link UdpSimpleClient}
 *//*from   w w w.  j av a 2  s  . c o  m*/
@Override
public void run() {
    group = new NioEventLoopGroup();
    UdpSimpleClientInitializer clientInitializer = new UdpSimpleClientInitializer(isOnlineFuture);
    clientInitializer.setScenario(scenarioHandler);
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, false)
                .handler(clientInitializer);

        b.connect(host, port).sync();

        synchronized (scenarioHandler) {
            LOG.debug("WAITING FOR SCENARIO");
            while (!scenarioHandler.isScenarioFinished()) {
                scenarioHandler.wait();
            }
        }
    } catch (Exception ex) {
        LOG.error(ex.getMessage(), ex);
    } finally {
        LOG.debug("shutting down");
        try {
            group.shutdownGracefully().get();
            LOG.debug("shutdown succesful");
        } catch (InterruptedException | ExecutionException e) {
            LOG.error(e.getMessage(), e);
        }
    }
    scenarioDone.set(true);
}

From source file:org.opendaylight.openflowjava.protocol.impl.core.UdpHandler.java

License:Open Source License

@Override
public void run() {
    final ChannelFuture f;
    try {/*  w  ww. j  av  a2 s.c o  m*/
        Bootstrap b = new Bootstrap();
        b.group(group).channel(datagramChannelClass).option(ChannelOption.SO_BROADCAST, false)
                .handler(channelInitializer);

        if (startupAddress != null) {
            f = b.bind(startupAddress.getHostAddress(), port).sync();
        } else {
            f = b.bind(port).sync();
        }
    } catch (InterruptedException e) {
        LOG.error("Interrupted while binding port {}", port, e);
        return;
    }

    try {
        InetSocketAddress isa = (InetSocketAddress) f.channel().localAddress();
        String address = isa.getHostString();

        // Update port, as it may have been specified as 0
        this.port = isa.getPort();

        LOG.debug("Address from udpHandler: {}", address);
        isOnlineFuture.set(true);
        LOG.info("Switch listener started and ready to accept incoming udp connections on port: {}", port);
        f.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        LOG.error("Interrupted while waiting for port {} shutdown", port, e);
    } finally {
        shutdown();
    }
}

From source file:org.rzo.netty.mcast.MulticastEndpoint.java

License:Apache License

public void init(ChannelPipelineFactory factory) throws Exception {
    id = String.format("%1$020d", Math.abs(new Random(System.currentTimeMillis()).nextLong())).getBytes();

    group = new OioEventLoopGroup();
    connectionlessBootstrap = new Bootstrap();
    connectionlessBootstrap.group(group);
    connectionlessBootstrap.option(ChannelOption.SO_BROADCAST, true);
    connectionlessBootstrap.handler(factory);
    connectionlessBootstrap.channel(OioDatagramChannel.class);
    ;/*from ww w .j av a  2  s .  co m*/
    datagramChannel = (DatagramChannel) connectionlessBootstrap.bind(new InetSocketAddress(mcastGroupPort))
            .sync().channel();
    multicastAddress = new InetSocketAddress(mcastGroupIp, mcastGroupPort);
    NetworkInterface networkInterface = NetworkInterface.getByInetAddress(InetAddress.getByName(bindAddress));
    // for (Enumeration nifs = NetworkInterface.getNetworkInterfaces();
    // nifs.hasMoreElements(); )
    datagramChannel.joinGroup(multicastAddress, null);// (NetworkInterface)
    // nifs.nextElement());
    init = true;
    if (debug)
        factory.debug();
}

From source file:picoview.collectd.CollectClient.java

License:Apache License

@Override
public void initialize() throws RuntimeException {
    NetworkInterface nic;/*from  w w  w . j  av a  2 s  .com*/
    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);
    }
}

From source file:qotm.QuoteOfTheMomentClient.java

License:Apache License

public static void main(String[] args) throws Exception {

    EventLoopGroup group = new NioEventLoopGroup();
    try {/*from  ww  w.j av a2s .c om*/
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true)
                .handler(new ChannelInitializer<NioDatagramChannel>() {
                    @Override
                    protected void initChannel(NioDatagramChannel ch) throws Exception {
                        //ch.pipeline().addLast(new UdpEncoder());
                        //ch.pipeline().addLast(new QuoteOfTheMomentClientHandler());

                    }
                });

        Channel ch = b.bind(0).sync().channel();

        while (true) {
            // Broadcast the QOTM request to port 8080.
            ch.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8),
                    SocketUtils.socketAddress("192.168.0.23", 124))).sync();
            // Broadcast the QOTM request to port 8080.
            ch.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8),
                    SocketUtils.socketAddress("192.168.0.23", 123))).sync();
        }

        /*ch.writeAndFlush(new DatagramPacket(
            Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8),
            SocketUtils.socketAddress("192.168.0.23", 12345))).sync();*/

        // QuoteOfTheMomentClientHandler will close the DatagramChannel when a
        // response is received.  If the channel is not closed within 5 seconds,
        // print an error message and quit.
        /*if (!ch.closeFuture().await(5000)) {
        System.err.println("QOTM request timed out.");
        }*/
    } finally {
        group.shutdownGracefully();
    }
}

From source file:ru.jts.authserver.AuthServer.java

License:Apache License

public static void startNetworkServer() {
    // For Game Clients
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.option(ChannelOption.SO_BROADCAST, true);
    bootstrap.channel(NioDatagramChannel.class).handler(new AuthClientsChannelHandler());

    String host = AuthServerProperty.getInstance().AUTH_CLIENT_HOST;
    int port = AuthServerProperty.getInstance().AUTH_CLIENT_PORT;
    if (host.equals("*")) {
        bootstrap.localAddress(port);/*from   w w  w .j ava 2s  .com*/
    } else {
        bootstrap.localAddress(host, port);
    }

    NetworkThread clientsNetworkThread = new NetworkThread(bootstrap, true);

    clientsNetworkThread.start();

    log.info("Clients NetworkThread loaded on {}:{}", host, port);

    //TODO  ?   
    // For Game Servers
    bootstrap = new Bootstrap();
    bootstrap.channel(NioServerSocketChannel.class).handler(new GameServersChannelHandler());
    if (host.equals("*")) {
        bootstrap.localAddress(port);
    } else {
        bootstrap.localAddress(host, port);
    }

    NetworkThread serversNetworkThread = new NetworkThread(bootstrap, true);
    serversNetworkThread.start();

    log.info("Servers NetworkThread loaded on {}:{}", host, port);
}

From source file:ru.jts.gameserver.GameServer.java

License:Apache License

public static void startNetworkServer() {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.option(ChannelOption.SO_BROADCAST, true);
    bootstrap.channel(NioDatagramChannel.class).handler(new Game2ClientChannelHandler());

    String host = GameServerProperty.getInstance().GAME_CLIENT_HOST;
    int port = GameServerProperty.getInstance().GAME_CLIENT_PORT;
    if (host.equals("*")) {
        bootstrap.localAddress(port);//from   ww  w . j  a v  a2  s . c o  m
    } else {
        bootstrap.localAddress(host, port);
    }

    NetworkThread clientsNetworkThread = new NetworkThread(bootstrap, true);

    clientsNetworkThread.start();

    log.info("Clients NetworkThread loaded on {}:{}", host, port);
}