Merge xdebug from 1.3.x.
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / core / XDebugProxy.java
index 93294b2..26183fb 100644 (file)
@@ -1,35 +1,31 @@
 package net.sourceforge.phpeclipse.xdebug.core;
 
-import java.io.DataInputStream;
 import java.io.IOException;
-import java.io.OutputStreamWriter;
-import java.io.UnsupportedEncodingException;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.UnknownHostException;
-
-import net.sourceforge.phpeclipse.xdebug.core.xdebug.XDebugConnection;
-
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.ISafeRunnable;
 import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Platform;
+//import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.Job;
-//import org.eclipse.debug.core.DebugException;
 import org.eclipse.debug.core.IDebugEventFilter;
 import org.eclipse.debug.core.IDebugEventSetListener;
+import org.eclipse.jface.util.SafeRunnable;
 
+import net.sourceforge.phpeclipse.xdebug.core.xdebug.XDebugConnection;
 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget;
 
 public class XDebugProxy {
-       private XDebugTarget fTarget;
-
-       protected String fInitString;
-       protected String fIdeKey;
-
-       /*protected*/ private /*AbstractDebugConnection*/ XDebugConnection fConnection;
-
+       private XDebugConnection fConnection;
+       private ServerSocket fProxyServerSocket;
+       private ProxyListenerJob fProxyListener;
+       private boolean fTerminate;
+       private int fProxyPort;
+       private ListenerMap fEventListeners;
+       private boolean fIsRunning;
+       
        class ProxyListenerJob extends Job {
                public ProxyListenerJob() {
                        super("XDebug Proxy Connection Dispatch");
@@ -42,14 +38,10 @@ public class XDebugProxy {
                protected IStatus run(IProgressMonitor monitor) {
                        boolean error;
                        Socket socket = null;
-                       DataInputStream reader = null;
-                       OutputStreamWriter writer = null;
                        
                        while (!fTerminate) {
                                error = false;
                                socket = null;
-                               reader = null;
-                               writer = null;
                                if (monitor.isCanceled()) return Status.CANCEL_STATUS;
                                try {
                                        socket = fProxyServerSocket.accept();
@@ -59,25 +51,22 @@ public class XDebugProxy {
                                if (!error) {
                                        XDebugCorePlugin.log(IStatus.INFO,"Proxy: someone tries to connect");
                                        
-                                       try {
-                                               writer = new OutputStreamWriter(socket.getOutputStream(), "UTF8");
-                                               reader = new DataInputStream(socket.getInputStream()); 
-                                       } catch (UnsupportedEncodingException e) {
-                                               // TODO Auto-generated catch block
-                                               e.printStackTrace();
-                                       } catch (IOException e) {
-                                               // TODO Auto-generated catch block
-                                               e.printStackTrace();
-                                       }
-                                       fConnection=/*(AbstractDebugConnection)*/ new XDebugConnection(socket,reader,writer);   
+                                       fConnection = new XDebugConnection(socket);     
                                        if (fConnection.isInitialized()) {
-                                               fIdeKey=fConnection.getSessionID();
-                                               XDebugCorePlugin.log(IStatus.INFO,"<init idekey \""+fIdeKey+"\">");
-                                       
-                                               fireProxyEvent();
+                                               String IdeKey = fConnection.getSessionID();
+                                               
+                                               Object a = getEventListener(IdeKey);
+                                               
+                                               if (a instanceof XDebugTarget) {
+                                                       XDebugCorePlugin.log(IStatus.INFO, "<init idekey \"" + IdeKey + "\">");
+                                               
+                                                       fireProxyEvent(IdeKey);
+                                               } else {
+                                                       fConnection.close();
+                                                       fConnection = null;
+                                               }
                                        }
-                               }
-                               
+                               }       
                        }
                        return Status.OK_STATUS;
                }
@@ -103,7 +92,7 @@ public class XDebugProxy {
                 * @see org.eclipse.core.runtime.ISafeRunnable#run()
                 */
                public void run() throws Exception {
-                       fListener.handleProxyEvent(fIdeKey, fInitString, fConnection);
+                       fListener.handleProxyEvent(/*fIdeKey,*/ fConnection);
                }
                
                /**
@@ -112,58 +101,34 @@ public class XDebugProxy {
                 * 
                 * @param events debug events
                 */
-               public void dispatch() {
-                       fListener = (IProxyEventListener) getEventListener(fIdeKey);
-                       if (fListener == null) {     // no listener is found so start the script
-                               //try {
-                                       fConnection.run();
-                               /*} catch (DebugException e) {
-                                       // TODO Auto-generated catch block
-                                       e.printStackTrace();
-                               }*/
-                       } else
-                               Platform.run(this);
+               public void dispatch(String id) {
+                       fListener = (IProxyEventListener) getEventListener(id/*fIdeKey*/);
+                       SafeRunnable.run(this);
+                       //Platform.run(this);
                        fListener = null;
                }
 
        }
 
-
-       private ServerSocket fProxyServerSocket;
-       private ProxyListenerJob fProxyListener;
-       private boolean fTerminate;
-       private int fProxyPort;
-       private ListenerMap fEventListeners;
-       private boolean fIsRunning;
-       
-       
-       public XDebugProxy (int port) {
-               fProxyPort=port;
-       }
-       
-       public void setTarget( XDebugTarget Target ) {
-               fTarget = Target;
-       }
-       
-       public XDebugTarget getTarget() {
-               return fTarget;
+       public XDebugProxy(int port) {
+               fProxyPort = port;
        }
        
        public void start() {
-               if (fIsRunning)
-                       return;
-               try {
-                       fProxyServerSocket = new ServerSocket(fProxyPort);
-                       XDebugCorePlugin.log(IStatus.INFO,"Proxy listens on port "+fProxyPort);
-               } catch (UnknownHostException e) {
-                       e.printStackTrace();
-               } catch (IOException e) {
-                       e.printStackTrace();
+               if (!fIsRunning) {
+                       try {
+                               fProxyServerSocket = new ServerSocket(fProxyPort);
+                               XDebugCorePlugin.log(IStatus.INFO,"Proxy listens on port "+fProxyPort);
+                       } catch (UnknownHostException e) {
+                               e.printStackTrace();
+                       } catch (IOException e) {
+                               e.printStackTrace();
+                       }
+                       fTerminate = false;
+                       fProxyListener = new ProxyListenerJob();
+                       fProxyListener.schedule();
+                       fIsRunning = true;
                }
-               fTerminate = false;
-               fProxyListener = new ProxyListenerJob();
-               fProxyListener.schedule();
-               fIsRunning = true;
        }
        
        public void stop() {
@@ -202,9 +167,13 @@ public class XDebugProxy {
         *
         * @param listener the listener to remove
         */
-       public void removeProxyEventListener(IProxyEventListener listener,String key) {
+       public void removeProxyEventListener(IProxyEventListener listener, String key) {
                if (fEventListeners != null) {
-                       fEventListeners.remove(listener,key);
+                       //((XDebugTarget)listener).stopped();
+                       fEventListeners.remove(listener, key);
+                       if (fEventListeners.size() == 0) {
+                               stop();
+                       }
                }
        }       
        
@@ -218,9 +187,9 @@ public class XDebugProxy {
         * @see IDebugEventSetListener
         * @since 2.0
         */
-       public void fireProxyEvent() {
+       public void fireProxyEvent(String id) {
                EventNotifier fNotifier = new EventNotifier();
-               fNotifier.dispatch();
+               fNotifier.dispatch(id);
        }
        
        /**
@@ -243,8 +212,4 @@ public class XDebugProxy {
        public int getProxyPort() {
                return fProxyPort;
        }
-
-       public boolean isRunning() {
-               return fIsRunning;
-       }
 }
\ No newline at end of file