Merge xdebug from 1.3.x.
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / core / xdebug / XDebugConnection.java
index 415a808..24fedd1 100644 (file)
@@ -7,12 +7,14 @@ import java.io.DataInputStream;
 import java.io.EOFException;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
 import java.net.Socket;
 
 import net.sourceforge.phpeclipse.xdebug.core.Base64;
 import net.sourceforge.phpeclipse.xdebug.core.PHPDebugUtils;
 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
-import net.sourceforge.phpeclipse.xdebug.core.xdebug.ResponseListener.DebugResponse;
+//import net.sourceforge.phpeclipse.xdebug.core.xdebug.ResponseListener.XDebugResponse;
+
 import org.eclipse.core.runtime.IStatus;
 
 /**
@@ -20,12 +22,10 @@ import org.eclipse.core.runtime.IStatus;
  *
  */
 public class XDebugConnection {
-       int fTransactionID = 0;
+       private int fTransactionID = 0;
        private Socket fDebugSocket;
        private OutputStreamWriter fDebugWriter;
        private DataInputStream fDebugReader;
-       private ResponseList fResponseList;
-       private ResponseListener fResponseListener;
 
        protected boolean fInitialized = false;
        protected boolean fIsClosed = true;
@@ -44,13 +44,21 @@ public class XDebugConnection {
                return fIsClosed;
        }
 
-       public XDebugConnection(Socket debugSocket, DataInputStream reader, OutputStreamWriter writer) {
-               fResponseList = new ResponseList();
-               fDebugWriter = writer;
-               fDebugReader = reader;
+       public XDebugConnection(Socket debugSocket) {
                fDebugSocket = debugSocket;
                fTransactionID = 0;
                fInitialized = false;
+               try {
+                       fDebugWriter = new OutputStreamWriter(debugSocket.getOutputStream(), "UTF8");
+                       fDebugReader = new DataInputStream(debugSocket.getInputStream()); 
+               } catch (UnsupportedEncodingException e) {
+                       e.printStackTrace();
+               } catch (IOException e) {
+                       e.printStackTrace();
+               }
+
+               fIsClosed = false;
+
                String initString = readData();
                XDebugCorePlugin.log(IStatus.INFO,initString);
 
@@ -62,20 +70,19 @@ public class XDebugConnection {
                if (endIdx==-1)
                        return;
                fSessionID = initString.substring(startIdx,endIdx);
-               
                fInitialized = true;
-               fIsClosed = false;
-               fResponseListener = new ResponseListener(this);
        }
        
        protected String readData()     {
+               if (fIsClosed)
+                       return null;
+               
         byte byteBuffer[]=null,b;
                int count=0;
                
                try {
                        while ((b =fDebugReader.readByte()) != 0) {
                                count = count * 10 + b - '0';
-//                             count=count*10+Integer.parseInt(b);
                        }
                        byteBuffer = new byte[count];
                        int readCount=0;
@@ -95,22 +102,24 @@ public class XDebugConnection {
                        if((b= fDebugReader.readByte())!=0) // reads the NULL Byte at the end;
                                System.out.println("NULL-Byte missing!!"); 
                } catch (IOException e) {
-                       // TODO Auto-generated catch block
-                       if (e instanceof EOFException==false)
-                               e.printStackTrace();
+                       if (e instanceof EOFException == false) {
+                               if (!fIsClosed) {
+                                       e.printStackTrace();
+                               }
+                       }
                        return null;
                }
                return new String(byteBuffer);
        }
        
-       public DebugResponse sendRequest(String command, String arguments) {
+       private /*XDebugResponse*/ int sendRequest(String command, String arguments) {
                int id = -1;
                
                id = _sendRequest(command, arguments);
 
-               DebugResponse response = getResponse(id);
+               //XDebugResponse response = getResponse(id);
 
-               return response;
+               return /*response*/ id;
        }
 
        private synchronized int _sendRequest(String command, String arguments) {
@@ -131,46 +140,67 @@ public class XDebugConnection {
                return fTransactionID++;
        }
 
-       public DebugResponse eval(String Expression) {
+       public /*XDebugResponse*/ int eval(String Expression) {
                String encoded = Base64.encodeBytes(Expression.getBytes());
                
                return sendRequest("eval", "-- "+encoded);
        }
 
-       public DebugResponse featureGet(String featureName) {
+       public /*XDebugResponse*/ int featureGet(String featureName) {
                return sendRequest("feature_get","-n "+featureName);
        }
 
-       public boolean featureSet(String featureName, String value) {
-               DebugResponse id = sendRequest("feature_set","-n "+featureName + " -v " + value);
+       public /*boolean*/ int  featureSet(String featureName, String value) {
+               //XDebugResponse id = sendRequest("feature_set","-n "+featureName + " -v " + value);
+
+               int id = sendRequest("feature_set","-n "+featureName + " -v " + value);
+               
+               return id;
+               /*XDebugResponse response = getResponse(id);
 
-               if (id.getAttributeValue("success").equals("1") )
+               if (response.getAttributeValue("success").equals("1") )
                        return true;
                else
-                       return false;
+                       return false;*/
        }
 
-       protected DebugResponse getResponse(int id) {
+       /*protected XDebugResponse getResponse(int id) {
                return fResponseList.get(id);
        }
 
-       protected void addResponse(DebugResponse response, int id) {
+       protected void addResponse(XDebugResponse response, int id) {
                fResponseList.add(response, id);
-       }
+       }*/
        
+       public /*XDebugResponse*/ int  breakpointSetOld(String file, int lineNumber) {
+               String arg;
+               
+               arg = "-t line -f file://"+PHPDebugUtils.escapeString(file)+" -n " + lineNumber;
+               return sendRequest("breakpoint_set", arg);              
+       }
        
-       public DebugResponse breakpointSet(String file, int lineNumber) {
+       public /*XDebugResponse*/ int  breakpointSet(String file, int lineNumber, int hitCount) {
                String arg;
                
                arg = "-t line -f file://"+PHPDebugUtils.escapeString(file)+" -n " + lineNumber;
+               if (hitCount > 0) {
+                       arg += " -h " + hitCount;       
+               }
                return sendRequest("breakpoint_set", arg);              
        }
        
-       public DebugResponse breakpointRemove(int id) {
+       public int  breakpointGet(int id) {
+               String arg;
+               
+               arg = "-d " + id;
+               return sendRequest("breakpoint_get", arg);              
+       }
+       
+       public /*XDebugResponse*/ int  breakpointRemove(int id) {
                return sendRequest("breakpoint_set", "-d " + id);
        }
 
-       public DebugResponse stackGet(int Level) {
+       public /*XDebugResponse*/ int  stackGet(/*int Level*/) {
                /*if (Level > -1) {
                        return sendRequest("stack_get", "-d " + Level);
                } else {*/
@@ -198,42 +228,53 @@ public class XDebugConnection {
                sendRequest("stop", "");
        }
 
-       public void startListener() {
-               fResponseListener.schedule();
-       }
-       
-       public DebugResponse propertySet(String Name, String Value) {
+       public /*XDebugResponse*/ int  propertySet(String Name, String Value) {
                String str = Base64.encodeBytes(Value.getBytes());
                int len = str.length();
 
                return sendRequest("property_set", "-n " + Name + " -l " + len + " -- " + str);
        }
 
-       public boolean setVarValue(String Name, String Value) {
-               DebugResponse dr = propertySet(Name, Value);
+       public /*XDebugResponse*/ int  contextGet(int Level, int Type) {
+               return sendRequest("context_get", "-d " + Level + " -c " + Type);
+       }
 
-               if ((dr.getAttributeValue("success")).equals("1"))
+       public /*boolean*/int setVarValue(String Name, String Value) {
+               //XDebugResponse dr = propertySet(Name, Value);
+
+               int id = propertySet(Name, Value);
+               //XDebugResponse response = getResponse(id);
+               return id;
+               
+               /*if ((response.getAttributeValue("success")).equals("1"))
                        return true;
                
-               return false;
+               return false;*/
        }
        
-       public void close() {
-               fIsClosed = true;
-               //fResponseListener.cancel();
-               //fResponseListener = null;
-               try {
-                       fDebugReader.close();           
-                       fDebugWriter.close();
-                       fDebugSocket.close();
-               } catch (IOException e) {
-                       e.printStackTrace();
-               }
-               fResponseListener.cancel();
-               fIsClosed=true;
+       /*public void startListener() {
+               fResponseListener.schedule();
        }
        
-       public DebugResponse contextGet(int Level, int Type) {
-               return sendRequest("context_get", "-d " + Level + " -c " + Type);
-       }
+       public boolean stopListener() {
+               return fResponseListener.cancel();
+       }*/
+       
+       public void close() {
+               if (!fIsClosed) {
+                       fIsClosed = true;
+                       //fResponseListener.cancel();
+                       //fResponseListener = null;
+                       try {
+                               fDebugSocket.close();
+                               fDebugReader.close();
+                               fDebugReader = null;
+                               fDebugWriter.close();
+                       } catch (IOException e) {
+                               e.printStackTrace();
+                       }
+               }
+               //fResponseListener.cancel();
+               //fIsClosed=true;
+       }       
 }
\ No newline at end of file