1) Added missing strings for italic, underline and strike through.
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / core / XDebugProxy.java
1 package net.sourceforge.phpeclipse.xdebug.core;
2
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;
16
17 import net.sourceforge.phpeclipse.xdebug.core.xdebug.XDebugConnection;
18 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget;
19
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;
28         
29         class ProxyListenerJob extends Job {
30                 public ProxyListenerJob() {
31                         super("XDebug Proxy Connection Dispatch");
32                         setSystem(true);        
33                 }
34                 
35                 /* (non-Javadoc)
36                  * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
37                  */
38                 protected IStatus run(IProgressMonitor monitor) {
39                         boolean error;
40                         Socket socket = null;
41                         
42                         while (!fTerminate) {
43                                 error = false;
44                                 socket = null;
45                                 if (monitor.isCanceled()) return Status.CANCEL_STATUS;
46                                 try {
47                                         socket = fProxyServerSocket.accept();
48                                 } catch (IOException e) {
49                                         error = true;
50                                 }
51                                 if (!error) {
52                                         XDebugCorePlugin.log(IStatus.INFO,"Proxy: someone tries to connect");
53                                         
54                                         fConnection = new XDebugConnection(socket);     
55                                         if (fConnection.isInitialized()) {
56                                                 String IdeKey = fConnection.getSessionID();
57                                                 
58                                                 Object a = getEventListener(IdeKey);
59                                                 
60                                                 if (a instanceof XDebugTarget) {
61                                                         XDebugCorePlugin.log(IStatus.INFO, "<init idekey \"" + IdeKey + "\">");
62                                                 
63                                                         fireProxyEvent(IdeKey);
64                                                 } else {
65                                                         fConnection.close();
66                                                         fConnection = null;
67                                                 }
68                                         }
69                                 }       
70                         }
71                         return Status.OK_STATUS;
72                 }
73         }
74         
75         /**
76          * Filters and dispatches events in a safe runnable to handle any
77          * exceptions.
78          */
79         class EventNotifier implements ISafeRunnable {
80                 
81                 private IProxyEventListener fListener;
82                 
83                 /**
84                  * @see org.eclipse.core.runtime.ISafeRunnable#handleException(java.lang.Throwable)
85                  */
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);
89                 }
90
91                 /**
92                  * @see org.eclipse.core.runtime.ISafeRunnable#run()
93                  */
94                 public void run() throws Exception {
95                         fListener.handleProxyEvent(/*fIdeKey,*/ fConnection);
96                 }
97                 
98                 /**
99                  * Filter and dispatch the given events. If an exception occurs in one
100                  * listener, events are still fired to subsequent listeners.
101                  * 
102                  * @param events debug events
103                  */
104                 public void dispatch(String id) {
105                         fListener = (IProxyEventListener) getEventListener(id/*fIdeKey*/);
106                         SafeRunnable.run(this);
107                         //Platform.run(this);
108                         fListener = null;
109                 }
110
111         }
112
113         public XDebugProxy(int port) {
114                 fProxyPort = port;
115         }
116         
117         public void start() {
118                 if (!fIsRunning) {
119                         try {
120                                 fProxyServerSocket = new ServerSocket(fProxyPort);
121                                 XDebugCorePlugin.log(IStatus.INFO,"Proxy listens on port "+fProxyPort);
122                         } catch (UnknownHostException e) {
123                                 e.printStackTrace();
124                         } catch (IOException e) {
125                                 e.printStackTrace();
126                         }
127                         fTerminate = false;
128                         fProxyListener = new ProxyListenerJob();
129                         fProxyListener.schedule();
130                         fIsRunning = true;
131                 }
132         }
133         
134         public void stop() {
135                 if (fIsRunning) {
136                         fProxyListener.cancel();
137                         fTerminate = true;
138                         try {
139                                 fProxyServerSocket.close();
140                         } catch (IOException e) {
141                                 e.printStackTrace();
142                         }
143                         fIsRunning = false;
144                         XDebugCorePlugin.log(IStatus.INFO,"Proxy stopped");
145                 }
146         }
147
148         /**
149          * Adds the given listener to the collection of registered proxy
150          * event listeners. Has no effect if an identical listener is already
151          * registered.
152          *
153          * @param listener the listener to add
154
155          */
156         public void addProxyEventListener(IProxyEventListener listener, String key) {
157                 if (fEventListeners == null) {
158                         fEventListeners = new ListenerMap(5);
159                 }
160                 fEventListeners.add(listener, key);
161         }
162         
163         /**
164          * Removes the given listener from the collection of registered proxy
165          * event listeners. Has no effect if an identical listener is not already
166          * registered.
167          *
168          * @param listener the listener to remove
169          */
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) {
175                                 stop();
176                         }
177                 }
178         }       
179         
180         /**
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.
184          * 
185          * @param events array of debug events to fire
186          * @see IDebugEventFilter
187          * @see IDebugEventSetListener
188          * @since 2.0
189          */
190         public void fireProxyEvent(String id) {
191                 EventNotifier fNotifier = new EventNotifier();
192                 fNotifier.dispatch(id);
193         }
194         
195         /**
196          * Returns the collection of registered proxy event listeners
197          * 
198          * @return list of registered proxy event listeners, instances
199          *  of <code>IProxyEventListeners</code>
200          */
201         /*private Map getEventListeners() {
202                 return fEventListeners.getListeners();
203         }*/
204         
205         private Object getEventListener(String ideKey) {
206                 return fEventListeners.getListener(ideKey);
207         }
208                 
209         /**
210          * @return Returns the fProxyPort.
211          */
212         public int getProxyPort() {
213                 return fProxyPort;
214         }
215 }