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");
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();
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;
}
* @see org.eclipse.core.runtime.ISafeRunnable#run()
*/
public void run() throws Exception {
- fListener.handleProxyEvent(fIdeKey, fInitString, fConnection);
+ fListener.handleProxyEvent(/*fIdeKey,*/ fConnection);
}
/**
*
* @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() {
*
* @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();
+ }
}
}
* @see IDebugEventSetListener
* @since 2.0
*/
- public void fireProxyEvent() {
+ public void fireProxyEvent(String id) {
EventNotifier fNotifier = new EventNotifier();
- fNotifier.dispatch();
+ fNotifier.dispatch(id);
}
/**
public int getProxyPort() {
return fProxyPort;
}
-
- public boolean isRunning() {
- return fIsRunning;
- }
}
\ No newline at end of file