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) {
102 if((b= fDebugReader.readByte())!=0) // reads the NULL Byte at the end;
103 System.out.println("NULL-Byte missing!!");
104 } catch (IOException e) {
105 if (e instanceof EOFException == false) {
112 return new String(byteBuffer);
115 private /*XDebugResponse*/ int sendRequest(String command, String arguments) {
118 id = _sendRequest(command, arguments);
120 //XDebugResponse response = getResponse(id);
122 return /*response*/ id;
125 private synchronized int _sendRequest(String command, String arguments) {
126 XDebugCorePlugin.log(IStatus.INFO,command+" -i "+fTransactionID+" "+arguments);
127 synchronized (fDebugSocket) {
129 fDebugWriter.write(command);
130 fDebugWriter.write(" -i " + fTransactionID);
131 if (!"".equals(arguments))
132 fDebugWriter.write(" " + arguments);
133 fDebugWriter.write(0);
134 fDebugWriter.flush();
135 } catch (IOException e) {
140 return fTransactionID++;
143 public /*XDebugResponse*/ int eval(String Expression) {
144 String encoded = Base64.encodeBytes(Expression.getBytes());
146 return sendRequest("eval", "-- "+encoded);
149 public /*XDebugResponse*/ int featureGet(String featureName) {
150 return sendRequest("feature_get","-n "+featureName);
153 public /*boolean*/ int featureSet(String featureName, String value) {
154 //XDebugResponse id = sendRequest("feature_set","-n "+featureName + " -v " + value);
156 int id = sendRequest("feature_set","-n "+featureName + " -v " + value);
159 /*XDebugResponse response = getResponse(id);
161 if (response.getAttributeValue("success").equals("1") )
167 /*protected XDebugResponse getResponse(int id) {
168 return fResponseList.get(id);
171 protected void addResponse(XDebugResponse response, int id) {
172 fResponseList.add(response, id);
175 public /*XDebugResponse*/ int breakpointSetOld(String file, int lineNumber) {
178 arg = "-t line -f file://"+PHPDebugUtils.escapeString(file)+" -n " + lineNumber;
179 return sendRequest("breakpoint_set", arg);
182 public /*XDebugResponse*/ int breakpointSet(String file, int lineNumber, int hitCount) {
185 arg = "-t line -f file://"+PHPDebugUtils.escapeString(file)+" -n " + lineNumber;
187 arg += " -h " + hitCount;
189 return sendRequest("breakpoint_set", arg);
192 public int breakpointGet(int id) {
196 return sendRequest("breakpoint_get", arg);
199 public /*XDebugResponse*/ int breakpointRemove(int id) {
200 return sendRequest("breakpoint_set", "-d " + id);
203 public /*XDebugResponse*/ int stackGet(/*int Level*/) {
205 return sendRequest("stack_get", "-d " + Level);
207 return sendRequest("stack_get", "");
211 public void stepOver() {
212 sendRequest("step_over", "");
215 public void stepInto() {
216 sendRequest("step_into", "");
219 public void stepOut() {
220 sendRequest("step_out", "");
224 sendRequest("run", "");
228 sendRequest("stop", "");
231 public /*XDebugResponse*/ int propertySet(String Name, String Value) {
232 String str = Base64.encodeBytes(Value.getBytes());
233 int len = str.length();
235 return sendRequest("property_set", "-n " + Name + " -l " + len + " -- " + str);
238 public /*XDebugResponse*/ int contextGet(int Level, int Type) {
239 return sendRequest("context_get", "-d " + Level + " -c " + Type);
242 public /*boolean*/int setVarValue(String Name, String Value) {
243 //XDebugResponse dr = propertySet(Name, Value);
245 int id = propertySet(Name, Value);
246 //XDebugResponse response = getResponse(id);
249 /*if ((response.getAttributeValue("success")).equals("1"))
255 /*public void startListener() {
256 fResponseListener.schedule();
259 public boolean stopListener() {
260 return fResponseListener.cancel();
263 public void close() {
266 //fResponseListener.cancel();
267 //fResponseListener = null;
269 fDebugSocket.close();
270 fDebugReader.close();
272 fDebugWriter.close();
273 } catch (IOException e) {
277 //fResponseListener.cancel();