<commit_before>package org.jolokia.discovery;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.*;
import java.util.*;

/**
 * Utility class for handling multicast stuff
 *
 * @author roland
 * @since 28.01.14
 */
public class MulticastUtil {

    // IPv4 Address for Jolokia's Multicast group
    public static final String JOLOKIA_MULTICAST_GROUP_IP4 = "172.16.31.10";

    // IpV6 multicast address (not yet supported)
    public static final String JOLOKIA_MULTICAST_GROUP_IP6 = "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b:84";

    // Multicast port where to listen for queries
    public static final int JOLOKIA_MULTICAST_PORT = 24884;

    // Only available for Java 6
    private static Method isUp;
    private static Method supportsMulticast;

    static {
        // Check for JDK method which are available only for JDK6
        try {
            isUp = NetworkInterface.class.getMethod("isUp", (Class<?>[]) null);
            supportsMulticast = NetworkInterface.class.getMethod("supportsMulticast", (Class<?>[]) null);
        } catch (NoSuchMethodException e) {
            isUp = null;
            supportsMulticast = null;
        }
    }

    static MulticastSocket newMulticastSocket() throws IOException {
        return newMulticastSocket(null);
    }

    static MulticastSocket newMulticastSocket(InetAddress pAddress) throws IOException {

        // TODO: IpV6
        InetAddress address = pAddress != null && pAddress instanceof Inet4Address ? pAddress : getLocalAddress();
        if (address == null) {
            throw new UnknownHostException("Cannot find address of local host which can be used for multicasting");
        }
        MulticastSocket socket = new MulticastSocket(JOLOKIA_MULTICAST_PORT);
        socket.setReuseAddress(true);
        //socket.setNetworkInterface(NetworkInterface.getByInetAddress(address));
        socket.setTimeToLive(255);
        // V6: ffx8::/16
        socket.joinGroup(getMulticastGroup(address));
        return socket;
    }

    private static InetAddress getMulticastGroup(InetAddress pAddress) throws UnknownHostException {
        return InetAddress.getByName(pAddress instanceof Inet6Address ? JOLOKIA_MULTICAST_GROUP_IP6 : JOLOKIA_MULTICAST_GROUP_IP4);
    }

    public static List<DiscoveryIncomingMessage> sendQueryAndCollectAnswers(DiscoveryOutgoingMessage pOutMsg) throws IOException {
        // Note for Ipv6 support: If there are two local addresses, one with IpV6 and one with IpV4 then two discovery request
        // should be sent, on each interface respectively. Currently, only IpV4 is supported.
        InetAddress address = getLocalAddress();
        if (address == null) {
            throw new UnknownHostException("Cannot find address of local host which can be used for sending discover package");
        }
        final DatagramSocket socket = new DatagramSocket(0,address);
        try {
            socket.setSoTimeout(1000);

            // Discover UDP packet send to multicast address
            DatagramPacket out = pOutMsg.getDatagramPacket(getMulticastGroup(address),
                                                           JOLOKIA_MULTICAST_PORT);
            socket.send(out);

            List<DiscoveryIncomingMessage> ret = new ArrayList<DiscoveryIncomingMessage>();
            try {
                do {
                    byte[] buf = new byte[AbstractDiscoveryMessage.MAX_MSG_SIZE];
                            DatagramPacket in = new DatagramPacket(buf, buf.length);
                    socket.receive(in);
                    DiscoveryIncomingMessage inMsg = new DiscoveryIncomingMessage(in);
                    if (!inMsg.isQuery()) {
                        ret.add(inMsg);
                    }
                } while (true); // should leave loop by SocketTimeoutException
            } catch (SocketTimeoutException exp) {
                // Expected
            }
            return ret;
        } finally {
            socket.close();
        }
    }

    private static InetAddress getLocalAddress() throws UnknownHostException {
        InetAddress addr = InetAddress.getLocalHost();
        if (addr.isLoopbackAddress()) {
            // Find local address that isn't a loopback address
            InetAddress lookedUpAddr = findLocalAddress();
            // If a local, multicast enabled address can be found, use it. Otherwise
            // we are using the local address, which might not be what you want
            if (lookedUpAddr != null) {
                addr = lookedUpAddr;
            }
        }
        return addr;
    }

    private static InetAddress findLocalAddress() {
        Enumeration<NetworkInterface> networkInterfaces;
        try {
            networkInterfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            return null;
        }

        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface nif = networkInterfaces.nextElement();
            for (Enumeration<InetAddress> addrEnum = nif.getInetAddresses(); addrEnum.hasMoreElements();) {
                InetAddress interfaceAddress = addrEnum.nextElement();
                if (useInetAddress(nif, interfaceAddress)) {
                    return interfaceAddress;
                }
            }
        }
        return null;
    }

    // Only use the given interface on the given network interface if it is up and supports multicast
    private static boolean useInetAddress(NetworkInterface networkInterface, InetAddress interfaceAddress) {
        return checkMethod(networkInterface, isUp) &&
               checkMethod(networkInterface, supportsMulticast) &&
               // TODO: IpV6 support
               ! (interfaceAddress instanceof Inet6Address) &&
               !interfaceAddress.isLoopbackAddress();
    }

    // Call a method and return the result as boolean. In case of problems, return false.
    private static Boolean checkMethod(NetworkInterface iface, Method toCheck) {
        if (toCheck != null) {
            try {
                return (Boolean) toCheck.invoke(iface, (Object[]) null);
            } catch (IllegalAccessException e) {
                return false;
            } catch (InvocationTargetException e) {
                return false;
            }
        }
        return false;
    }


}
<commit_msg>Reverted last change<commit_after>package org.jolokia.discovery;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.*;
import java.util.*;

