<commit_before>package com.bitschupfa.sw16.yaq.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

import com.bitschupfa.sw16.yaq.communication.ConnectedDevice;

import java.io.IOException;

public class ConnectionListener implements Runnable {
    private final static String TAG = "BTConnectionListener";

    private BluetoothServerSocket btServerSocket;
    private volatile boolean isDiscoverable = false;

    public ConnectionListener() {

    }

    @Override
    public void run() {
        Log.d(TAG, "Starting thread.");

        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
        if (btAdapter == null) {
            Log.e(TAG, "Bluetooth not available on device. Killing thread.");
            return;
        }

        if (!isDiscoverable) {
            Log.d(TAG, "Device is not discoverable. Idle until status changes.");
            //noinspection StatementWithEmptyBody
            while (!isDiscoverable);
            Log.d(TAG, "Device is now discoverable.");
        }

        try {
            btServerSocket = btAdapter.listenUsingRfcommWithServiceRecord(BTService.SERVICE_NAME, BTService.SERVICE_UUID);
        } catch (IOException e) {
            Log.e(TAG, "Could not create Server Socket: " + e.getMessage());
            return;
        }

        Log.d(TAG, "Waiting for new connections on the Server Socket.");
        BluetoothSocket socket;
        while (true) {
            try {
                socket = btServerSocket.accept();
            } catch (IOException e) {
                Log.e(TAG, "Server Socket was killed: " + e.getMessage());
                return;
            }

            if (socket != null) {
                try {
                    ConnectedDevice client =  new ConnectedDevice(socket);
                    registerClient(client);
                } catch (IOException e) {
                    Log.e(TAG, "Could not create new ConnectedDevice: " + e.getMessage());
                }
            }
        }
    }

    public void close() {
        if(btServerSocket == null) {
            return;
        }
        Log.d(TAG, "Killing thread.");

        try {
            btServerSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "Error while closing the socket: " + e.getMessage());
        }
    }

    @SuppressWarnings("WeakerAccess")
    public void registerClient(ConnectedDevice client) {
        new Thread(client).start();
        // TODO: register client in the game
    }

    public void setDiscoverable() {
        isDiscoverable = true;
    }
}<commit_msg>[MW, JK] changed discoverability spinlock to a semaphore<commit_after>package com.bitschupfa.sw16.yaq.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

import com.bitschupfa.sw16.yaq.communication.ConnectedDevice;

import java.io.IOException;

public class ConnectionListener implements Runnable {
    private final static String TAG = "BTConnectionListener";

    private BluetoothServerSocket btServerSocket;
    private boolean isDiscoverable = false;


    public ConnectionListener() {

    }

    @Override
    public synchronized void run() {
        Log.d(TAG, "Starting thread.");

        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
        if (btAdapter == null) {
            Log.e(TAG, "Bluetooth not available on device. Killing thread.");
            return;
        }

        if (!isDiscoverable) {
            Log.d(TAG, "Device is not discoverable. Wait until status changes.");
            while (!isDiscoverable) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    Log.e(TAG, "Interrupted while waiting for device to be discoverable. Message: "
                            + e.getMessage());
                    return;
                }
            }
            Log.d(TAG, "Device is now discoverable.");
        }

        try {
            btServerSocket = btAdapter.listenUsingRfcommWithServiceRecord(BTService.SERVICE_NAME, BTService.SERVICE_UUID);
        } catch (IOException e) {
            Log.e(TAG, "Could not create Server Socket: " + e.getMessage());
            return;
        }

        Log.d(TAG, "Waiting for new connections on the Server Socket.");
        BluetoothSocket socket;
        while (true) {
            try {
                socket = btServerSocket.accept();
            } catch (IOException e) {
                Log.e(TAG, "Server Socket was killed: " + e.getMessage());
                return;
            }

            if (socket != null) {
                try {
                    ConnectedDevice client =  new ConnectedDevice(socket);
                    registerClient(client);
                } catch (IOException e) {
                    Log.e(TAG, "Could not create new ConnectedDevice: " + e.getMessage());
                }
            }
        }
    }

    public void close() {
        if(btServerSocket == null) {
            return;
        }
        Log.d(TAG, "Killing thread.");

        try {
            btServerSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "Error while closing the socket: " + e.getMessage());
        }
    }

    @SuppressWarnings("WeakerAccess")
    public void registerClient(ConnectedDevice client) {
        new Thread(client).start();
        // TODO: register client in the game
    }

    public synchronized void setDiscoverable() {
        isDiscoverable = true;
        notify();
    }
}
{'fraction_non_alphanumeric': 0.09693165969316597, 'fraction_numerical': 0.001394700139470014, 'mean_word_length': 1.9048101265822786, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6991230', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1471, 'n_words': 376}