jtl-eclipse-master/JTL.resource.jtl/src-gen/JTL/resource/jtl/debug/JtlDebugProxy.java
/**
 * <copyright>
 * </copyright>
 *
 * 
 */
package JTL.resource.jtl.debug;

/**
 * The DebugProxy allows to communicate between the interpreter, which runs in a
 * separate thread or process and the Eclipse Debug framework (i.e., the
 * DebugTarget class).
 */
public class JtlDebugProxy {
	
	public static final int STARTUP_DELAY = 1000;
	
	private java.io.PrintStream output;
	
	private java.io.BufferedReader reader;
	
	private JTL.resource.jtl.debug.JtlDebugTarget debugTarget;
	
	private JTL.resource.jtl.debug.JtlDebugCommunicationHelper communicationHelper = new JTL.resource.jtl.debug.JtlDebugCommunicationHelper();
	
	public JtlDebugProxy(JTL.resource.jtl.debug.JtlDebugTarget debugTarget, int requestPort) throws java.net.UnknownHostException, java.io.IOException {
		this.debugTarget = debugTarget;
		// give interpreter a chance to start
		try {
			Thread.sleep(STARTUP_DELAY);
		} catch (InterruptedException e) {
		}
		startSocket(requestPort);
	}
	
	private void startSocket(int requestPort) throws java.net.UnknownHostException, java.io.IOException {
		// creating client proxy socket (trying to connect)...
		java.net.Socket client = new java.net.Socket("localhost", requestPort);
		// creating client proxy socket - done. (connected)
		try {
			java.io.BufferedInputStream input = new java.io.BufferedInputStream(client.getInputStream());
			reader = new java.io.BufferedReader(new java.io.InputStreamReader(input));
		} catch (java.io.IOException e) {
			System.out.println(e);
		}
		try {
			output = new java.io.PrintStream(client.getOutputStream());
		} catch (java.io.IOException e) {
			System.out.println(e);
		}
	}
	
	public void resume() {
		sendCommand(JTL.resource.jtl.debug.EJtlDebugMessageTypes.RESUME);
	}
	
	public void stepOver() {
		sendCommand(JTL.resource.jtl.debug.EJtlDebugMessageTypes.STEP_OVER);
	}
	
	public void stepInto() {
		sendCommand(JTL.resource.jtl.debug.EJtlDebugMessageTypes.STEP_INTO);
	}
	
	public void stepReturn() {
		sendCommand(JTL.resource.jtl.debug.EJtlDebugMessageTypes.STEP_RETURN);
	}
	
	public void terminate() {
		sendCommand(JTL.resource.jtl.debug.EJtlDebugMessageTypes.EXIT);
	}
	
	public JTL.resource.jtl.debug.JtlDebugMessage getStack() {
		return sendCommandAndRead(JTL.resource.jtl.debug.EJtlDebugMessageTypes.GET_STACK);
	}
	
	public void addLineBreakpoint(String location, int line) {
		JTL.resource.jtl.debug.JtlDebugMessage message = new JTL.resource.jtl.debug.JtlDebugMessage(JTL.resource.jtl.debug.EJtlDebugMessageTypes.ADD_LINE_BREAKPOINT, new String[] {location, Integer.toString(line)});
		communicationHelper.sendEvent(message, output);
	}
	
	public void removeLineBreakpoint(String location, int line) {
		JTL.resource.jtl.debug.JtlDebugMessage message = new JTL.resource.jtl.debug.JtlDebugMessage(JTL.resource.jtl.debug.EJtlDebugMessageTypes.REMOVE_LINE_BREAKPOINT, new String[] {location, Integer.toString(line)});
		communicationHelper.sendEvent(message, output);
	}
	
	public org.eclipse.debug.core.model.IVariable[] getStackVariables(String stackFrame) {
		JTL.resource.jtl.debug.JtlDebugMessage response = sendCommandAndRead(JTL.resource.jtl.debug.EJtlDebugMessageTypes.GET_FRAME_VARIABLES, new String[] {stackFrame});
		String[] ids = response.getArguments();
		// fetch all variables
		org.eclipse.debug.core.model.IVariable[] variables = getVariables(ids);
		return variables;
	}
	
	public org.eclipse.debug.core.model.IVariable[] getVariables(String... requestedIDs) {
		JTL.resource.jtl.debug.JtlDebugMessage response = sendCommandAndRead(JTL.resource.jtl.debug.EJtlDebugMessageTypes.GET_VARIABLES, requestedIDs);
		String[] varStrings = response.getArguments();
		JTL.resource.jtl.debug.JtlDebugVariable[] variables  = new JTL.resource.jtl.debug.JtlDebugVariable[varStrings.length];
		int i = 0;
		for (String varString : varStrings) {
			java.util.Map<String, String> properties = JTL.resource.jtl.util.JtlStringUtil.convertFromString(varString);
			
			// convert varString to variables and values
			String valueString = properties.get("!valueString");
			String valueRefType = "valueRefType";
			java.util.Map<String, Long> childVariables = new java.util.TreeMap<String, Long>(new java.util.Comparator<String>() {
				public int compare(String s1, String s2) {
					return s1.compareToIgnoreCase(s2);
				}
			});
			for (String property : properties.keySet()) {
				// ignore special properties - they are not children
				if (property.startsWith("!")) {
					continue;
				}
				childVariables.put(property, Long.parseLong(properties.get(property)));
			}
			String id = properties.get("!id");
			org.eclipse.debug.core.model.IValue value = new JTL.resource.jtl.debug.JtlDebugValue(debugTarget, id, valueString, valueRefType, childVariables);
			
			String variableName = properties.get("!name");
			String variableRefType = properties.get("!type");
			
			JTL.resource.jtl.debug.JtlDebugVariable variable = new JTL.resource.jtl.debug.JtlDebugVariable(debugTarget, variableName, value, variableRefType);
			variables[i++] = variable;
		}
		return variables;
	}
	
	private void sendCommand(JTL.resource.jtl.debug.EJtlDebugMessageTypes command, String... parameters) {
		JTL.resource.jtl.debug.JtlDebugMessage message = new JTL.resource.jtl.debug.JtlDebugMessage(command, parameters);
		communicationHelper.sendEvent(message, output);
	}
	
	private JTL.resource.jtl.debug.JtlDebugMessage sendCommandAndRead(JTL.resource.jtl.debug.EJtlDebugMessageTypes command, String... parameters) {
		JTL.resource.jtl.debug.JtlDebugMessage message = new JTL.resource.jtl.debug.JtlDebugMessage(command, parameters);
		return communicationHelper.sendAndReceive(message, output, reader);
	}
	
}
