<commit_before>package com.eakjb.meshchat;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import com.eakjb.meshchat.gui.MeshChatWindow;

public class ChatSystem implements Runnable, ChatConstants {
	private final ArrayList<String> addresses = new ArrayList<String>();
	private final ArrayList<String> chats = new ArrayList<String>();
	private final RecieveServer recieveServer = new RecieveServer(this);
	private final MeshChatWindow frame = new MeshChatWindow(this);
	private String localHostName;
	
	private String username = "NoName";
	
	private boolean running = true;
	
	public ChatSystem() throws UnknownHostException {
		this(InetAddress.getLocalHost().getHostName());
	}
	
	public ChatSystem(String localHostName) {
		this.setLocalHostName(localHostName);
	}

	public void run() {
		recieveServer.start();
		frame.setVisible(true);
	}
	
	public void updateUI() {
		StringBuffer b = new StringBuffer();
		
		for (String chat : chats) {
			b.append(chat);
			b.append("\n");
		}
		
		frame.getTextArea().setText(b.toString());
	}
	
	public void updateAddresses(String address) throws IOException {
		Socket sock = new Socket(address,PORT);
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
		BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
		
		System.out.println("Writing message...");
		out.write("1"+METASEPARATOR+"\n");
		out.flush();

		System.out.println("Waiting for response...");
		
		StringBuffer b = new StringBuffer();
		String line;
		while((line=in.readLine())!=null) {
			b.append(line);
			System.out.println(b.toString());
		}

		addClients(b.toString().split(ADDRSEPARATOR));
		
		in.close();
		out.close();
		sock.close();
	}
	
	public void sendChat(String chat) {
		String msg = CHATBRACKETL+username+CHATBRACKETR+" "+chat;
		for (String addr : addresses) {
			Thread t = new Thread(new SendHandler(addr,msg));
			t.start();
		}
		addChat(msg);
	}
	
	private class SendHandler implements Runnable {
		private final String addr;
		private final String chat;
		
		SendHandler(String addr,String chat) {
			this.addr=addr;
			this.chat=chat;
		}
		
		@Override
		public void run() {
			try {
				Socket sock = new Socket(addr,PORT);
				BufferedWriter out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
				out.write("0"+METASEPARATOR+chat);
				out.flush();
				out.close();
				sock.close();
			} catch (IOException e) {
				ErrorHandler.handle(e);
			}
		}<commit_msg>Fixed double chat bug by removing local addition code.  System now inefficeiently connects to locahost socket, but it's ok for now.<commit_after>package com.eakjb.meshchat;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import com.eakjb.meshchat.gui.MeshChatWindow;

public class ChatSystem implements Runnable, ChatConstants {
	private final ArrayList<String> addresses = new ArrayList<String>();
	private final ArrayList<String> chats = new ArrayList<String>();
	private final RecieveServer recieveServer = new RecieveServer(this);
	private final MeshChatWindow frame = new MeshChatWindow(this);
	private String localHostName;
	
	private String username = "NoName";
	
	private boolean running = true;
	
	public ChatSystem() throws UnknownHostException {
		this(InetAddress.getLocalHost().getHostName());
	}
	
	public ChatSystem(String localHostName) {
		this.setLocalHostName(localHostName);
	}

	public void run() {
		recieveServer.start();
		frame.setVisible(true);
	}
	
	public void updateUI() {
		StringBuffer b = new StringBuffer();
		
		for (String chat : chats) {
			b.append(chat);
			b.append("\n");
		}
		
		frame.getTextArea().setText(b.toString());
	}
	
	public void updateAddresses(String address) throws IOException {
		Socket sock = new Socket(address,PORT);
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
		BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
		
		System.out.println("Writing message...");
		out.write("1"+METASEPARATOR+"\n");
		out.flush();

		System.out.println("Waiting for response...");
		
		StringBuffer b = new StringBuffer();
		String line;
		while((line=in.readLine())!=null) {
			b.append(line);
			System.out.println(b.toString());
		}

		addClients(b.toString().split(ADDRSEPARATOR));
		
		in.close();
		out.close();
		sock.close();
	}
	
	public void sendChat(String chat) {
		String msg = CHATBRACKETL+username+CHATBRACKETR+" "+chat;
		for (String addr : addresses) {
			Thread t = new Thread(new SendHandler(addr,msg));
			t.start();
		}
	}
	
	private class SendHandler implements Runnable {
		private final String addr;
		private final String chat;
		
		SendHandler(String addr,String chat) {
			this.addr=addr;
			this.chat=chat;
		}
		
		@Override
		public void run() {
			try {
				Socket sock = new Socket(addr,PORT);
				BufferedWriter out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
				out.write("0"+METASEPARATOR+chat);
				out.flush();
				out.close();
				sock.close();
			} catch (IOException e) {
				ErrorHandler.handle(e);
			}
		}
{'fraction_non_alphanumeric': 0.12367678193366267, 'fraction_numerical': 0.0007057163020465773, 'mean_word_length': 4.5199610516066215, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '4808924', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1708, 'n_words': 407}