<commit_before>package ru.nsu.ccfit.bogush;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;
import ru.nsu.ccfit.bogush.message.types.Text;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import java.util.concurrent.ArrayBlockingQueue;

public class Server implements Runnable {
	static { LoggingConfiguration.addConfigFile(LoggingConfiguration.DEFAULT_LOGGER_CONFIG_FILE); }
	private static final Logger logger = LogManager.getLogger(Server.class.getSimpleName());

	private static final String DO_LOGGING_KEY = "log";
	private static final String SERVER_PORT_KEY = "server-port";

	private static final String DO_LOGGING_DEFAULT = "true";
	private static final String SERVER_PORT_DEFAULT = "0";

	private static final String PROPERTIES_FILE = "server.properties";
	private static final String PROPERTIES_COMMENT = "Server properties file";

	private static final Properties DEFAULT_PROPERTIES = new Properties();

	static {
		DEFAULT_PROPERTIES.setProperty(DO_LOGGING_KEY, DO_LOGGING_DEFAULT);
		DEFAULT_PROPERTIES.setProperty(SERVER_PORT_KEY, SERVER_PORT_DEFAULT);
	}

	private static final int HISTORY_CAPACITY = 50;
	private static final String STOP_COMMAND = "stop";

	private ServerSocket serverSocket;

	private Properties properties = new Properties(DEFAULT_PROPERTIES);

	private HashSet<ConnectedUser> connectedUsers = new HashSet<>();

	private ArrayBlockingQueue<Text> history = new ArrayBlockingQueue<>(HISTORY_CAPACITY);

	private int port;

	private Thread thread;

	private static void help() {
		System.out.println("Type 'stop' to stop the server");
	}

	public static void main(String[] args) {
		Server server = new Server();
	}

	private Server() {
		configure();
		thread = new Thread(this, this.getClass().getSimpleName());
		start();
		enterConsoleMode();
	}

