1 package net.sourceforge.phpeclipse.xdebug.core.xdebug;
3 import java.io.DataInputStream;
4 import java.io.EOFException;
5 import java.io.IOException;
6 import java.io.OutputStreamWriter;
7 import java.io.UnsupportedEncodingException;
8 import java.net.Socket;
10 import net.sourceforge.phpeclipse.xdebug.core.Base64;
11 import net.sourceforge.phpeclipse.xdebug.core.PHPDebugUtils;
12 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
14 import org.eclipse.core.runtime.IStatus;
17 * @author Christian Perkonig
20 public class XDebugConnection {
21 private int fTransactionID = 0;
22 private Socket fDebugSocket;
23 private OutputStreamWriter fDebugWriter;
24 private DataInputStream fDebugReader;
26 protected boolean fInitialized = false;
27 protected boolean fIsClosed = true;
29 protected String fSessionID = "";
31 public String getSessionID() {
35 public boolean isInitialized() {
39 public boolean isClosed() {
43 public XDebugConnection(Socket debugSocket) {
44 fDebugSocket = debugSocket;
48 fDebugWriter = new OutputStreamWriter(debugSocket.getOutputStream(), "UTF8");
49 fDebugReader = new DataInputStream(debugSocket.getInputStream());
50 } catch (UnsupportedEncodingException e) {
52 } catch (IOException e) {
58 String initString = readData();
59 XDebugCorePlugin.log(IStatus.INFO,initString);
61 int startIdx = initString.indexOf("idekey=\"");
65 int endIdx=initString.indexOf('"',startIdx);
68 fSessionID = initString.substring(startIdx,endIdx);
72 protected String readData() {
76 byte byteBuffer[]=null,b;
80 while ((b =fDebugReader.readByte()) != 0) {
81 count = count * 10 + b - '0';
83 byteBuffer = new byte[count];
86 while ((count >0) && (attempts <5)) {
87 int rc=fDebugReader.read(byteBuffer,readCount,count);
93 } catch (InterruptedException e) {
99 fDebugReader.readFully(byteBuffer,readCount,count);
101 if((b= fDebugReader.readByte())!=0) // reads the NULL Byte at the end;
102 System.out.println("NULL-Byte missing!!");
103 } catch (IOException e) {
104 if (e instanceof EOFException == false) {
111 return new String(byteBuffer);
114 private /*XDebugResponse*/ int sendRequest(String command, String arguments) {
115 return _sendRequest(command, arguments);
118 private synchronized int _sendRequest(String command, String arguments) {
119 XDebugCorePlugin.log(IStatus.INFO,command+" -i "+fTransactionID+" "+arguments);
120 synchronized (fDebugSocket) {
122 fDebugWriter.write(command);
123 fDebugWriter.write(" -i " + fTransactionID);
124 if (!"".equals(arguments))
125 fDebugWriter.write(" " + arguments);
126 fDebugWriter.write(0);
127 fDebugWriter.flush();
128 } catch (IOException e) {
133 return fTransactionID++;
136 public /*XDebugResponse*/ int eval(String Expression) {
137 String encoded = Base64.encodeBytes(Expression.getBytes());
139 return sendRequest("eval", "-- "+encoded);
142 public /*XDebugResponse*/ int featureGet(String featureName) {
143 return sendRequest("feature_get","-n "+featureName);
146 public int featureSet(String featureName, String value) {
147 return sendRequest("feature_set","-n "+featureName + " -v " + value);
150 public /*XDebugResponse*/ int breakpointSetOld(String file, int lineNumber) {
151 String arg = "-t line -f file://"+PHPDebugUtils.escapeString(file)+" -n " + lineNumber;
152 return sendRequest("breakpoint_set", arg);
155 public /*XDebugResponse*/ int breakpointSet(String file, int lineNumber, int hitCount) {
156 String arg = "-t line -f file://"+PHPDebugUtils.escapeString(file)+" -n " + lineNumber;
158 arg += " -h " + hitCount;
160 return sendRequest("breakpoint_set", arg);
163 public int breakpointGet(int id) {
164 String arg = "-d " + id;
166 return sendRequest("breakpoint_get", arg);
169 public /*XDebugResponse*/ int breakpointRemove(int id) {
170 return sendRequest("breakpoint_set", "-d " + id);
173 public /*XDebugResponse*/ int stackGet(/*int Level*/) {
174 return sendRequest("stack_get", "");
177 public void stepOver() {
178 sendRequest("step_over", "");
181 public void stepInto() {
182 sendRequest("step_into", "");
185 public void stepOut() {
186 sendRequest("step_out", "");
190 sendRequest("run", "");
194 sendRequest("stop", "");
197 public /*XDebugResponse*/ int propertySet(String Name, String Value) {
198 String str = Base64.encodeBytes(Value.getBytes());
199 int len = str.length();
201 return sendRequest("property_set", "-n " + Name + " -d 0 -l " + len + " -- " + str);
204 public /*XDebugResponse*/ int contextGet(int Level, int Type) {
205 return sendRequest("context_get", "-d " + Level + " -c " + Type);
208 public int setVarValue(String Name, String Value) {
209 return propertySet(Name, Value);
212 public void close() {
216 fDebugSocket.close();
217 fDebugReader.close();
218 //fDebugReader = null;
219 fDebugWriter.close();
220 } catch (IOException e) {