/**
 * Utility class for handling multicast stuff
 *
 * @author roland
 * @since 28.01.14
 */
public class MulticastUtil {

    // IPv4 Address for Jolokia's Multicast group
    public static final String JOLOKIA_MULTICAST_GROUP_IP4 = "172.16.31.10";

    // IpV6 multicast address (not yet supported)
    public static final String JOLOKIA_MULTICAST_GROUP_IP6 = "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b:84";

    // Multicast port where to listen for queries
    public static final int JOLOKIA_MULTICAST_PORT = 24884;

    // Only available for Java 6
    private static Method isUp;
    private static Method supportsMulticast;

    static {
        // Check for JDK method which are available only for JDK6
        try {
            isUp = NetworkInterface.class.getMethod("isUp", (Class<?>[]) null);
            supportsMulticast = NetworkInterface.class.getMethod("supportsMulticast", (Class<?>[]) null);
        } catch (NoSuchMethodException e) {
            isUp = null;
            supportsMulticast = null;
        }
    }

    static MulticastSocket newMulticastSocket() throws IOException {
        return newMulticastSocket(null);
    }

    static MulticastSocket newMulticastSocket(InetAddress pAddress) throws IOException {

        // TODO: IpV6
        InetAddress address = pAddress != null && pAddress instanceof Inet4Address ? pAddress : getLocalAddress();
        if (address == null) {
            throw new UnknownHostException("Cannot find address of local host which can be used for multicasting");
        }
        MulticastSocket socket = new MulticastSocket(JOLOKIA_MULTICAST_PORT);
        socket.setReuseAddress(true);
        socket.setNetworkInterface(NetworkInterface.getByInetAddress(address));
        socket.setTimeToLive(255);
        // V6: ffx8::/16
        socket.joinGroup(getMulticastGroup(address));
        return socket;
    }

    private static InetAddress getMulticastGroup(InetAddress pAddress) throws UnknownHostException {
        return InetAddress.getByName(pAddress instanceof Inet6Address ? JOLOKIA_MULTICAST_GROUP_IP6 : JOLOKIA_MULTICAST_GROUP_IP4);
    }

    public static List<DiscoveryIncomingMessage> sendQueryAndCollectAnswers(DiscoveryOutgoingMessage pOutMsg) throws IOException {
        // Note for Ipv6 support: If there are two local addresses, one with IpV6 and one with IpV4 then two discovery request
        // should be sent, on each interface respectively. Currently, only IpV4 is supported.
        InetAddress address = getLocalAddress();
        if (address == null) {
            throw new UnknownHostException("Cannot find address of local host which can be used for sending discover package");
        }
        final DatagramSocket socket = new DatagramSocket(0,address);
        try {
            socket.setSoTimeout(1000);

            // Discover UDP packet send to multicast address
            DatagramPacket out = pOutMsg.getDatagramPacket(getMulticastGroup(address),
                                                           JOLOKIA_MULTICAST_PORT);
            socket.send(out);

            List<DiscoveryIncomingMessage> ret = new ArrayList<DiscoveryIncomingMessage>();
            try {
                do {
                    byte[] buf = new byte[AbstractDiscoveryMessage.MAX_MSG_SIZE];
                            DatagramPacket in = new DatagramPacket(buf, buf.length);
                    socket.receive(in);
                    DiscoveryIncomingMessage inMsg = new DiscoveryIncomingMessage(in);
                    if (!inMsg.isQuery()) {
                        ret.add(inMsg);
                    }
                } while (true); // should leave loop by SocketTimeoutException
            } catch (SocketTimeoutException exp) {
                // Expected
            }
            return ret;
        } finally {
            socket.close();
        }
    }

    private static InetAddress getLocalAddress() throws UnknownHostException {
        InetAddress addr = InetAddress.getLocalHost();
        if (addr.isLoopbackAddress()) {
            // Find local address that isn't a loopback address
            InetAddress lookedUpAddr = findLocalAddress();
            // If a local, multicast enabled address can be found, use it. Otherwise
            // we are using the local address, which might not be what you want
            if (lookedUpAddr != null) {
                addr = lookedUpAddr;
            }
        }
        return addr;
    }

    private static InetAddress findLocalAddress() {
        Enumeration<NetworkInterface> networkInterfaces;
        try {
            networkInterfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            return null;
        }

        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface nif = networkInterfaces.nextElement();
            for (Enumeration<InetAddress> addrEnum = nif.getInetAddresses(); addrEnum.hasMoreElements();) {
                InetAddress interfaceAddress = addrEnum.nextElement();
                if (useInetAddress(nif, interfaceAddress)) {
                    return interfaceAddress;
                }
            }
        }
        return null;
    }

    // Only use the given interface on the given network interface if it is up and supports multicast
    private static boolean useInetAddress(NetworkInterface networkInterface, InetAddress interfaceAddress) {
        return checkMethod(networkInterface, isUp) &&
               checkMethod(networkInterface, supportsMulticast) &&
               // TODO: IpV6 support
               ! (interfaceAddress instanceof Inet6Address) &&
               !interfaceAddress.isLoopbackAddress();
    }

    // Call a method and return the result as boolean. In case of problems, return false.
    private static Boolean checkMethod(NetworkInterface iface, Method toCheck) {
        if (toCheck != null) {
            try {
                return (Boolean) toCheck.invoke(iface, (Object[]) null);
            } catch (IllegalAccessException e) {
                return false;
            } catch (InvocationTargetException e) {
                return false;
            }
        }
        return false;
    }


}

{'fraction_non_alphanumeric': 0.07583897843183057, 'fraction_numerical': 0.010900879856731294, 'mean_word_length': 2.2690251972512088, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3632993', 'n_tokens_mistral': 3410, 'n_tokens_neox': 3231, 'n_words': 910}