4 package net.sourceforge.phpeclipse.xdebug.core.xdebug;
6 import java.io.DataInputStream;
7 import java.io.EOFException;
8 import java.io.IOException;
9 import java.io.OutputStreamWriter;
10 import java.io.UnsupportedEncodingException;
11 import java.net.Socket;
13 import net.sourceforge.phpeclipse.xdebug.core.Base64;
14 import net.sourceforge.phpeclipse.xdebug.core.PHPDebugUtils;
15 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
16 //import net.sourceforge.phpeclipse.xdebug.core.xdebug.ResponseListener.XDebugResponse;
18 import org.eclipse.core.runtime.IStatus;
21 * @author Christian Perkonig
24 public class XDebugConnection {
25 private int fTransactionID = 0;
26 private Socket fDebugSocket;
27 private OutputStreamWriter fDebugWriter;
28 private DataInputStream fDebugReader;
30 protected boolean fInitialized = false;
31 protected boolean fIsClosed = true;
33 protected String fSessionID = "";
35 public String getSessionID() {
39 public boolean isInitialized() {
43 public boolean isClosed() {
47 public XDebugConnection(Socket debugSocket) {
48 fDebugSocket = debugSocket;
52 fDebugWriter = new OutputStreamWriter(debugSocket.getOutputStream(), "UTF8");
53 fDebugReader = new DataInputStream(debugSocket.getInputStream());
54 } catch (UnsupportedEncodingException e) {
56 } catch (IOException e) {
62 String initString = readData();
63 XDebugCorePlugin.log(IStatus.INFO,initString);
65 int startIdx = initString.indexOf("idekey=\"");
69 int endIdx=initString.indexOf('"',startIdx);
72 fSessionID = initString.substring(startIdx,endIdx);
76 protected String readData() {
80 byte byteBuffer[]=null,b;
84 while ((b =fDebugReader.readByte()) != 0) {
85 count = count * 10 + b - '0';
87 byteBuffer = new byte[count];
90 while ((count >0) && (attempts <5)) {
91 int rc=fDebugReader.read(byteBuffer,readCount,count);
97 } catch (InterruptedException e) {
103 fDebugReader.readFully(byteBuffer,readCount,count);
105 if((b= fDebugReader.readByte())!=0) // reads the NULL Byte at the end;
106 System.out.println("NULL-Byte missing!!");
107 } catch (IOException e) {
108 if (e instanceof EOFException == false) {
115 return new String(byteBuffer);
118 private /*XDebugResponse*/ int sendRequest(String command, String arguments) {
121 id = _sendRequest(command, arguments);
123 //XDebugResponse response = getResponse(id);
125 return /*response*/ id;
128 private synchronized int _sendRequest(String command, String arguments) {
129 XDebugCorePlugin.log(IStatus.INFO,command+" -i "+fTransactionID+" "+arguments);
130 synchronized (fDebugSocket) {
132 fDebugWriter.write(command);
133 fDebugWriter.write(" -i " + fTransactionID);
134 if (!"".equals(arguments))
135 fDebugWriter.write(" " + arguments);
136 fDebugWriter.write(0);
137 fDebugWriter.flush();
138 } catch (IOException e) {
143 return fTransactionID++;
146 public /*XDebugResponse*/ int eval(String Expression) {
147 String encoded = Base64.encodeBytes(Expression.getBytes());
149 return sendRequest("eval", "-- "+encoded);
152 public /*XDebugResponse*/ int featureGet(String featureName) {
153 return sendRequest("feature_get","-n "+featureName);
156 public /*boolean*/ int featureSet(String featureName, String value) {
157 //XDebugResponse id = sendRequest("feature_set","-n "+featureName + " -v " + value);
159 int id = sendRequest("feature_set","-n "+featureName + " -v " + value);
162 /*XDebugResponse response = getResponse(id);
164 if (response.getAttributeValue("success").equals("1") )
170 /*protected XDebugResponse getResponse(int id) {
171 return fResponseList.get(id);
174 protected void addResponse(XDebugResponse response, int id) {
175 fResponseList.add(response, id);
178 public /*XDebugResponse*/ int breakpointSetOld(String file, int lineNumber) {
181 arg = "-t line -f file://"+PHPDebugUtils.escapeString(file)+" -n " + lineNumber;
182 return sendRequest("breakpoint_set", arg);
185 public /*XDebugResponse*/ int breakpointSet(String file, int lineNumber, int hitCount) {
188 arg = "-t line -f file://"+PHPDebugUtils.escapeString(file)+" -n " + lineNumber;
190 arg += " -h " + hitCount;
192 return sendRequest("breakpoint_set", arg);
195 public int breakpointGet(int id) {
199 return sendRequest("breakpoint_get", arg);
202 public /*XDebugResponse*/ int breakpointRemove(int id) {
203 return sendRequest("breakpoint_set", "-d " + id);
206 public /*XDebugResponse*/ int stackGet(/*int Level*/) {
208 return sendRequest("stack_get", "-d " + Level);
210 return sendRequest("stack_get", "");
214 public void stepOver() {
215 sendRequest("step_over", "");
218 public void stepInto() {
219 sendRequest("step_into", "");
222 public void stepOut() {
223 sendRequest("step_out", "");
227 sendRequest("run", "");
231 sendRequest("stop", "");
234 public /*XDebugResponse*/ int propertySet(String Name, String Value) {
235 String str = Base64.encodeBytes(Value.getBytes());
236 int len = str.length();
238 return sendRequest("property_set", "-n " + Name + " -l " + len + " -- " + str);
241 public /*XDebugResponse*/ int contextGet(int Level, int Type) {
242 return sendRequest("context_get", "-d " + Level + " -c " + Type);
245 public /*boolean*/int setVarValue(String Name, String Value) {
246 //XDebugResponse dr = propertySet(Name, Value);
248 int id = propertySet(Name, Value);
249 //XDebugResponse response = getResponse(id);
252 /*if ((response.getAttributeValue("success")).equals("1"))
258 /*public void startListener() {
259 fResponseListener.schedule();
262 public boolean stopListener() {
263 return fResponseListener.cancel();
266 public void close() {
269 //fResponseListener.cancel();
270 //fResponseListener = null;
272 fDebugSocket.close();
273 fDebugReader.close();
275 fDebugWriter.close();
276 } catch (IOException e) {
280 //fResponseListener.cancel();