1 package net.sourceforge.phpeclipse.xdebug.core;
3 import java.io.IOException;
4 import java.net.ServerSocket;
5 import java.net.Socket;
6 import java.net.UnknownHostException;
7 import org.eclipse.core.runtime.IProgressMonitor;
8 import org.eclipse.core.runtime.ISafeRunnable;
9 import org.eclipse.core.runtime.IStatus;
10 //import org.eclipse.core.runtime.Platform;
11 import org.eclipse.core.runtime.Status;
12 import org.eclipse.core.runtime.jobs.Job;
13 import org.eclipse.debug.core.IDebugEventFilter;
14 import org.eclipse.debug.core.IDebugEventSetListener;
15 import org.eclipse.jface.util.SafeRunnable;
17 import net.sourceforge.phpeclipse.xdebug.core.xdebug.XDebugConnection;
18 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget;
20 public class XDebugProxy {
21 private XDebugConnection fConnection;
22 private ServerSocket fProxyServerSocket;
23 private ProxyListenerJob fProxyListener;
24 private boolean fTerminate;
25 private int fProxyPort;
26 private ListenerMap fEventListeners;
27 private boolean fIsRunning;
29 class ProxyListenerJob extends Job {
30 public ProxyListenerJob() {
31 super("XDebug Proxy Connection Dispatch");
36 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
38 protected IStatus run(IProgressMonitor monitor) {
45 if (monitor.isCanceled()) return Status.CANCEL_STATUS;
47 socket = fProxyServerSocket.accept();
48 } catch (IOException e) {
52 XDebugCorePlugin.log(IStatus.INFO,"Proxy: someone tries to connect");
54 fConnection = new XDebugConnection(socket);
55 if (fConnection.isInitialized()) {
56 String IdeKey = fConnection.getSessionID();
58 Object a = getEventListener(IdeKey);
60 if (a instanceof XDebugTarget) {
61 XDebugCorePlugin.log(IStatus.INFO, "<init idekey \"" + IdeKey + "\">");
63 fireProxyEvent(IdeKey);
71 return Status.OK_STATUS;
76 * Filters and dispatches events in a safe runnable to handle any
79 class EventNotifier implements ISafeRunnable {
81 private IProxyEventListener fListener;
84 * @see org.eclipse.core.runtime.ISafeRunnable#handleException(java.lang.Throwable)
86 public void handleException(Throwable exception) {
87 IStatus status = new Status(IStatus.ERROR, XDebugCorePlugin.getUniqueIdentifier(), IStatus.ERROR, "An exception occurred while dispatching proxy", exception); //$NON-NLS-1$
88 XDebugCorePlugin.log(status);
92 * @see org.eclipse.core.runtime.ISafeRunnable#run()
94 public void run() throws Exception {
95 fListener.handleProxyEvent(/*fIdeKey,*/ fConnection);
99 * Filter and dispatch the given events. If an exception occurs in one
100 * listener, events are still fired to subsequent listeners.
102 * @param events debug events
104 public void dispatch(String id) {
105 fListener = (IProxyEventListener) getEventListener(id/*fIdeKey*/);
106 SafeRunnable.run(this);
107 //Platform.run(this);
113 public XDebugProxy(int port) {
117 public void start() {
120 fProxyServerSocket = new ServerSocket(fProxyPort);
121 XDebugCorePlugin.log(IStatus.INFO,"Proxy listens on port "+fProxyPort);
122 } catch (UnknownHostException e) {
124 } catch (IOException e) {
128 fProxyListener = new ProxyListenerJob();
129 fProxyListener.schedule();
136 fProxyListener.cancel();
139 fProxyServerSocket.close();
140 } catch (IOException e) {
144 XDebugCorePlugin.log(IStatus.INFO,"Proxy stopped");
149 * Adds the given listener to the collection of registered proxy
150 * event listeners. Has no effect if an identical listener is already
153 * @param listener the listener to add
156 public void addProxyEventListener(IProxyEventListener listener, String key) {
157 if (fEventListeners == null) {
158 fEventListeners = new ListenerMap(5);
160 fEventListeners.add(listener, key);
164 * Removes the given listener from the collection of registered proxy
165 * event listeners. Has no effect if an identical listener is not already
168 * @param listener the listener to remove
170 public void removeProxyEventListener(IProxyEventListener listener, String key) {
171 if (fEventListeners != null) {
172 //((XDebugTarget)listener).stopped();
173 fEventListeners.remove(listener, key);
174 if (fEventListeners.size() == 0) {
181 * Notifies all registered proxy event set listeners of the given
182 * proxy events. Events which are filtered by a registered debug event
183 * filter are not fired.
185 * @param events array of debug events to fire
186 * @see IDebugEventFilter
187 * @see IDebugEventSetListener
190 public void fireProxyEvent(String id) {
191 EventNotifier fNotifier = new EventNotifier();
192 fNotifier.dispatch(id);
196 * Returns the collection of registered proxy event listeners
198 * @return list of registered proxy event listeners, instances
199 * of <code>IProxyEventListeners</code>
201 /*private Map getEventListeners() {
202 return fEventListeners.getListeners();
205 private Object getEventListener(String ideKey) {
206 return fEventListeners.getListener(ideKey);
210 * @return Returns the fProxyPort.
212 public int getProxyPort() {