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;
22 private Socket fDebugSocket;
23 private OutputStreamWriter fDebugWriter;
24 private DataInputStream fDebugReader;
25 private boolean fInitialized;
26 private boolean fIsClosed;
27 private String fSessionID;
29 public String getSessionID() {
33 public boolean isInitialized() {
37 public boolean isClosed() {
41 public XDebugConnection(Socket debugSocket) {
42 fDebugSocket = debugSocket;
46 fDebugWriter = new OutputStreamWriter(debugSocket.getOutputStream(), "UTF8");
47 fDebugReader = new DataInputStream(debugSocket.getInputStream());
48 } catch (UnsupportedEncodingException e) {
50 } catch (IOException e) {
56 String initString = readData();
57 XDebugCorePlugin.log(IStatus.INFO,initString);
59 int startIdx = initString.indexOf("idekey=\"");
63 int endIdx=initString.indexOf('"',startIdx);
66 fSessionID = initString.substring(startIdx,endIdx);
70 protected String readData() {
74 byte byteBuffer[]=null,b;
78 while ((b =fDebugReader.readByte()) != 0) {
79 count = count * 10 + b - '0';
81 byteBuffer = new byte[count];
84 while ((count >0) && (attempts <5)) {
85 int rc=fDebugReader.read(byteBuffer,readCount,count);
91 } catch (InterruptedException e) {
97 fDebugReader.readFully(byteBuffer,readCount,count);
99 if((b= fDebugReader.readByte())!=0) // reads the NULL Byte at the end;
100 System.out.println("NULL-Byte missing!!");
101 } catch (IOException e) {
102 if (e instanceof EOFException == false) {
109 return new String(byteBuffer);
112 private /*XDebugResponse*/ int sendRequest(String command, String arguments) {
113 return _sendRequest(command, arguments);
116 private synchronized int _sendRequest(String command, String arguments) {
117 if (fDebugSocket == null) {
121 XDebugCorePlugin.log(IStatus.INFO,command+" -i "+fTransactionID+" "+arguments);
122 synchronized (fDebugSocket) {
124 fDebugWriter.write(command);
125 fDebugWriter.write(" -i " + fTransactionID);
126 if (!"".equals(arguments))
127 fDebugWriter.write(" " + arguments);
128 fDebugWriter.write(0);
129 fDebugWriter.flush();
130 } catch (IOException e) {
135 return fTransactionID++;
138 public /*XDebugResponse*/ int eval(String Expression) {
139 String encoded = Base64.encodeBytes(Expression.getBytes());
141 return sendRequest("eval", "-- "+encoded);
144 public /*XDebugResponse*/ int featureGet(String featureName) {
145 return sendRequest("feature_get","-n "+featureName);
148 public int featureSet(String featureName, String value) {
149 return sendRequest("feature_set","-n "+featureName + " -v " + value);
152 public /*XDebugResponse*/ int breakpointSetOld(String file, int lineNumber) {
153 String arg = "-t line -f file://"+PHPDebugUtils.escapeString(file)+" -n " + lineNumber;
154 return sendRequest("breakpoint_set", arg);
157 public /*XDebugResponse*/ int breakpointSet(String file, int lineNumber, int hitCount) {
158 String arg = "-t line -f file://"+PHPDebugUtils.escapeString(file)+" -n " + lineNumber;
160 arg += " -h " + hitCount;
162 return sendRequest("breakpoint_set", arg);
165 public int breakpointGet(int id) {
166 String arg = "-d " + id;
168 return sendRequest("breakpoint_get", arg);
171 public /*XDebugResponse*/ int breakpointRemove(int id) {
172 return sendRequest("breakpoint_set", "-d " + id);
175 public /*XDebugResponse*/ int stackGet(/*int Level*/) {
176 return sendRequest("stack_get", "");
179 public void stepOver() {
180 sendRequest("step_over", "");
183 public void stepInto() {
184 sendRequest("step_into", "");
187 public void stepOut() {
188 sendRequest("step_out", "");
192 sendRequest("run", "");
196 sendRequest("stop", "");
199 public /*XDebugResponse*/ int propertySet(String Name, String Value) {
200 String str = Base64.encodeBytes(Value.getBytes());
201 int len = str.length();
203 return sendRequest("property_set", "-n " + Name + " -d 0 -l " + len + " -- " + str);
206 public /*XDebugResponse*/ int contextGet(int Level, int Type) {
207 return sendRequest("context_get", "-d " + Level + " -c " + Type);
210 public int setVarValue(String Name, String Value) {
211 return propertySet(Name, Value);
214 public void close() {
218 fDebugSocket.close();
219 fDebugReader.close();
220 //fDebugReader = null;
221 fDebugWriter.close();
222 } catch (IOException e) {