1 package net.mograsim.plugin.launch;
3 import java.util.HashMap;
6 import org.eclipse.core.runtime.IStatus;
7 import org.eclipse.core.runtime.PlatformObject;
8 import org.eclipse.core.runtime.Status;
9 import org.eclipse.debug.core.DebugEvent;
10 import org.eclipse.debug.core.DebugException;
11 import org.eclipse.debug.core.DebugPlugin;
12 import org.eclipse.debug.core.ILaunch;
13 import org.eclipse.debug.core.ILaunchConfiguration;
14 import org.eclipse.debug.core.model.IDebugTarget;
15 import org.eclipse.debug.core.model.IProcess;
16 import org.eclipse.debug.core.model.IStreamsProxy;
17 import org.eclipse.debug.core.model.ISuspendResume;
19 import net.mograsim.logic.model.LogicExecuter;
20 import net.mograsim.machine.Machine;
21 import net.mograsim.machine.MachineDefinition;
22 import net.mograsim.plugin.MograsimActivator;
24 public class MachineProcess extends PlatformObject implements IProcess, ISuspendResume
26 private final ILaunch launch;
27 private final Machine machine;
28 private final LogicExecuter exec;
30 private boolean running;
32 public MachineProcess(ILaunch launch, MachineDefinition machineDefinition)
35 this.machine = machineDefinition.createNew();
36 this.exec = new LogicExecuter(machine.getTimeline());
38 exec.startLiveExecution();
41 launch.addProcess(this);
45 public Machine getMachine()
51 public ILaunch getLaunch()
56 public String getName()
58 return "Mograsim machine \"" + machine.getDefinition().getId() + '"';
62 public String getLabel()
64 return getName() + " (" + getStatusString() + ')';
67 public String getStatusString()
69 return isTerminated() ? "terminated" : isSuspended() ? "paused" : "running";
72 public void setExecutionSpeed(double speed)
74 exec.setSpeedFactor(speed);
78 public boolean isSuspended()
80 return exec.isPaused();
84 public boolean canSuspend()
86 return !isTerminated() && !isSuspended();
90 public void suspend() throws DebugException
93 throw new DebugException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, DebugException.TARGET_REQUEST_FAILED,
94 "Can't suspend a terminated MachineProcess", null));
96 throw new DebugException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, DebugException.TARGET_REQUEST_FAILED,
97 "Can't suspend a suspended MachineProcess", null));
99 exec.pauseLiveExecution();
100 fireSuspendEvent(DebugEvent.CLIENT_REQUEST);
104 public boolean canResume()
106 return !isTerminated() && isSuspended();
110 public void resume() throws DebugException
113 throw new DebugException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, DebugException.TARGET_REQUEST_FAILED,
114 "Can't resume a terminated MachineProcess", null));
116 throw new DebugException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, DebugException.TARGET_REQUEST_FAILED,
117 "Can't resume a non-suspended MachineProcess", null));
119 exec.unpauseLiveExecution();
120 fireResumeEvent(DebugEvent.CLIENT_REQUEST);
124 public boolean isTerminated()
130 public boolean canTerminate()
132 return !isTerminated();
136 public void terminate() throws DebugException
141 exec.stopLiveExecution();
143 fireTerminateEvent();
147 public int getExitValue() throws DebugException
150 throw new DebugException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, DebugException.TARGET_REQUEST_FAILED,
151 "Can't get the exit value of a running process", null));
157 public IStreamsProxy getStreamsProxy()
162 private Map<String, String> attributes;
165 public void setAttribute(String key, String value)
167 if (attributes == null)
168 attributes = new HashMap<>(5);
169 Object origVal = attributes.get(key);
170 if (origVal != null && origVal.equals(value))
172 attributes.put(key, value);
177 public String getAttribute(String key)
179 if (attributes == null)
181 return attributes.get(key);
184 @SuppressWarnings("unchecked")
186 public <T> T getAdapter(Class<T> adapter)
188 if (adapter.equals(IProcess.class))
191 if (adapter.equals(IDebugTarget.class))
193 ILaunch launch = getLaunch();
194 IDebugTarget[] targets = launch.getDebugTargets();
195 for (int i = 0; i < targets.length; i++)
196 if (this.equals(targets[i].getProcess()))
197 return (T) targets[i];
201 if (adapter.equals(ILaunch.class))
202 return (T) getLaunch();
204 if (adapter.equals(ILaunchConfiguration.class))
205 return (T) getLaunch().getLaunchConfiguration();
207 return super.getAdapter(adapter);
211 * Fires a creation event.
213 private void fireCreationEvent()
215 fireEvent(new DebugEvent(this, DebugEvent.CREATE));
219 * Fires a change event.
221 private void fireChangeEvent()
223 fireEvent(new DebugEvent(this, DebugEvent.CHANGE));
227 * Fires a resume for this debug element with the specified detail code.
229 * @param detail detail code for the resume event, such as <code>DebugEvent.STEP_OVER</code>
231 private void fireResumeEvent(int detail)
233 fireEvent(new DebugEvent(this, DebugEvent.RESUME, detail));
237 * Fires a suspend event for this debug element with the specified detail code.
239 * @param detail detail code for the suspend event, such as <code>DebugEvent.BREAKPOINT</code>
241 private void fireSuspendEvent(int detail)
243 fireEvent(new DebugEvent(this, DebugEvent.SUSPEND, detail));
247 * Fires a terminate event.
249 private void fireTerminateEvent()
251 fireEvent(new DebugEvent(this, DebugEvent.TERMINATE));
255 * Fires the given debug event.
257 * @param event debug event to fire
259 private static void fireEvent(DebugEvent event)
261 DebugPlugin manager = DebugPlugin.getDefault();
263 manager.fireDebugEventSet(new DebugEvent[] { event });