| 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.codegen.simucontroller.debug; |
| 5 | |
| 6 | import java.util.ArrayList; |
| 7 | import java.util.Observable; |
| 8 | import java.util.Observer; |
| 9 | |
| 10 | import org.eclipse.core.resources.IMarkerDelta; |
| 11 | import org.eclipse.debug.core.DebugEvent; |
| 12 | import org.eclipse.debug.core.DebugException; |
| 13 | import org.eclipse.debug.core.ILaunch; |
| 14 | import org.eclipse.debug.core.model.IBreakpoint; |
| 15 | import org.eclipse.debug.core.model.IDebugTarget; |
| 16 | import org.eclipse.debug.core.model.IMemoryBlock; |
| 17 | import org.eclipse.debug.core.model.IProcess; |
| 18 | import org.eclipse.debug.core.model.IThread; |
| 19 | import org.eclipse.emf.common.notify.Notification; |
| 20 | |
| 21 | import de.uka.ipd.sdq.codegen.simucontroller.dockmodel.DockModel; |
| 22 | import de.uka.ipd.sdq.codegen.simucontroller.dockmodel.events.DockEvent; |
| 23 | import de.uka.ipd.sdq.codegen.simucontroller.dockmodel.events.DockSimulationStartedEvent; |
| 24 | import de.uka.ipd.sdq.codegen.simucontroller.dockmodel.events.DockSimulationTerminatedEvent; |
| 25 | import de.uka.ipd.sdq.simucomframework.simucomstatus.Process; |
| 26 | import de.uka.ipd.sdq.simucomframework.simucomstatus.SimuComStatus; |
| 27 | import de.uka.ipd.sdq.simucomframework.simucomstatus.SimucomstatusPackage; |
| 28 | import de.uka.ipd.sdq.simucomframework.simulationdock.SimulationDockService; |
| 29 | |
| 30 | /** |
| 31 | * Class to support SimuCom debugging interactions |
| 32 | * @author Steffen Becker |
| 33 | */ |
| 34 | public class SimulationDebugTarget extends SimulationDebugElement implements IDebugTarget, Observer { |
| 35 | |
| 36 | private SimulationDockService simControl; |
| 37 | private SimulationDebugThread myEventProcessorThread = null; |
| 38 | private DockModel myDock; |
| 39 | private boolean isTerminated; |
| 40 | private ArrayList<IThread> runningSimThreads = new ArrayList<IThread>(); |
| 41 | private SimuComStatus simulationStatus; |
| 42 | |
| 43 | public SimulationDebugTarget(ILaunch launch, |
| 44 | DockModel dock) { |
| 45 | super(null,launch); |
| 46 | myDebugTarget = this; |
| 47 | this.myDock = dock; |
| 48 | this.simControl = dock.getService(); |
| 49 | this.myDock.addObserver(this); |
| 50 | myEventProcessorThread = new SimulationDebugThread(this,launch,dock); |
| 51 | } |
| 52 | |
| 53 | /* (non-Javadoc) |
| 54 | * @see org.eclipse.debug.core.model.IDebugTarget#getName() |
| 55 | */ |
| 56 | public String getName() throws DebugException { |
| 57 | return "SimuCom Simulation Run"; |
| 58 | } |
| 59 | |
| 60 | /* (non-Javadoc) |
| 61 | * @see org.eclipse.debug.core.model.IDebugTarget#getProcess() |
| 62 | */ |
| 63 | public IProcess getProcess() { |
| 64 | return launch.getProcesses()[0]; |
| 65 | } |
| 66 | |
| 67 | /* (non-Javadoc) |
| 68 | * @see org.eclipse.debug.core.model.IDebugTarget#getThreads() |
| 69 | */ |
| 70 | public IThread[] getThreads() throws DebugException { |
| 71 | ArrayList<IThread> result = new ArrayList<IThread>(); |
| 72 | if (!this.isTerminated) { |
| 73 | result.add(myEventProcessorThread); |
| 74 | result.addAll(runningSimThreads); |
| 75 | } |
| 76 | return result.toArray(new IThread[]{}); |
| 77 | } |
| 78 | |
| 79 | /* (non-Javadoc) |
| 80 | * @see org.eclipse.debug.core.model.IDebugTarget#hasThreads() |
| 81 | */ |
| 82 | public boolean hasThreads() throws DebugException { |
| 83 | return !isTerminated(); |
| 84 | } |
| 85 | |
| 86 | /* (non-Javadoc) |
| 87 | * @see org.eclipse.debug.core.model.IDebugTarget#supportsBreakpoint(org.eclipse.debug.core.model.IBreakpoint) |
| 88 | */ |
| 89 | public boolean supportsBreakpoint(IBreakpoint breakpoint) { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | /* (non-Javadoc) |
| 94 | * @see org.eclipse.debug.core.model.ITerminate#canTerminate() |
| 95 | */ |
| 96 | public boolean canTerminate() { |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | /* (non-Javadoc) |
| 101 | * @see org.eclipse.debug.core.model.ITerminate#isTerminated() |
| 102 | */ |
| 103 | public boolean isTerminated() { |
| 104 | return this.isTerminated; |
| 105 | } |
| 106 | |
| 107 | /* (non-Javadoc) |
| 108 | * @see org.eclipse.debug.core.model.ITerminate#terminate() |
| 109 | */ |
| 110 | public void terminate() throws DebugException { |
| 111 | simControl.stopSimulation(); |
| 112 | } |
| 113 | |
| 114 | /* (non-Javadoc) |
| 115 | * @see org.eclipse.debug.core.model.ISuspendResume#canResume() |
| 116 | */ |
| 117 | public boolean canResume() { |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | /* (non-Javadoc) |
| 122 | * @see org.eclipse.debug.core.model.ISuspendResume#canSuspend() |
| 123 | */ |
| 124 | public boolean canSuspend() { |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | /* (non-Javadoc) |
| 129 | * @see org.eclipse.debug.core.model.ISuspendResume#isSuspended() |
| 130 | */ |
| 131 | public boolean isSuspended() { |
| 132 | return this.myDock.isSuspended(); |
| 133 | } |
| 134 | |
| 135 | /* (non-Javadoc) |
| 136 | * @see org.eclipse.debug.core.model.ISuspendResume#resume() |
| 137 | */ |
| 138 | public void resume() throws DebugException { |
| 139 | // Do not execute resume in a UI thread |
| 140 | new Thread(new Runnable(){ |
| 141 | |
| 142 | public void run() { |
| 143 | simControl.resume(); |
| 144 | } |
| 145 | |
| 146 | }).run(); |
| 147 | } |
| 148 | |
| 149 | /* (non-Javadoc) |
| 150 | * @see org.eclipse.debug.core.model.ISuspendResume#suspend() |
| 151 | */ |
| 152 | public void suspend() throws DebugException { |
| 153 | simControl.suspend(); |
| 154 | } |
| 155 | |
| 156 | /* (non-Javadoc) |
| 157 | * @see org.eclipse.debug.core.IBreakpointListener#breakpointAdded(org.eclipse.debug.core.model.IBreakpoint) |
| 158 | */ |
| 159 | public void breakpointAdded(IBreakpoint breakpoint) { |
| 160 | } |
| 161 | |
| 162 | /* (non-Javadoc) |
| 163 | * @see org.eclipse.debug.core.IBreakpointListener#breakpointChanged(org.eclipse.debug.core.model.IBreakpoint, org.eclipse.core.resources.IMarkerDelta) |
| 164 | */ |
| 165 | public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta delta) { |
| 166 | // TODO Auto-generated method stub |
| 167 | |
| 168 | } |
| 169 | |
| 170 | /* (non-Javadoc) |
| 171 | * @see org.eclipse.debug.core.IBreakpointListener#breakpointRemoved(org.eclipse.debug.core.model.IBreakpoint, org.eclipse.core.resources.IMarkerDelta) |
| 172 | */ |
| 173 | public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta delta) { |
| 174 | // TODO Auto-generated method stub |
| 175 | |
| 176 | } |
| 177 | |
| 178 | /* (non-Javadoc) |
| 179 | * @see org.eclipse.debug.core.model.IDisconnect#canDisconnect() |
| 180 | */ |
| 181 | public boolean canDisconnect() { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | /* (non-Javadoc) |
| 186 | * @see org.eclipse.debug.core.model.IDisconnect#disconnect() |
| 187 | */ |
| 188 | public void disconnect() throws DebugException { |
| 189 | } |
| 190 | |
| 191 | /* (non-Javadoc) |
| 192 | * @see org.eclipse.debug.core.model.IDisconnect#isDisconnected() |
| 193 | */ |
| 194 | public boolean isDisconnected() { |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | /* (non-Javadoc) |
| 199 | * @see org.eclipse.debug.core.model.IMemoryBlockRetrieval#getMemoryBlock(long, long) |
| 200 | */ |
| 201 | public IMemoryBlock getMemoryBlock(long startAddress, long length) |
| 202 | throws DebugException { |
| 203 | return null; |
| 204 | } |
| 205 | |
| 206 | /* (non-Javadoc) |
| 207 | * @see org.eclipse.debug.core.model.IMemoryBlockRetrieval#supportsStorageRetrieval() |
| 208 | */ |
| 209 | public boolean supportsStorageRetrieval() { |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | public void dispose() { |
| 214 | assert isTerminated; |
| 215 | |
| 216 | this.myEventProcessorThread.dispose(); |
| 217 | |
| 218 | this.simControl = null; |
| 219 | this.myDock.deleteObserver(this); |
| 220 | this.myDock = null; |
| 221 | } |
| 222 | |
| 223 | public void update(Observable o, Object arg) { |
| 224 | if (arg instanceof DockEvent) { |
| 225 | DockEvent dockEvent = (DockEvent) arg; |
| 226 | if (dockEvent.comesFrom(this.myDock)) { |
| 227 | if (dockEvent instanceof DockSimulationTerminatedEvent) { |
| 228 | this.isTerminated = true; |
| 229 | if (!this.myDock.isRemote()) { |
| 230 | this.simulationStatus.eAdapters().remove(this); |
| 231 | this.simulationStatus.getProcessStatus().eAdapters().remove(this); |
| 232 | this.simulationStatus.getResourceStatus().eAdapters().remove(this); |
| 233 | } |
| 234 | fireEvent(this, DebugEvent.TERMINATE); |
| 235 | } |
| 236 | if (dockEvent instanceof DockSimulationStartedEvent) { |
| 237 | if (!this.myDock.isRemote()) { |
| 238 | this.simulationStatus = this.simControl.getSimuComStatus(); |
| 239 | this.simulationStatus.eAdapters().add(this); |
| 240 | this.simulationStatus.getProcessStatus().eAdapters().add(this); |
| 241 | this.simulationStatus.getResourceStatus().eAdapters().add(this); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public void notifyChanged(Notification notification) { |
| 250 | if (notification.getEventType() == Notification.ADD) { |
| 251 | if (notification.getFeature() == SimucomstatusPackage.eINSTANCE.getSimulatedProcesses_Processes()) { |
| 252 | this.runningSimThreads.add(new SimuComProcessDebugThread(this,launch,(Process)notification.getNewValue())); |
| 253 | fireEvent(this, DebugEvent.CHANGE); |
| 254 | } |
| 255 | } |
| 256 | if (notification.getEventType() == Notification.REMOVE) { |
| 257 | if (notification.getFeature() == SimucomstatusPackage.eINSTANCE.getSimulatedProcesses_Processes()) { |
| 258 | SimuComProcessDebugThread candidate = null; |
| 259 | for (IThread thread : runningSimThreads) { |
| 260 | if (thread instanceof SimuComProcessDebugThread) { |
| 261 | candidate = (SimuComProcessDebugThread) thread; |
| 262 | if (candidate.getProcess() == notification.getOldValue()){ |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | this.runningSimThreads.remove(candidate); |
| 268 | candidate.dispose(); |
| 269 | fireEvent(this, DebugEvent.CHANGE); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |