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 throwDebugException("Can't suspend a terminated MachineProcess");
95 throwDebugException("Can't suspend a suspended MachineProcess");
97 exec.pauseLiveExecution();
98 fireSuspendEvent(DebugEvent.CLIENT_REQUEST);
102 public boolean canResume()
104 return !isTerminated() && isSuspended();
108 public void resume() throws DebugException
111 throwDebugException("Can't resume a terminated MachineProcess");
113 throwDebugException("Can't resume a non-suspended MachineProcess");
115 exec.unpauseLiveExecution();
116 fireResumeEvent(DebugEvent.CLIENT_REQUEST);
120 public boolean isTerminated()
126 public boolean canTerminate()
128 return !isTerminated();
132 public void terminate() throws DebugException
137 exec.stopLiveExecution();
139 fireTerminateEvent();
143 public int getExitValue() throws DebugException
146 throwDebugException("Can't get the exit value of a running process");
152 public IStreamsProxy getStreamsProxy()
157 private Map<String, String> attributes;
160 public void setAttribute(String key, String value)
162 if (attributes == null)
163 attributes = new HashMap<>(5);
164 Object origVal = attributes.get(key);
165 if (origVal != null && origVal.equals(value))
167 attributes.put(key, value);
172 public String getAttribute(String key)
174 if (attributes == null)
176 return attributes.get(key);
179 @SuppressWarnings("unchecked")
181 public <T> T getAdapter(Class<T> adapter)
183 if (adapter.equals(IProcess.class))
186 if (adapter.equals(IDebugTarget.class))
188 ILaunch launch = getLaunch();
189 IDebugTarget[] targets = launch.getDebugTargets();
190 for (int i = 0; i < targets.length; i++)
191 if (this.equals(targets[i].getProcess()))
192 return (T) targets[i];
196 if (adapter.equals(ILaunch.class))
197 return (T) getLaunch();
199 if (adapter.equals(ILaunchConfiguration.class))
200 return (T) getLaunch().getLaunchConfiguration();
202 return super.getAdapter(adapter);
206 * Fires a creation event.
208 private void fireCreationEvent()
210 fireEvent(new DebugEvent(this, DebugEvent.CREATE));
214 * Fires a change event.
216 private void fireChangeEvent()
218 fireEvent(new DebugEvent(this, DebugEvent.CHANGE));
222 * Fires a resume for this debug element with the specified detail code.
224 * @param detail detail code for the resume event, such as <code>DebugEvent.STEP_OVER</code>
226 private void fireResumeEvent(int detail)
228 fireEvent(new DebugEvent(this, DebugEvent.RESUME, detail));
232 * Fires a suspend event for this debug element with the specified detail code.
234 * @param detail detail code for the suspend event, such as <code>DebugEvent.BREAKPOINT</code>
236 private void fireSuspendEvent(int detail)
238 fireEvent(new DebugEvent(this, DebugEvent.SUSPEND, detail));
242 * Fires a terminate event.
244 private void fireTerminateEvent()
246 fireEvent(new DebugEvent(this, DebugEvent.TERMINATE));
250 * Fires the given debug event.
252 * @param event debug event to fire
254 private static void fireEvent(DebugEvent event)
256 DebugPlugin manager = DebugPlugin.getDefault();
258 manager.fireDebugEventSet(new DebugEvent[] { event });
261 private static void throwDebugException(String message) throws DebugException
263 throw new DebugException(
264 new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, DebugException.TARGET_REQUEST_FAILED, message, null));