	private void enterConsoleMode() {
		logger.info("Entering console mode...\n\n");
		help();
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
			String command;
			while ((command = reader.readLine()) != null) {
				if (command.trim().toLowerCase().equals(STOP_COMMAND)) {
					logger.trace("Command '{}' received", STOP_COMMAND);
					stop();
					return;
				} else {
					System.out.println("Unknown command");
					help();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("eof found");
			System.exit(1);
		}
	}

	@Override
	public void run() {
		try {
			serverSocket = new ServerSocket(port);
			try {
				if (port == 0) {
					port = serverSocket.getLocalPort();
					logger.info("Port set automatically to {}", port);
				}
				logger.info("Created socket on port {}", port);
				logger.info("Server localhost ip: {}", InetAddress.getLocalHost().getHostAddress());
				while (!Thread.interrupted()) {
					logger.info("");
					logger.info("");
					Socket socket = serverSocket.accept();
					logger.info("Socket [{}] accepted", socket.getInetAddress().getHostAddress());
					ConnectedUser connectedUser = new ConnectedUser(this, socket);
					if (connectedUsers.contains(connectedUser)) {
						logger.debug("User already connected");
					} else {
						connectedUsers.add(connectedUser);
						connectedUser.start();
					}
				}
			} finally {
				serverSocket.close();
			}
		} catch (SocketException e) {
			logger.info(e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			logger.info("Stop server");
			stop();
		}
	}

	private void start() {
		logger.info("Start server");
		thread.start();
		logger.info("Server started");
	}

	private void stop() {
		logger.info("Stopping the server");
		try {
			serverSocket.close();
		} catch (IOException e) {
			logger.error("Couldn't close server socket: {}", e.getMessage());
		}

		for (ConnectedUser user : connectedUsers) {
			user.stop();
		}
		thread.interrupt();
		logger.info("Server stopped. Exiting...");
	}

	void logout(ConnectedUser connectedUser) {
		connectedUsers.remove(connectedUser);
		connectedUser.stop();
	}

	void addToHistory(Text message) {
		logger.trace("Add \"{}\" to history", message.toString());<commit_msg>Refactor<commit_after>package ru.nsu.ccfit.bogush;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;
import ru.nsu.ccfit.bogush.message.types.Text;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import java.util.concurrent.ArrayBlockingQueue;

public class Server implements Runnable {
	static { LoggingConfiguration.addConfigFile(LoggingConfiguration.DEFAULT_LOGGER_CONFIG_FILE); }
	private static final Logger logger = LogManager.getLogger(Server.class.getSimpleName());

	private static final String DO_LOGGING_KEY = "log";
	private static final String SERVER_PORT_KEY = "server-port";

	private static final String DO_LOGGING_DEFAULT = "true";
	private static final String SERVER_PORT_DEFAULT = "0";

	private static final String PROPERTIES_FILE = "server.properties";
	private static final String PROPERTIES_COMMENT = "Server properties file";

	private static final Properties DEFAULT_PROPERTIES = new Properties();

	static {
		DEFAULT_PROPERTIES.setProperty(DO_LOGGING_KEY, DO_LOGGING_DEFAULT);
		DEFAULT_PROPERTIES.setProperty(SERVER_PORT_KEY, SERVER_PORT_DEFAULT);
	}

	private static final int HISTORY_CAPACITY = 50;
	private static final String STOP_COMMAND = "stop";

	private ServerSocket serverSocket;

	private Properties properties = new Properties(DEFAULT_PROPERTIES);

	private HashSet<ConnectedUser> connectedUsers = new HashSet<>();

	private ArrayBlockingQueue<Text> history = new ArrayBlockingQueue<>(HISTORY_CAPACITY);

	private int port;

	private Thread thread;

	private static void help() {
		System.out.println("Type 'stop' to stop the server");
	}

	public static void main(String[] args) {
		Server server = new Server();
	}

	private Server() {
		configure();
		thread = new Thread(this, this.getClass().getSimpleName());
		start();
		enterConsoleMode();
	}

	private void enterConsoleMode() {
		logger.info("Entering console mode...\n\n");
		help();
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
			String command;
			while ((command = reader.readLine()) != null) {
				if (command.trim().toLowerCase().equals(STOP_COMMAND)) {
					logger.trace("Command '{}' received", STOP_COMMAND);
					stop();
					return;
				} else {
					System.out.println("Unknown command");
					help();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("eof found");
			System.exit(1);
		}
	}

	@Override
	public void run() {
		try {
			serverSocket = new ServerSocket(port);
			if (port == 0) {
				port = serverSocket.getLocalPort();
				logger.info("Port set automatically to {}", port);
			}
			logger.info("Created socket on port {}", port);
			logger.info("Server localhost ip: {}", InetAddress.getLocalHost().getHostAddress());
			while (!Thread.interrupted()) {
				logger.info("");
				logger.info("");
				Socket socket = serverSocket.accept();
				logger.info("Socket [{}] accepted", socket.getInetAddress().getHostAddress());
				ConnectedUser connectedUser = new ConnectedUser(this, socket);
				if (connectedUsers.contains(connectedUser)) {
					logger.debug("User already connected");
				} else {
					connectedUsers.add(connectedUser);
					connectedUser.start();
				}
			}
		} catch (SocketException e) {
			logger.info(e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			logger.info("Stop server");
			stop();
		}
	}

	private void start() {
		logger.info("Start server");
		thread.start();
		logger.info("Server started");
	}

	private void stop() {
		logger.info("Stopping the server");
		try {
			logger.trace("Closing socket");
			serverSocket.close();
			logger.trace("Socket closed");
		} catch (IOException e) {
			logger.error("Couldn't close server socket: {}", e.getMessage());
		}

		for (ConnectedUser user : connectedUsers) {
			user.stop();
		}
		thread.interrupt();
		logger.info("Server stopped. Exiting...");
	}

	void logout(ConnectedUser connectedUser) {
		connectedUsers.remove(connectedUser);
		connectedUser.stop();
	}

	void addToHistory(Text message) {
		logger.trace("Add \"{}\" to history", message.toString());
{'fraction_non_alphanumeric': 0.13110712684894665, 'fraction_numerical': 0.0020170327207530256, 'mean_word_length': 4.432136335970785, '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': '512752', 'n_tokens_mistral': 3028, 'n_tokens_neox': 2679, 'n_words': 620}