<commit_before>package rit.eyeTrackingAPI.EyeTrackerUtilities.udpClients;

import java.io.IOException;
import java.net.BindException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.logging.log4j.Logger;

import rit.eyeTrackingAPI.Event;
import rit.eyeTrackingAPI.EventImpl;
import rit.eyeTrackingAPI.SmoothingFilters.Filter;

/**
 * A class used to set up communication with the mTrackerServerCommandAddress
 * eye tracking device. This class runs on a separate thread. Necessary, because
 * otherwise issues occur when communicating with the
 * mTrackerServerCommandAddress and trying to draw to the GLCanvas at the same
 * time.
 * 
 * TODO: Document how the class is instantiated and prepared for use
 * TODO: The class should be provided with a Protocol instance that implements
 *       the actual operation because commands seem to differ between RED and REDm.
 * 
 * @author <NAME>, <NAME>
 * 
 */
public class IViewXComm extends EyeTrackerClient {
	// <editor-fold defaultstate="expanded" desc="Private Members">

	private State state = new Disconnected();
	private Map<String,Set<MessageListener>> messageListeners = new Hashtable<String,Set<MessageListener>>();
	private boolean stop = false;
	private final boolean toggleOn = true;

	// Set up some members to handle the connection to the tracker, with
	// defaults
	// (Should be able to change these before connecting, or between
	// connections)
	private InetAddress trackerIP = null;
	
