<commit_before>package net.happybrackets.core;

import java.io.File;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.Scanner;

public abstract class Device {

	public static final String myHostname;						//the hostname for this PI (wifi)
	public static final String myIP;
	public static final String myMAC;							//the wlan MAC for this PI (wifi)
	public static final String preferedInterface;

	static {
		String tmpHostname = null;
        String tmpIP = null;
		String tmpMAC = null;
		String tmpPreferedInterface = null;
		try {
			NetworkInterface netInterface;
			System.out.println("Detected OS: " + System.getProperty("os.name"));

            System.out.println("Interfaces:");
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            String favouriteInterfaceName = null;
            while (interfaces.hasMoreElements()) {
                netInterface = interfaces.nextElement();
                // Windows by default has a lot of extra interfaces,
                //  lets at least try and getInstance a real interface...
                if (isViableNetworkInterface(netInterface)) {
                    favouriteInterfaceName = netInterface.getName();
                    System.out.println("I like: " + favouriteInterfaceName + ", " + netInterface.getDisplayName());
                }
                else {
                    System.out.println("Ignored: " + netInterface.getName() + ", " + netInterface.getDisplayName());
                }
            }
            if (favouriteInterfaceName != null ) {
                netInterface = NetworkInterface.getByName(favouriteInterfaceName);
            }
            else {
                netInterface = NetworkInterface.getByName("wlan0"); // take a stab in the dark...
            }
            System.out.println("Selected interface: " + netInterface.getName() + ", " + netInterface.getDisplayName());
            tmpHostname = netInterface.getInetAddresses().nextElement().getHostName();
            tmpIP = netInterface.getInetAddresses().nextElement().getHostAddress();

//			if (System.getProperty("os.name").startsWith("Mac OS")) {
//				netInterface = NetworkInterface.getByName("en1");<commit_msg>Fixed device interface and address selection bug.<commit_after>package net.happybrackets.core;

import java.io.File;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Scanner;

public abstract class Device {

	public static final String myHostname;						//the hostname for this PI (wifi)
	public static final String myIP;
	public static final String myMAC;							//the wlan MAC for this PI (wifi)
	public static final String preferedInterface;

	static {
		String tmpHostname = null;
        String tmpIP = null;
		String tmpMAC = null;
		String tmpPreferedInterface = null;
		try {
			NetworkInterface netInterface;
			String operatingSystem = System.getProperty("os.name");
			System.out.println("Detected OS: " + operatingSystem);

            System.out.println("Interfaces:");
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            String favouriteInterfaceName = null;
            ArrayList<NetworkInterface> favouredInterfaces = new ArrayList<>();
            while (interfaces.hasMoreElements()) {
                netInterface = interfaces.nextElement();
                // Windows by default has a lot of extra interfaces,
                //  lets at least try and getInstance a real interface...
                if (isViableNetworkInterface(netInterface)) {
                    //collect all viable interfaces
                    favouredInterfaces.add(netInterface);
                    //favouriteInterfaceName = netInterface.getName();
                    //System.out.println("I like: " + favouriteInterfaceName + ", " + netInterface.getDisplayName());
                }
                else {
                    System.out.println("Ignored: " + netInterface.getName() + ", " + netInterface.getDisplayName());
                }
            }
            if ( !favouredInterfaces.isEmpty() ) {
                System.out.println("Selecting from valid interfaces:");
                favouredInterfaces.forEach((i) -> System.out.println("\t" + i.getName() + ", " + i.getDisplayName()));

                if (favouredInterfaces.size() == 1) {
                    netInterface = favouredInterfaces.get(0);
                } else if (operatingSystem.startsWith("Windows") || operatingSystem.startsWith("Linux")) {
                    favouredInterfaces.sort( (a, b) -> a.getName().compareToIgnoreCase(b.getName()) ); //sort interface by name
                    netInterface = favouredInterfaces.get(favouredInterfaces.size() - 1); //get last, this should be a wlan interface if available
                }
                else if (operatingSystem.startsWith("Mac OS")) {
                    netInterface = favouredInterfaces.get(0);
                }
                else {
                    System.err.println("Operating system " + operatingSystem + " is not expressly handled, defaulting to first favoured interface");
                    netInterface = favouredInterfaces.get(0);
                }
            }
            else {
                // take a stab in the dark...
                if (operatingSystem.startsWith("Linux") || operatingSystem.startsWith("Windows")) {
                    netInterface = NetworkInterface.getByName("wlan0");
                }
                else if (operatingSystem.startsWith("Mac OS")) {
                    netInterface = NetworkInterface.getByName("en1");
                }
                else {
                    System.err.println("Unable to determine a network interface!");
                    netInterface = NetworkInterface.getByIndex(0); //Maybe the loopback?
                }
            }

            //report back
            System.out.println("Selected interface: " + netInterface.getName() + ", " + netInterface.getDisplayName());
            tmpHostname = netInterface.getInetAddresses().nextElement().getHostName();
            tmpIP = netInterface.getInetAddresses().nextElement().getHostAddress();

//			if (System.getProperty("os.name").startsWith("Mac OS")) {
//				netInterface = NetworkInterface.getByName("en1");
{'fraction_non_alphanumeric': 0.10552763819095477, 'fraction_numerical': 0.0016750418760469012, 'mean_word_length': 2.282358820589705, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '256171', 'n_tokens_mistral': 1707, 'n_tokens_neox': 1591, 'n_words': 378}