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.net.Socket;
12 import net.sourceforge.phpeclipse.xdebug.core.Base64;
13 import net.sourceforge.phpeclipse.xdebug.core.PHPDebugUtils;
14 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
15 import net.sourceforge.phpeclipse.xdebug.core.xdebug.ResponseListener.DebugResponse;
16 import org.eclipse.core.runtime.IStatus;
19 * @author Christian Perkonig
22 public class XDebugConnection {
23 int fTransactionID = 0;
24 private Socket fDebugSocket;
25 private OutputStreamWriter fDebugWriter;
26 private DataInputStream fDebugReader;
27 private ResponseList fResponseList;
28 private ResponseListener fResponseListener;
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, DataInputStream reader, OutputStreamWriter writer) {
48 fResponseList = new ResponseList();
49 fDebugWriter = writer;
50 fDebugReader = reader;
51 fDebugSocket = debugSocket;
54 String initString = readData();
55 XDebugCorePlugin.log(IStatus.INFO,initString);
57 int startIdx = initString.indexOf("idekey=\"");
61 int endIdx=initString.indexOf('"',startIdx);
64 fSessionID = initString.substring(startIdx,endIdx);
68 fResponseListener = new ResponseListener(this);
71 protected String readData() {
72 byte byteBuffer[]=null,b;
76 while ((b =fDebugReader.readByte()) != 0) {
77 count = count * 10 + b - '0';
78 // count=count*10+Integer.parseInt(b);
80 byteBuffer = new byte[count];
83 while ((count >0) && (attempts <5)) {
84 int rc=fDebugReader.read(byteBuffer,readCount,count);
90 } catch (InterruptedException e) {
95 if((b= fDebugReader.readByte())!=0) // reads the NULL Byte at the end;
96 System.out.println("NULL-Byte missing!!");
97 } catch (IOException e) {
98 // TODO Auto-generated catch block
99 if (e instanceof EOFException==false)
103 return new String(byteBuffer);
106 public DebugResponse sendRequest(String command, String arguments) {
109 id = _sendRequest(command, arguments);
111 DebugResponse response = getResponse(id);
116 private synchronized int _sendRequest(String command, String arguments) {
117 XDebugCorePlugin.log(IStatus.INFO,command+" -i "+fTransactionID+" "+arguments);
118 synchronized (fDebugSocket) {
120 fDebugWriter.write(command);
121 fDebugWriter.write(" -i " + fTransactionID);
122 if (!"".equals(arguments))
123 fDebugWriter.write(" " + arguments);
124 fDebugWriter.write(0);
125 fDebugWriter.flush();
126 } catch (IOException e) {
131 return fTransactionID++;
134 public DebugResponse eval(String Expression) {
135 String encoded = Base64.encodeBytes(Expression.getBytes());
137 return sendRequest("eval", "-- "+encoded);
140 public DebugResponse featureGet(String featureName) {
141 return sendRequest("feature_get","-n "+featureName);
144 public boolean featureSet(String featureName, String value) {
145 DebugResponse id = sendRequest("feature_set","-n "+featureName + " -v " + value);
147 if (id.getAttributeValue("success").equals("1") )
153 protected DebugResponse getResponse(int id) {
154 return fResponseList.get(id);
157 protected void addResponse(DebugResponse response, int id) {
158 fResponseList.add(response, id);
162 public DebugResponse breakpointSet(String file, int lineNumber) {
165 arg = "-t line -f file://"+PHPDebugUtils.escapeString(file)+" -n " + lineNumber;
166 return sendRequest("breakpoint_set", arg);
169 public DebugResponse breakpointRemove(int id) {
170 return sendRequest("breakpoint_set", "-d " + id);
173 public DebugResponse stackGet(int Level) {
175 return sendRequest("stack_get", "-d " + Level);
177 return sendRequest("stack_get", "");
181 public void stepOver() {
182 sendRequest("step_over", "");
185 public void stepInto() {
186 sendRequest("step_into", "");
189 public void stepOut() {
190 sendRequest("step_out", "");
194 sendRequest("run", "");
198 sendRequest("stop", "");
201 public void startListener() {
202 fResponseListener.schedule();
205 public DebugResponse propertySet(String Name, String Value) {
206 String str = Base64.encodeBytes(Value.getBytes());
207 int len = str.length();
209 return sendRequest("property_set", "-n " + Name + " -l " + len + " -- " + str);
212 public boolean setVarValue(String Name, String Value) {
213 DebugResponse dr = propertySet(Name, Value);
215 if ((dr.getAttributeValue("success")).equals("1"))
221 public void close() {
223 //fResponseListener.cancel();
224 //fResponseListener = null;
226 fDebugReader.close();
227 fDebugWriter.close();
228 fDebugSocket.close();
229 } catch (IOException e) {
232 fResponseListener.cancel();
236 public DebugResponse contextGet(int Level, int Type) {
237 return sendRequest("context_get", "-d " + Level + " -c " + Type);