<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import game.PlayerData;
import game.PlayerDataList;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author PastaPojken
 */
public class Client implements Runnable {

    private DatagramSocket srvSocket;
    private int srvPort;
    private String myID, srvIP;
    private boolean running, connected;
    private Thread InThread, OutThread, heartBeat, heartAttack;
    private PlayerDataList srvPlayerDataList;
    private PlayerData bufferedPlayerData;
    private Long heartTime, lastPacket;
    private int PACKAGE_SIZE, TICK_RATE;

    public Client(String ip, int port, String id) {
        PACKAGE_SIZE = 32;
        srvIP = ip;
        srvPort = port;
        myID = id;
        lastPacket = (long) 0;
        try {
            srvSocket = new DatagramSocket();
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
        connected = true;
        System.out.println("Connecting...");
        sendObj("heartbeat");
        heartTime = System.currentTimeMillis();
        running = true;

        heartBeat = new Thread(new Runnable() {
            @Override
            public void run() {
                while (running) {
                    Object obj = getObj(new byte[PACKAGE_SIZE], PACKAGE_SIZE);
                    if (obj instanceof String) {
                        String str = (String) obj;
                        if (str.contains("heartbeat-")) {
                            connected = true;
                            running = false;
                            System.out.println("Connected!");
                            PACKAGE_SIZE = Integer.parseInt(str.substring(str.indexOf("-") + 1, str.lastIndexOf("-")));
                            TICK_RATE = Integer.parseInt(str.substring(str.lastIndexOf("-") + 1, str.length()));
                            heartAttack.interrupt();
                        } else {
                            connected = false;
                        }
                    }
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                    }
                }
            }
        });
        heartAttack = new Thread(new Runnable() {
            @Override
            public void run() {
                while (running) {
                    sendObj("heartbeat");
                    if ((System.currentTimeMillis() - heartTime) >= 10000) { //10s
                        System.out.println("Unable to connect to the server!");<commit_msg>Super small bug fix! :D<commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import game.PlayerData;
import game.PlayerDataList;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author PastaPojken
 */
public class Client implements Runnable {

    private DatagramSocket srvSocket;
    private int srvPort;
    private String myID, srvIP;
    private boolean running, connected;
    private Thread InThread, OutThread, heartBeat, heartAttack;
    private PlayerDataList srvPlayerDataList;
    private PlayerData bufferedPlayerData;
    private Long heartTime, lastPacket;
    private int PACKAGE_SIZE, TICK_RATE;

    public Client(String ip, int port, String id) {
        PACKAGE_SIZE = 32;
        srvIP = ip;
        srvPort = port;
        myID = id;
        lastPacket = (long) 0;
        try {
            srvSocket = new DatagramSocket();
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
        connected = true;
        System.out.println("Connecting...");
        sendObj("heartbeat");
        heartTime = System.currentTimeMillis();
        running = true;

        heartBeat = new Thread(new Runnable() {
            @Override
            public void run() {
                while (running) {
                    Object obj = getObj(new byte[PACKAGE_SIZE], PACKAGE_SIZE);
                    if (obj instanceof String) {
                        String str = (String) obj;
                        if (str.contains("heartbeat-")) {
                            connected = true;
                            running = false;
                            System.out.println("Connected!");
                            PACKAGE_SIZE = Integer.parseInt(str.substring(str.indexOf("-") + 1, str.lastIndexOf("-")));
                            TICK_RATE = Integer.parseInt(str.substring(str.lastIndexOf("-") + 1, str.length()));
                            heartAttack.interrupt();
                        } else {
                            connected = false;
                        }
                    }
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException ex) {
                    }
                }
            }
        });
        heartAttack = new Thread(new Runnable() {
            @Override
            public void run() {
                while (running) {
                    sendObj("heartbeat");
                    if ((System.currentTimeMillis() - heartTime) >= 10000) { //10s
                        System.out.println("Unable to connect to the server!");
{'fraction_non_alphanumeric': 0.09571382533032549, 'fraction_numerical': 0.0045117628101836935, 'mean_word_length': 1.7283516483516483, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5602940', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1546, 'n_words': 405}