First submit for debug plugin
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / model / PHPDebugTarget.java
diff --git a/net.sourceforge.phpeclipse.debug.core/src/net/sourceforge/phpdt/internal/debug/core/model/PHPDebugTarget.java b/net.sourceforge.phpeclipse.debug.core/src/net/sourceforge/phpdt/internal/debug/core/model/PHPDebugTarget.java
new file mode 100644 (file)
index 0000000..9ecd65e
--- /dev/null
@@ -0,0 +1,241 @@
+/**********************************************************************
+Copyright (c) 2000, 2002 IBM Corp. and others.
+All rights reserved. This program and the accompanying materials
+are made available under the terms of the Common Public License v1.0
+which accompanies this distribution, and is available at
+http://www.eclipse.org/legal/cpl-v10.html
+
+Contributors:
+    IBM Corporation - Initial implementation
+    Vicente Fernando - www.alfersoft.com.ar
+**********************************************************************/
+package net.sourceforge.phpdt.internal.debug.core.model;
+
+import org.eclipse.core.resources.IMarkerDelta;
+import org.eclipse.debug.core.DebugEvent;
+import org.eclipse.debug.core.DebugException;
+import org.eclipse.debug.core.DebugPlugin;
+import org.eclipse.debug.core.IBreakpointManager;
+import org.eclipse.debug.core.IDebugEventSetListener;
+import org.eclipse.debug.core.ILaunch;
+import org.eclipse.debug.core.ILaunchListener;
+import org.eclipse.debug.core.model.IDebugTarget;
+import org.eclipse.debug.core.model.IBreakpoint;
+import org.eclipse.debug.core.model.IMemoryBlock;
+import org.eclipse.debug.core.model.IProcess;
+import org.eclipse.debug.core.model.IThread;
+
+import net.sourceforge.phpdt.internal.debug.core.PHPDebugCorePlugin;
+import net.sourceforge.phpdt.internal.debug.core.PHPDBGProxy;
+
+/**
+ * Debug target for PHP debug model.
+ */
+public class PHPDebugTarget implements IPHPDebugTarget, ILaunchListener, IDebugEventSetListener {
+               
+       private IProcess process;
+       private boolean isTerminated;
+       private ILaunch launch;
+       private PHPThread[] threads;
+       private PHPDBGProxy phpDBGProxy;
+
+       public PHPDebugTarget(ILaunch launch, IProcess process) {
+               this.launch = launch;
+               this.process = process;
+               this.threads = new PHPThread[0];
+               IBreakpointManager manager= DebugPlugin.getDefault().getBreakpointManager();
+               manager.addBreakpointListener(this);
+               DebugPlugin.getDefault().addDebugEventListener(this);
+               initialize();
+       }
+
+       protected synchronized void initialize() {
+               DebugEvent ev = new DebugEvent(this, DebugEvent.CREATE);
+               DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });    
+       }
+
+       public void addThread(PHPThread phpThread) {
+               int i;
+               PHPThread[] updatedThreads = new PHPThread[threads.length + 1];
+               
+               for(i=0; i < threads.length; i++) {
+                       updatedThreads[i] = threads[i];
+               }
+               updatedThreads[i] = phpThread;
+               threads = updatedThreads;
+
+               fireChangeEvent();
+       }
+
+       private void fireChangeEvent() {
+               DebugEvent ev = new DebugEvent(this, DebugEvent.CHANGE);
+               DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
+       }
+
+       protected PHPThread getThreadById(int id) {
+               for (int i = 0; i < threads.length; i++) {
+                       if (threads[i].getId() == id) {
+                               return threads[i];
+                       }
+               }
+               return null;
+       }
+
+       public IThread[] getThreads() {
+               return threads;
+       }
+
+       public boolean hasThreads() throws DebugException {
+               return threads.length > 0;
+       }
+
+       public String getName() throws DebugException {
+               return "PHP Debugger at localhost:" + getPHPDBGProxy().getPort();
+       }
+
+       public boolean supportsBreakpoint(IBreakpoint arg0) {
+               return false;
+       }
+
+       public String getModelIdentifier() {
+               return PHPDebugCorePlugin.getUniqueIdentifier();
+       }
+
+       public IDebugTarget getDebugTarget() {
+               return this;
+       }
+
+       public ILaunch getLaunch() {
+               return launch;
+       }
+
+       public boolean canTerminate() {
+               return !isTerminated;
+       }
+
+       public boolean isTerminated() {
+               return isTerminated;
+       }
+
+       public void terminate() {
+               phpDBGProxy.stop();
+               this.threads = new PHPThread[0];
+               isTerminated = true;
+               fireChangeEvent();
+       }
+
+       public boolean canResume() {
+               return false;
+       }
+
+       public boolean canSuspend() {
+               return false;
+       }
+
+       public boolean isSuspended() {
+               return false;
+       }
+
+       public void resume() throws DebugException {
+       }
+
+       public void suspend() throws DebugException {
+       }
+
+       public void breakpointAdded(IBreakpoint breakpoint) {
+               this.getPHPDBGProxy().addBreakpoint(breakpoint) ;
+       }
+
+       public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta arg1) {
+               this.getPHPDBGProxy().removeBreakpoint(breakpoint) ;            
+       }
+
+       public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta arg1) {
+               // is called e.g. after a line has been inserted before a breakpoint
+               // but then the debugger is out of sync with the file anyway, so debugging
+               // should be stopped here.
+       }
+
+       public boolean canDisconnect() {
+               return false;
+       }
+
+       public void disconnect() throws DebugException {
+       }
+
+       public boolean isDisconnected() {
+               return false;
+       }
+
+       public boolean supportsStorageRetrieval() {
+               return false;
+       }
+
+       public IMemoryBlock getMemoryBlock(long arg0, long arg1) throws DebugException {
+               return null;
+       }
+
+       public Object getAdapter(Class arg0) {
+               return null;
+       }
+
+       public IProcess getProcess() {
+               return process;
+       }
+
+       public void setProcess(IProcess process) {
+               this.process = process;
+       }
+
+       public PHPDBGProxy getPHPDBGProxy() {
+               return phpDBGProxy;
+       }
+
+       public void setPHPDBGProxy(PHPDBGProxy phpDBGProxy) {
+               this.phpDBGProxy = phpDBGProxy;
+       }
+       
+       /**
+        * @see ILaunchListener#launchRemoved(ILaunch)
+        */
+       public void launchRemoved(ILaunch launch) {
+               if (!isTerminated()) {
+                       return;
+               }
+               if (launch.equals(getLaunch())) {
+                       // This target has been deregistered, but it hasn't successfully terminated.
+                       // Update internal state to reflect that it is disconnected
+                       terminate();
+               }
+       }
+       
+       /**
+        * @see ILaunchListener#launchAdded(ILaunch)
+        */
+       public void launchAdded(ILaunch launch) {
+       }
+       
+       /**
+        * @see ILaunchListener#launchChanged(ILaunch)
+        */
+       public void launchChanged(ILaunch launch) {
+       }
+       
+       /**
+        * When a debug target or process terminates, terminate DBG Proxy.
+        * 
+        * @see IDebugEventSetListener#handleDebugEvents(DebugEvent[])
+        */
+       public void handleDebugEvents(DebugEvent[] events) {
+               for (int i = 0; i < events.length; i++) {
+                       DebugEvent event = events[i];
+                       if (event.getKind() == DebugEvent.TERMINATE) {
+                               Object source = event.getSource();
+                               if (source instanceof PHPDebugTarget || source instanceof IDebugTarget || source instanceof IProcess) {
+                                       getPHPDBGProxy().stop();
+                               }
+                       }
+               }
+       }
+}
+