Example usage for java.net InterfaceAddress getBroadcast

List of usage examples for java.net InterfaceAddress getBroadcast

Introduction

In this page you can find the example usage for java.net InterfaceAddress getBroadcast.

Prototype

public InetAddress getBroadcast() 

Source Link

Document

Returns an InetAddress for the broadcast address for this InterfaceAddress.

Usage

From source file:org.eclipse.smarthome.binding.lifx.internal.LifxLightDiscovery.java

@Override
protected void activate(Map<String, Object> configProperties) {
    super.activate(configProperties);

    broadcastAddresses = new ArrayList<InetSocketAddress>();
    interfaceAddresses = new ArrayList<InetAddress>();

    Enumeration<NetworkInterface> networkInterfaces = null;
    try {//from  ww w . ja va2  s.  com
        networkInterfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        logger.debug("An exception occurred while discovering LIFX lights : '{}'", e.getMessage());
    }
    if (networkInterfaces != null) {
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface iface = networkInterfaces.nextElement();
            try {
                if (iface.isUp() && !iface.isLoopback()) {
                    for (InterfaceAddress ifaceAddr : iface.getInterfaceAddresses()) {
                        if (ifaceAddr.getAddress() instanceof Inet4Address) {
                            logger.debug("Adding '{}' as interface address with MTU {}", ifaceAddr.getAddress(),
                                    iface.getMTU());
                            if (iface.getMTU() > bufferSize) {
                                bufferSize = iface.getMTU();
                            }
                            interfaceAddresses.add(ifaceAddr.getAddress());
                            if (ifaceAddr.getBroadcast() != null) {
                                logger.debug("Adding '{}' as broadcast address", ifaceAddr.getBroadcast());
                                broadcastAddresses
                                        .add(new InetSocketAddress(ifaceAddr.getBroadcast(), BROADCAST_PORT));
                            }
                        }
                    }
                }
            } catch (SocketException e) {
                logger.debug("An exception occurred while discovering LIFX lights : '{}'", e.getMessage());
            }
        }
    }
}

From source file:org.openhab.binding.harmonyhub.discovery.HarmonyHubDiscovery.java

/**
 * Send broadcast message over all active interfaces
 *
 * @param discoverString/*  w ww . ja  v a 2  s  .  c om*/
 *            String to be used for the discovery
 */
private void sendDiscoveryMessage(String discoverString) {
    DatagramSocket bcSend = null;
    try {
        bcSend = new DatagramSocket();
        bcSend.setBroadcast(true);

        byte[] sendData = discoverString.getBytes();

        // Broadcast the message over all the network interfaces
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface networkInterface = interfaces.nextElement();
            if (networkInterface.isLoopback() || !networkInterface.isUp()) {
                continue;
            }
            for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
                InetAddress[] broadcast = null;

                if (StringUtils.isNotBlank(optionalHost)) {
                    try {
                        broadcast = new InetAddress[] { InetAddress.getByName(optionalHost) };
                    } catch (Exception e) {
                        logger.error("Could not use host for hub discovery", e);
                        return;
                    }
                } else {
                    broadcast = new InetAddress[] { InetAddress.getByName("224.0.0.1"),
                            InetAddress.getByName("255.255.255.255"), interfaceAddress.getBroadcast() };
                }

                for (InetAddress bc : broadcast) {
                    // Send the broadcast package!
                    if (bc != null) {
                        try {
                            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, bc,
                                    DISCO_PORT);
                            bcSend.send(sendPacket);
                        } catch (IOException e) {
                            logger.debug("IO error during HarmonyHub discovery: {}", e.getMessage());
                        } catch (Exception e) {
                            logger.debug(e.getMessage(), e);
                        }
                        logger.trace("Request packet sent to: {} Interface: {}", bc.getHostAddress(),
                                networkInterface.getDisplayName());
                    }
                }
            }
        }

    } catch (IOException e) {
        logger.debug("IO error during HarmonyHub discovery: {}", e.getMessage());
    } finally {
        try {
            if (bcSend != null) {
                bcSend.close();
            }
        } catch (Exception e) {
            // Ignore
        }
    }

}