<commit_before>package com.jenjinstudios.net;

import com.jenjinstudios.io.MessageInputStream;
import com.jenjinstudios.io.MessageOutputStream;
import com.jenjinstudios.message.ExecutableMessage;
import com.jenjinstudios.message.Message;

import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * The communicator class is the superclass for any classes that communicate over socket.
 * @author <NAME>
 */
public abstract class Communicator extends Thread
{
	/** The logger used for this class. */
	private static final Logger LOGGER = Logger.getLogger(Communicator.class.getName());
	/** The list of collected ping times. */
	private final ArrayList<Long> pingTimes;
	/** The collection of messages to send at the next broadcast. */
	private final LinkedList<Message> outgoingMessages;
	/** Flags whether the client threads should be running. */
	private volatile boolean running;
	/** The input stream used to read messages. */
	private MessageInputStream inputStream;
	/** The output stream used to write messages. */
	private MessageOutputStream outputStream;
	/** The socket used to connect. */
	private Socket socket;
	/** Flags whether this client is connected. */
	private volatile boolean connected;
	/** The AES key of this client. */
	private byte[] aesKey;

	/** Cosntruct a new Commuicator. */
	protected Communicator() {
		outgoingMessages = new LinkedList<>();
		pingTimes = new ArrayList<>();
	}

	/** Close the link with the server. */
	protected void closeLink() {
		try // TODO Make sure error is handled gracefully
		{
			inputStream.close();
			outputStream.close();
			socket.close();
		} catch (IOException ex)
		{
			// Do nothing since we're closing the link anyway?
		} finally
		{
			connected = false;
		}
	}

	/**
	 * Set the socket used by this communicator.  Should only be called from subclass.
	 * @param socket The socket to be used by this communicator.
	 * @throws IOException If there is an exception creating message streams.
	 */
	public void setSocket(Socket socket) throws IOException {
		this.socket = socket;
		setOutputStream(new MessageOutputStream(socket.getOutputStream()));
		setInputStream(new MessageInputStream(socket.getInputStream()));
	}

	/** Send a ping request. */
	public void sendPing() {
		Message pingRequest = new Message("PingRequest");
		pingRequest.setArgument("requestTimeNanos", System.nanoTime());
		queueMessage(pingRequest);
	}

	/**
	 * Queue a message in the outgoing messages. This method is thread safe.
	 * @param message The message to add to the outgoing queue.
	 */
	public void queueMessage(Message message) {
		synchronized (outgoingMessages)<commit_msg>Clarified documentation for closeLink method.<commit_after>package com.jenjinstudios.net;

import com.jenjinstudios.io.MessageInputStream;
import com.jenjinstudios.io.MessageOutputStream;
import com.jenjinstudios.message.ExecutableMessage;
import com.jenjinstudios.message.Message;

import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * The communicator class is the superclass for any classes that communicate over socket.
 * @author <NAME>
 */
public abstract class Communicator extends Thread
{
	/** The logger used for this class. */
	private static final Logger LOGGER = Logger.getLogger(Communicator.class.getName());
	/** The list of collected ping times. */
	private final ArrayList<Long> pingTimes;
	/** The collection of messages to send at the next broadcast. */
	private final LinkedList<Message> outgoingMessages;
	/** Flags whether the client threads should be running. */
	private volatile boolean running;
	/** The input stream used to read messages. */
	private MessageInputStream inputStream;
	/** The output stream used to write messages. */
	private MessageOutputStream outputStream;
	/** The socket used to connect. */
	private Socket socket;
	/** Flags whether this client is connected. */
	private volatile boolean connected;
	/** The AES key of this client. */
	private byte[] aesKey;

	/** Cosntruct a new Commuicator. */
	protected Communicator() {
		outgoingMessages = new LinkedList<>();
		pingTimes = new ArrayList<>();
	}

	/** Close the link with the server. */
	protected void closeLink() {
		try
		{
			inputStream.close();
			outputStream.close();
			socket.close();
		} catch (IOException ignored)
		{
			// Link closing, possible _because_ of an IOExeption; will be shutting down.
		} finally
		{
			connected = false;
		}
	}

	/**
	 * Set the socket used by this communicator.  Should only be called from subclass.
	 * @param socket The socket to be used by this communicator.
	 * @throws IOException If there is an exception creating message streams.
	 */
	public void setSocket(Socket socket) throws IOException {
		this.socket = socket;
		setOutputStream(new MessageOutputStream(socket.getOutputStream()));
		setInputStream(new MessageInputStream(socket.getInputStream()));
	}

	/** Send a ping request. */
	public void sendPing() {
		Message pingRequest = new Message("PingRequest");
		pingRequest.setArgument("requestTimeNanos", System.nanoTime());
		queueMessage(pingRequest);
	}

	/**
	 * Queue a message in the outgoing messages. This method is thread safe.
	 * @param message The message to add to the outgoing queue.
	 */
	public void queueMessage(Message message) {
		synchronized (outgoingMessages)
{'fraction_non_alphanumeric': 0.09868064341225374, 'fraction_numerical': 0.0, 'mean_word_length': 4.23062381852552, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '371284', 'n_tokens_mistral': 1574, 'n_tokens_neox': 1474, 'n_words': 541}