1 package net.sourceforge.phpeclipse.xdebug.core;
3 import java.io.DataInputStream;
4 import java.io.IOException;
5 import java.io.OutputStreamWriter;
6 import java.io.UnsupportedEncodingException;
7 import java.net.ServerSocket;
8 import java.net.Socket;
9 import java.net.UnknownHostException;
11 import net.sourceforge.phpeclipse.xdebug.core.xdebug.XDebugConnection;
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.ISafeRunnable;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.core.runtime.jobs.Job;
19 import org.eclipse.debug.core.DebugException;
20 import org.eclipse.debug.core.IDebugEventFilter;
21 import org.eclipse.debug.core.IDebugEventSetListener;
23 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget;
25 public class XDebugProxy {
26 private XDebugTarget fTarget;
28 protected String fInitString;
29 protected String fIdeKey;
31 protected AbstractDebugConnection fConnection;
33 class ProxyListenerJob extends Job {
34 public ProxyListenerJob() {
35 super("XDebug Proxy Connection Dispatch");
40 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
42 protected IStatus run(IProgressMonitor monitor) {
45 DataInputStream reader = null;
46 OutputStreamWriter writer = null;
53 if (monitor.isCanceled()) return Status.CANCEL_STATUS;
55 socket = fProxyServerSocket.accept();
56 } catch (IOException e) {
60 XDebugCorePlugin.log(IStatus.INFO,"Proxy: someone tries to connect");
63 writer = new OutputStreamWriter(socket.getOutputStream(), "UTF8");
64 reader = new DataInputStream(socket.getInputStream());
65 } catch (UnsupportedEncodingException e) {
66 // TODO Auto-generated catch block
68 } catch (IOException e) {
69 // TODO Auto-generated catch block
72 fConnection=(AbstractDebugConnection) new XDebugConnection(socket,reader,writer);
73 if (fConnection.isInitialized()) {
74 fIdeKey=fConnection.getSessionID();
75 XDebugCorePlugin.log(IStatus.INFO,"<init idekey \""+fIdeKey+"\">");
82 return Status.OK_STATUS;
87 * Filters and dispatches events in a safe runnable to handle any
90 class EventNotifier implements ISafeRunnable {
92 private IProxyEventListener fListener;
95 * @see org.eclipse.core.runtime.ISafeRunnable#handleException(java.lang.Throwable)
97 public void handleException(Throwable exception) {
98 IStatus status = new Status(IStatus.ERROR, XDebugCorePlugin.getUniqueIdentifier(), IStatus.ERROR, "An exception occurred while dispatching proxy", exception); //$NON-NLS-1$
99 XDebugCorePlugin.log(status);
103 * @see org.eclipse.core.runtime.ISafeRunnable#run()
105 public void run() throws Exception {
106 fListener.handleProxyEvent(fIdeKey, fInitString, fConnection);
110 * Filter and dispatch the given events. If an exception occurs in one
111 * listener, events are still fired to subsequent listeners.
113 * @param events debug events
115 public void dispatch() {
116 fListener = (IProxyEventListener) getEventListener(fIdeKey);
117 if (fListener==null) { // no listener is found so start the script
120 } catch (DebugException e) {
121 // TODO Auto-generated catch block
132 private ServerSocket fProxyServerSocket;
133 private ProxyListenerJob fProxyListener;
134 private boolean fTerminate;
135 private int fProxyPort;
136 private ListenerMap fEventListeners;
137 private boolean fIsRunning;
140 public XDebugProxy (int port) {
144 public void setTarget( XDebugTarget Target ) {
148 public XDebugTarget getTarget() {
152 public void start() {
156 fProxyServerSocket = new ServerSocket(fProxyPort);
157 XDebugCorePlugin.log(IStatus.INFO,"Proxy listens on port "+fProxyPort);
158 } catch (UnknownHostException e) {
160 } catch (IOException e) {
164 fProxyListener = new ProxyListenerJob();
165 fProxyListener.schedule();
171 fProxyListener.cancel();
174 fProxyServerSocket.close();
175 } catch (IOException e) {
179 XDebugCorePlugin.log(IStatus.INFO,"Proxy stopped");
184 * Adds the given listener to the collection of registered proxy
185 * event listeners. Has no effect if an identical listener is already
188 * @param listener the listener to add
191 public void addProxyEventListener(IProxyEventListener listener, String key) {
192 if (fEventListeners == null) {
193 fEventListeners = new ListenerMap(5);
195 fEventListeners.add(listener, key);
199 * Removes the given listener from the collection of registered proxy
200 * event listeners. Has no effect if an identical listener is not already
203 * @param listener the listener to remove
205 public void removeProxyEventListener(IProxyEventListener listener,String key) {
206 if (fEventListeners != null) {
207 fEventListeners.remove(listener,key);
212 * Notifies all registered proxy event set listeners of the given
213 * proxy events. Events which are filtered by a registered debug event
214 * filter are not fired.
216 * @param events array of debug events to fire
217 * @see IDebugEventFilter
218 * @see IDebugEventSetListener
221 public void fireProxyEvent() {
222 EventNotifier fNotifier = new EventNotifier();
223 fNotifier.dispatch();
227 * Returns the collection of registered proxy event listeners
229 * @return list of registered proxy event listeners, instances
230 * of <code>IProxyEventListeners</code>
232 /*private Map getEventListeners() {
233 return fEventListeners.getListeners();
236 private Object getEventListener(String ideKey) {
237 return fEventListeners.getListener(ideKey);
241 * @return Returns the fProxyPort.
243 public int getProxyPort() {
247 public boolean isRunning() {