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");
41 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
43 protected IStatus run(IProgressMonitor monitor) {
47 DataInputStream reader=null;
48 OutputStreamWriter writer=null;
56 if (monitor.isCanceled()) return Status.CANCEL_STATUS;
58 socket = fProxyServerSocket.accept();
59 } catch (java.net.SocketTimeoutException e) {
61 } catch (IOException e) {
63 // e.printStackTrace();
65 if (!(timeout || error)) {
66 XDebugCorePlugin.log(IStatus.INFO,"Proxy: someone tries to connect");
69 writer = new OutputStreamWriter(socket.getOutputStream(), "UTF8");
70 reader = new DataInputStream(socket.getInputStream());
71 } catch (UnsupportedEncodingException e) {
72 // TODO Auto-generated catch block
74 } catch (IOException e) {
75 // TODO Auto-generated catch block
78 fConnection=(AbstractDebugConnection) new XDebugConnection(socket,reader,writer);
79 if (fConnection.isInitialized()) {
80 fIdeKey=fConnection.getSessionID();
81 XDebugCorePlugin.log(IStatus.INFO,"<init idekey \""+fIdeKey+"\">");
88 return Status.OK_STATUS;
93 * Filters and dispatches events in a safe runnable to handle any
96 class EventNotifier implements ISafeRunnable {
98 private IProxyEventListener fListener;
101 * @see org.eclipse.core.runtime.ISafeRunnable#handleException(java.lang.Throwable)
103 public void handleException(Throwable exception) {
104 IStatus status = new Status(IStatus.ERROR, XDebugCorePlugin.getUniqueIdentifier(), IStatus.ERROR, "An exception occurred while dispatching proxy", exception); //$NON-NLS-1$
105 XDebugCorePlugin.log(status);
109 * @see org.eclipse.core.runtime.ISafeRunnable#run()
111 public void run() throws Exception {
113 fListener.handleProxyEvent(fIdeKey, fInitString, fConnection);
117 * Filter and dispatch the given events. If an exception occurs in one
118 * listener, events are still fired to subsequent listeners.
120 * @param events debug events
122 public void dispatch() {
123 fListener = (IProxyEventListener) getEventListener(fIdeKey);
124 if (fListener==null) { // no listener is found so start the script
127 } catch (DebugException e) {
128 // TODO Auto-generated catch block
139 private ServerSocket fProxyServerSocket;
140 private ProxyListenerJob fProxyListener;
141 private boolean fTerminate;
142 private int fProxyPort;
143 private ListenerMap fEventListeners;
144 private boolean fIsRunning;
147 public XDebugProxy (int port) {
151 public void setTarget( XDebugTarget Target ) {
155 public XDebugTarget getTarget() {
159 public void start() {
163 fProxyServerSocket = new ServerSocket(fProxyPort);
164 // set 5sek as timeout
165 fProxyServerSocket.setSoTimeout(5000);
166 XDebugCorePlugin.log(IStatus.INFO,"Proxy listens on port "+fProxyPort);
168 // fDebugReader = new BufferedReader(new InputStreamReader(fDebugSocket.getInputStream()));
170 } catch (UnknownHostException e) {
172 // abort("Unable to connect to PHP Debuger", e);
173 } catch (IOException e) {
175 // abort("Unable to connect to PHP Debuger", e);
178 fProxyListener = new ProxyListenerJob();
179 fProxyListener.schedule();
184 /* public void stop() {
188 fProxyListener.cancel();
190 fProxyServerSocket.close();
191 } catch (IOException e) {
192 // TODO Auto-generated catch block
196 XDebugCorePlugin.log(IStatus.INFO,"Proxy stopped");
202 fProxyListener.cancel();
205 fProxyServerSocket.close();
206 } catch (IOException e) {
210 XDebugCorePlugin.log(IStatus.INFO,"Proxy stopped");
215 * Adds the given listener to the collection of registered proxy
216 * event listeners. Has no effect if an identical listener is already
219 * @param listener the listener to add
222 public void addProxyEventListener(IProxyEventListener listener, String key) {
223 if (fEventListeners == null) {
224 fEventListeners = new ListenerMap(5);
226 fEventListeners.add(listener, key);
230 * Removes the given listener from the collection of registered proxy
231 * event listeners. Has no effect if an identical listener is not already
234 * @param listener the listener to remove
236 public void removeProxyEventListener(IProxyEventListener listener,String key) {
237 if (fEventListeners != null) {
238 fEventListeners.remove(listener,key);
243 * Notifies all registered proxy event set listeners of the given
244 * proxy events. Events which are filtered by a registered debug event
245 * filter are not fired.
247 * @param events array of debug events to fire
248 * @see IDebugEventFilter
249 * @see IDebugEventSetListener
252 public void fireProxyEvent() {
253 EventNotifier fNotifier = new EventNotifier();
254 fNotifier.dispatch();
258 * Returns the collection of registered proxy event listeners
260 * @return list of registered proxy event listeners, instances
261 * of <code>IProxyEventListeners</code>
263 /*private Map getEventListeners() {
264 return fEventListeners.getListeners();
267 private Object getEventListener(String ideKey) {
268 return fEventListeners.getListener(ideKey);
272 * @return Returns the fProxyPort.
274 public int getProxyPort() {
278 public boolean isRunning() {