Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8453a92f07dabdcbc8b9e82acb659f201711e263 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.core.tests.runtime;

import org.eclipse.jdt.internal.compiler.ClassFile;
import org.eclipse.jdt.internal.compiler.util.Util;

import java.io.*;
import java.net.*;
/**
 * This is the interface to the target VM. It connects to an IDEInterface on the target side
 * using TCP/IO to send request for code snippet evaluation and to get the result back.
 *
 * @see org.eclipse.jdt.core.tests.eval.target.IDEInterface for details about the protocol.
 */
public class TargetInterface {
	/**
	 * Whether class files should be written in d:\eval\ instead of sending them to the target
	 * NB: d:\eval should contain a batch file TestCodeSnippet.bat with the following contents:
	 *		SET JDK=c:\jdk1.2.2
	 *		SET EVAL=d:\eval
	 *		%JDK%\bin\java -Xbootclasspath:%JDK%\jre\lib\rt.jar;%EVAL%\javaClasses; -classpath c:\temp;%EVAL%\snippets;%EVAL%\classes;"d:\ide\project_resources\Eclipse Java Evaluation\CodeSnippetSupport.jar" CodeSnippetTester %1
	 */
	static final boolean DEBUG = false;
	String codeSnippetClassName;

	/**
	 * Whether timing info should be printed to stdout
	 */
	static final boolean TIMING = false;
	long sentTime;

	/**
	 * The connection to the target's ide interface.
	 */
	Socket socket;

	public class Result {
		public char[] displayString;
		public char[] typeName;
	}

/**
 * (PRIVATE API)
 * Connects this interface to the target.
 * Try as long as the given time (in ms) has not expired.
 * Use isConnected() to find out if the connection was successful.
 */
public void connect(ServerSocket server, int timeout) {
	if (isConnected()) {
		return;
	}
	if (server != null) {
		long startTime = System.currentTimeMillis();
		do {
			try {
				this.socket = server.accept();
				this.socket.setTcpNoDelay(true);
				break;
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (this.socket == null) {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
				}
			}
		} while (!isConnected() && (System.currentTimeMillis() - startTime) < timeout);
	}
}
/**
 * (PRIVATE API)
 * Disconnects this interface from the target.
 */
public void disconnect() {
	if (this.socket != null) {
		try {
			this.socket.close();
		} catch (IOException e) {
			e.printStackTrace();
			// Already closed. Nothing more to do
		}
		this.socket = null;
	}
}
/**
 * Returns the result of the evaluation sent previously to the target.
 */
public Result getResult() {
	boolean hasValue = false;
	String typeName = null;
	String toString = null;
	if (DEBUG) {
		hasValue = true;
		typeName = "TargetInterface in debug mode. Run d:\\eval\\TestCodeSnippet.bat d:\\eval\\snippets\\" + this.codeSnippetClassName;
		toString = "";
	} else {
		if (isConnected()) {
			// TBD: Read type name and toString as a character array
			try {
				DataInputStream in = new DataInputStream(this.socket.getInputStream());
				hasValue = in.readBoolean();
				if (hasValue) {
					typeName = in.readUTF();
					toString = in.readUTF();
				} else {
					typeName = null;
					toString = null;
				}
			} catch (IOException e) {
				// The socket has likely been closed on the other end. So the code snippet runner has stopped.
				hasValue = true;
				typeName = e.getMessage();
				toString = "";
				disconnect();
			}
		} else {
			hasValue = true;
			typeName = "Connection has been lost";
			toString = "";
		}
	}
	if (TIMING) {
		System.out.println("Time to send compiled classes, run on target and get result is " + (System.currentTimeMillis() - this.sentTime) + "ms");
	}
	Result result = new Result();
	result.displayString = toString == null ? null : toString.toCharArray();
	result.typeName = typeName == null ? null : typeName.toCharArray();
	return result;
}
/**
 * Returns whether this interface is connected to the target.
 */
public boolean isConnected() {
	return this.socket != null;
}
/**
 * Sends the given class definitions to the target for loading and (if specified) for running.
 */
public void sendClasses(boolean mustRun, ClassFile[] classes) throws TargetException {
	if (DEBUG) {
		for (int i = 0; i < classes.length; i++) {
			String className = new String(classes[i].fileName()).replace('/', '\\') + ".class";
			if ((i == 0) && (className.indexOf("CodeSnippet") != -1)) {
				this.codeSnippetClassName = className;
				try {
					Util.writeToDisk(true, "d:\\eval\\snippets", className, classes[0]);
				} catch(IOException e) {
					e.printStackTrace();
				}
			} else {
				String dirName;
				if (className.startsWith("java")) {
					dirName = "d:\\eval\\" + LocalVMLauncher.BOOT_CLASSPATH_DIRECTORY;
				} else {
					dirName = "d:\\eval\\" + LocalVMLauncher.REGULAR_CLASSPATH_DIRECTORY;
				}
				try {
					Util.writeToDisk(true, dirName, className, classes[i]);
				} catch(IOException e) {
					e.printStackTrace();
				}
			}
		}
	} else {
		if (TIMING) {
			this.sentTime = System.currentTimeMillis();
		}
		if (!isConnected()) {
			throw new TargetException("Connection to the target VM has been lost");
		}
		try {
			DataOutputStream out = new DataOutputStream(this.socket.getOutputStream());
			out.writeBoolean(mustRun);
			out.writeInt(classes.length);
			for (int i = 0; i < classes.length; i++) {
				byte[] classDefinition = classes[i].getBytes();
				out.writeInt(classDefinition.length);
				out.write(classDefinition);
			}
		} catch (IOException e) {
			e.printStackTrace();
			// The socket has likely been closed on the other end. So the code snippet runner has stopped.
			disconnect();
		}
	}
}
}

Back to the top