	{
		try {
			trackerIP = InetAddress.getByName("127.0.0.1");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
	
	private int trackerPort  = 6665;
	private int localPort 	 = 7777;
	// private int samplingRate = 60; // TODO: RED-dev doesn't care
	
	/**
	 * Whether the transmission format of the eye tracker should be set (true)
	 * or the format already in use by the tracker should be parsed (false).
	 */
	private boolean setETFormat = false;

	// Two sockets, one to receive incoming data and one to send commands to the
	// tracker
	private DatagramSocket mSendSocket;
	private DatagramSocket mReceiveSocket;

	// Packet object used to store incoming data from the tracker
	private DatagramPacket mUdpPacket;

	// </editor-fold>

	// <editor-fold defaultstate="expanded" desc="Constants">

	private static final int MAX_RESPONSE_SIZE_BYTES = 256;

	public static final String PING_COMMAND 					= "ET_PNG\n";
	
	public static final String FORMAT_REQUEST_PREFIX		    = "ET_FRM ";
	
	public static final String CMD_SAMPLE_RATE					= "ET_SRT\n";
	
	public static final String CMD_START_DATA_PREFIX			= "ET_STR "; // can have an optional frame rate
	public static final String CMD_START_DATA				    = "ET_STR\n";
	public static final String CMD_STOP_DATA					= "ET_EST\n";
	
	public static final String CMD_START_5_PT_CALIBRATION		= "ET_CAL 5\n";
	public static final String CMD_CANCEL_CALIBRATION			= "ET_BRK\n";
	public static final String CMD_ACCEPT_CALIBRATION_POINT    	= "ET_ACC\n";
	public static final String CMD_VALIDATE						= "ET_VLS\n";
	
	public static final String MSG_START_CALIBRATION			= "ET_CAL";
	public static final String MSG_CALIBRATION_AREA				= "ET_CSZ";
	public static final String MSG_CALIBRATION_PT				= "ET_PNT";
	public static final String MSG_CALIBRATION_PT_CHANGE		= "ET_CHG";
	public static final String MSG_END_CALIBRATION			    = "ET_FIN";
	public static final String MSG_VALIDATION					= "ET_VLS";
	
	public static final String CMD_TRACKER_PARAMETER_PREFIX 	= "ET_SFT ";
	public static final String PARAM_LEFT_EYE			        = "0";
	public static final String PARAM_RIGHT_EYE			     	= "1";
	public static final String PARAM_PUPIL_THRESHOLD		 	= "0";
	public static final String PARAM_REFLEX_THRESHOLD			= "1";
	public static final String PARAM_SHOW_AOI				    = "2";
	public static final String PARAM_SHOW_CONTOUR				= "3";
	public static final String PARAM_SHOW_PUPIL				    = "4";
	public static final String PARAM_SHOW_REFLEX			    = "5";
	public static final String PARAM_DYNAMIC_THRESHOLD			= "6";
	public static final String PARAM_PUPIL_AREA 				= "11";
	public static final String PARAM_PUPIL_PERIMETER			= "12";
	public static final String PARAM_PUPIL_DENSITY			    = "13";
	public static final String PARAM_REFLEX_PERIMETER			= "14";
	public static final String PARAM_REFLEX_PUPIL_DISTANCE	    = "15";
	
	public static final String CMD_START_IMAGE_STREAMING 		= "ET_SIM\n";
	public static final String CMD_STOP_IMAGE_STREAMING			= "ET_EIM\n";
	
	public static final String CMD_START_IMAGE_RECORDING_PREFIX	= "ET_EVB ";
	public static final String CMD_STOP_IMAGE_RECORDING			= "ET_EVE\n";
	
	public static final String CMD_START_FIXATION_DETECTION 	= "ET_FIX\n";
	public static final String CMD_STOP_FIXATION_DETECTION 		= "ET_EFX\n";
	
	public static final String CMD_CALIBRATION_PARAMS			= "ET_CPA\n";
	
	public static final String RESPONSE_DATA_STRING 			= "ET_SPL";
	
	protected static final Pattern RESPONSE_PATTERN_FIXATION_START =
		Pattern.compile("\\AET_FIX\\s+(?<ET>l|r|b)\\s+(?<TU>\\d+)\\s+(?<SX>[0-9\\-\\.]+)\\s+(?<SY>[0-9\\-\\.]+)\\z");<commit_msg>+ AOIFilter now creates events of ID AOI + now sending correct ET_FIX requests to RED5 + wrestling with vlcj, got VLCjServer working, need to switch to DirectMediaPlayer<commit_after>package rit.eyeTrackingAPI.EyeTrackerUtilities.udpClients;

import java.io.IOException;
import java.net.BindException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.logging.log4j.Logger;

import rit.eyeTrackingAPI.Event;
import rit.eyeTrackingAPI.EventImpl;
import rit.eyeTrackingAPI.SmoothingFilters.Filter;

/**
 * A class used to set up communication with the mTrackerServerCommandAddress
 * eye tracking device. This class runs on a separate thread. Necessary, because
 * otherwise issues occur when communicating with the
 * mTrackerServerCommandAddress and trying to draw to the GLCanvas at the same
 * time.
 * 
 * TODO: Document how the class is instantiated and prepared for use
 * TODO: The class should be provided with a Protocol instance that implements
 *       the actual operation because commands seem to differ between RED and REDm.
 * 
 * @author <NAME>, <NAME>
 * 
 */
public class IViewXComm extends EyeTrackerClient {
	// <editor-fold defaultstate="expanded" desc="Private Members">

	private State state = new Disconnected();
	private Map<String,Set<MessageListener>> messageListeners = new Hashtable<String,Set<MessageListener>>();
	private boolean stop = false;
	private final boolean toggleOn = true;

	// Set up some members to handle the connection to the tracker, with
	// defaults
	// (Should be able to change these before connecting, or between
	// connections)
	private InetAddress trackerIP = null;
	
	{
		try {
			trackerIP = InetAddress.getByName("127.0.0.1");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
	
	private int trackerPort  = 6665;
	private int localPort 	 = 7777;
	// private int samplingRate = 60; // TODO: RED-dev doesn't care
	
	/**
	 * Whether the transmission format of the eye tracker should be set (true)
	 * or the format already in use by the tracker should be parsed (false).
	 */
	private boolean setETFormat = false;

	// Two sockets, one to receive incoming data and one to send commands to the
	// tracker
	private DatagramSocket mSendSocket;
	private DatagramSocket mReceiveSocket;

	// Packet object used to store incoming data from the tracker
	private DatagramPacket mUdpPacket;

	// </editor-fold>

	// <editor-fold defaultstate="expanded" desc="Constants">

	private static final int MAX_RESPONSE_SIZE_BYTES = 256;

	public static final String PING_COMMAND 					= "ET_PNG\n";
	
	public static final String FORMAT_REQUEST_PREFIX		    = "ET_FRM ";
	
	public static final String CMD_SAMPLE_RATE					= "ET_SRT\n";
	
	public static final String CMD_START_DATA_PREFIX			= "ET_STR "; // can have an optional frame rate
	public static final String CMD_START_DATA				    = "ET_STR\n";
	public static final String CMD_STOP_DATA					= "ET_EST\n";
	
	public static final String CMD_START_5_PT_CALIBRATION		= "ET_CAL 5\n";
	public static final String CMD_CANCEL_CALIBRATION			= "ET_BRK\n";
	public static final String CMD_ACCEPT_CALIBRATION_POINT    	= "ET_ACC\n";
	public static final String CMD_VALIDATE						= "ET_VLS\n";
	
	public static final String MSG_START_CALIBRATION			= "ET_CAL";
	public static final String MSG_CALIBRATION_AREA				= "ET_CSZ";
	public static final String MSG_CALIBRATION_PT				= "ET_PNT";
	public static final String MSG_CALIBRATION_PT_CHANGE		= "ET_CHG";
	public static final String MSG_END_CALIBRATION			    = "ET_FIN";
	public static final String MSG_VALIDATION					= "ET_VLS";
	
	public static final String CMD_TRACKER_PARAMETER_PREFIX 	= "ET_SFT ";
	public static final String PARAM_LEFT_EYE			        = "0";
	public static final String PARAM_RIGHT_EYE			     	= "1";
	public static final String PARAM_PUPIL_THRESHOLD		 	= "0";
	public static final String PARAM_REFLEX_THRESHOLD			= "1";
	public static final String PARAM_SHOW_AOI				    = "2";
	public static final String PARAM_SHOW_CONTOUR				= "3";
	public static final String PARAM_SHOW_PUPIL				    = "4";
	public static final String PARAM_SHOW_REFLEX			    = "5";
	public static final String PARAM_DYNAMIC_THRESHOLD			= "6";
	public static final String PARAM_PUPIL_AREA 				= "11";
	public static final String PARAM_PUPIL_PERIMETER			= "12";
	public static final String PARAM_PUPIL_DENSITY			    = "13";
	public static final String PARAM_REFLEX_PERIMETER			= "14";
	public static final String PARAM_REFLEX_PUPIL_DISTANCE	    = "15";
	
	public static final String CMD_START_IMAGE_STREAMING 		= "ET_SIM\n";
	public static final String CMD_STOP_IMAGE_STREAMING			= "ET_EIM\n";
	
	public static final String CMD_START_IMAGE_RECORDING_PREFIX	= "ET_EVB ";
	public static final String CMD_STOP_IMAGE_RECORDING			= "ET_EVE\n";
	
	public static final String CMD_START_FIXATION_DETECTION 	= "ET_FIX\n"; // used by REDm / respective iViewX version
	public static final String CMD_START_FIXATION_DETECTION_PREFIX = "ET_FIX "; // used by RED5 / respective iViewX version
	public static final String CMD_STOP_FIXATION_DETECTION 		= "ET_EFX\n";
	
	public static final String CMD_CALIBRATION_PARAMS			= "ET_CPA\n";
	
	public static final String RESPONSE_DATA_STRING 			= "ET_SPL";
	
	protected static final Pattern RESPONSE_PATTERN_FIXATION_START =
		Pattern.compile("\\AET_FIX\\s+(?<ET>l|r|b)\\s+(?<TU>\\d+)\\s+(?<SX>[0-9\\-\\.]+)\\s+(?<SY>[0-9\\-\\.]+)\\z");
{'fraction_non_alphanumeric': 0.08463949843260188, 'fraction_numerical': 0.008240035826242723, 'mean_word_length': 4.239793524167058, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '3634453', 'n_tokens_mistral': 3927, 'n_tokens_neox': 3581, 'n_words': 1078}