<commit_before>package xyz.thepathfinder.android;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.websocket.CloseReason;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.Session;
import java.io.IOException;

/**
 * Controls access the web socket connection with the Pathfinder sever.
 * To gain access to the connection use {@link PathfinderServices#getConnection()}.
 *
 * @author <NAME>
 */
class Connection extends Endpoint {

    private static final Logger logger = LoggerFactory.getLogger(Action.class);

    /**
     * The user's credentials to the Pathfinder server.
     */
    private final String userCredentials;

    /**
     * Access to the model registry to notify {@link Model}s of incoming web socket messages.
     */
    private PathfinderServices services;

    /**
     * The web socket session used to send messages through the web socket.
     */
    private Session session;

    /**
     * Number of messages sent through the web socket.
     */
    private long sentMessageCount;

    /**
     * Handles incoming web socket messages.
     */
    private MessageHandler messageHandler;

    /**
     * Constructs a connection object that controls access to the web socket connection
     * with the Pathfinder Server.
     *
     * @param userCredentials      the user's credentials for this application
     */
    protected Connection(String userCredentials) {
        this.userCredentials = userCredentials;
        this.sentMessageCount = 0L;
    }

    /**
     * Sets the pathfinder services object.
     *
     * @param services a pathfinder services object
     */
    protected void setServices(PathfinderServices services) {
        this.services = services;
        this.messageHandler = new MessageHandler(this.services);
    }

    /**
     * Sends a text message through the web socket to the Pathfinder server.
     *
     * @param message the message to be sent.
     * @throws IllegalStateException the web socket is not connected.
     */
    public void sendMessage(String message) {
        logger.info("Sending json to Pathfinder: " + message);
        if (this.isConnected()) {
            this.session.getAsyncRemote().sendText(message);
            this.sentMessageCount++;
        } else {
            logger.error("Illegal State Exception: The connection to Pathfinder is not open.");
            throw new IllegalStateException("The connection to Pathfinder is not open.");
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onOpen(Session session, EndpointConfig config) {
        logger.info("Pathfinder connection opened");
        this.session = session;
        this.session.addMessageHandler(this.messageHandler);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onClose(Session session, CloseReason closeReason) {
        logger.info("Pathfinder connection closed: " + closeReason);
        this.session = session;
    }

    /**
     * Returns if the web socket is connected.
     *
     * @return <tt>true</tt> if the web socket is connected, <tt>false</tt> otherwise.
     */
    public boolean isConnected() {
        return this.session != null && this.session.isOpen();
    }

    /**
     * Returns the number of messages sent through the web socket.
     *
     * @return the number of messages sent.
     */<commit_msg>Message are stored now if the connection is down and sent when it comes back up<commit_after>package xyz.thepathfinder.android;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.websocket.CloseReason;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.Session;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;

/**
 * Controls access the web socket connection with the Pathfinder sever.
 * To gain access to the connection use {@link PathfinderServices#getConnection()}.
 *
 * @author <NAME>
 */
class Connection extends Endpoint {

    private static final Logger logger = LoggerFactory.getLogger(Action.class);

    /**
     * The user's credentials to the Pathfinder server.
     */
    private final String userCredentials;

    /**
     * Access to the model registry to notify {@link Model}s of incoming web socket messages.
     */
    private PathfinderServices services;

    /**
     * The web socket session used to send messages through the web socket.
     */
    private Session session;

    /**
     * Number of messages sent through the web socket.
     */
    private long sentMessageCount;

    /**
     * Handles incoming web socket messages.
     */
    private MessageHandler messageHandler;

    /**
     * Stores messages while the connection is down.
     */
    private Queue<String> messageQueue;

    /**
     * Constructs a connection object that controls access to the web socket connection
     * with the Pathfinder Server.
     *
     * @param userCredentials      the user's credentials for this application
     */
    protected Connection(String userCredentials) {
        this.userCredentials = userCredentials;
        this.sentMessageCount = 0L;
        this.messageQueue = new LinkedList<String>();
    }

    /**
     * Sets the pathfinder services object.
     *
     * @param services a pathfinder services object
     */
    protected void setServices(PathfinderServices services) {
        this.services = services;
        this.messageHandler = new MessageHandler(this.services);
    }

    private void send(String message) {
        this.session.getAsyncRemote().sendText(message);
        this.sentMessageCount++;
    }

    /**
     * Sends a text message through the web socket to the Pathfinder server.
     *
     * @param message the message to be sent.
     */
    public void sendMessage(String message) {
        logger.info("Sending json to Pathfinder: " + message);
        if (this.isConnected()) {
            this.send(message);
        } else {
            this.messageQueue.add(message);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onOpen(Session session, EndpointConfig config) {
        logger.info("Pathfinder connection opened");
        this.session = session;
        this.session.addMessageHandler(this.messageHandler);

        for(String message : this.messageQueue) {
            this.send(message);
        }
        this.messageQueue.clear();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onClose(Session session, CloseReason closeReason) {
        logger.info("Pathfinder connection closed: " + closeReason);
        this.session = session;
    }

    /**
     * Returns if the web socket is connected.
     *
     * @return <tt>true</tt> if the web socket is connected, <tt>false</tt> otherwise.
     */
    public boolean isConnected() {
        return this.session != null && this.session.isOpen();
    }

    /**
     * Returns the number of messages sent through the web socket.
     *
     * @return the number of messages sent.
     */
{'fraction_non_alphanumeric': 0.09126760563380282, 'fraction_numerical': 0.0008450704225352113, 'mean_word_length': 2.554054054054054, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '4820081', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1810, 'n_words': 592}