X-Git-Url: http://git.phpeclipse.com diff --git a/net.sourceforge.phpeclipse.xdebug.core/src/net/sourceforge/phpeclipse/xdebug/core/XDebugProxy.java b/net.sourceforge.phpeclipse.xdebug.core/src/net/sourceforge/phpeclipse/xdebug/core/XDebugProxy.java index 3d6396a..26183fb 100644 --- a/net.sourceforge.phpeclipse.xdebug.core/src/net/sourceforge/phpeclipse/xdebug/core/XDebugProxy.java +++ b/net.sourceforge.phpeclipse.xdebug.core/src/net/sourceforge/phpeclipse/xdebug/core/XDebugProxy.java @@ -1,89 +1,72 @@ 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 AbstractDebugConnection 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"); - setSystem(true); - + setSystem(true); } /* (non-Javadoc) * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor) */ protected IStatus run(IProgressMonitor monitor) { - boolean timeout; boolean error; - Socket socket=null; - DataInputStream reader=null; - OutputStreamWriter writer=null; + Socket socket = null; while (!fTerminate) { - timeout = false; error = false; - socket=null; - reader=null; - writer=null; + socket = null; if (monitor.isCanceled()) return Status.CANCEL_STATUS; try { socket = fProxyServerSocket.accept(); - } catch (java.net.SocketTimeoutException e) { - timeout=true; } catch (IOException e) { - error=true; -// e.printStackTrace(); + error = true; } - if (!(timeout || error)) { + 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,""); - - fireProxyEvent(); + String IdeKey = fConnection.getSessionID(); + + Object a = getEventListener(IdeKey); + + if (a instanceof XDebugTarget) { + XDebugCorePlugin.log(IStatus.INFO, ""); + + fireProxyEvent(IdeKey); + } else { + fConnection.close(); + fConnection = null; + } } - } - + } } return Status.OK_STATUS; } @@ -109,8 +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); } /** @@ -119,84 +101,36 @@ 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); - // set 5sek as timeout - fProxyServerSocket.setSoTimeout(5000); - XDebugCorePlugin.log(IStatus.INFO,"Proxy listens on port "+fProxyPort); - -// fDebugReader = new BufferedReader(new InputStreamReader(fDebugSocket.getInputStream())); - - } catch (UnknownHostException e) { - e.printStackTrace(); -// abort("Unable to connect to PHP Debuger", e); - } catch (IOException e) { - e.printStackTrace(); -// abort("Unable to connect to PHP Debuger", e); + 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() { - if (!fIsRunning) - return; - fTerminate=true; - fProxyListener.cancel(); - try { - fProxyServerSocket.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - fIsRunning = false; - XDebugCorePlugin.log(IStatus.INFO,"Proxy stopped"); - - }*/ - public void stop() { if (fIsRunning) { fProxyListener.cancel(); @@ -233,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(); + } } } @@ -249,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); } /** @@ -274,8 +212,4 @@ public class XDebugProxy { public int getProxyPort() { return fProxyPort; } - - public boolean isRunning() { - return fIsRunning; - } } \ No newline at end of file