| 1 | package tmcsim.cadsimulator.managers; |
|---|
| 2 | |
|---|
| 3 | import java.rmi.RemoteException; |
|---|
| 4 | import java.util.Timer; |
|---|
| 5 | import java.util.TimerTask; |
|---|
| 6 | |
|---|
| 7 | import tmcsim.cadsimulator.Coordinator; |
|---|
| 8 | import tmcsim.common.CADEnums.SCRIPT_STATUS; |
|---|
| 9 | |
|---|
| 10 | public class SimulationControlManager { |
|---|
| 11 | |
|---|
| 12 | /** |
|---|
| 13 | * |
|---|
| 14 | */ |
|---|
| 15 | private class ClockTask extends TimerTask { |
|---|
| 16 | public void run() { |
|---|
| 17 | currentSimTime++; |
|---|
| 18 | theCoordinator.tick(); |
|---|
| 19 | } |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | /** The SimulationTimer used to keep track of simulation time. */ |
|---|
| 23 | private Timer simTimer = null; |
|---|
| 24 | |
|---|
| 25 | /** */ |
|---|
| 26 | private Coordinator theCoordinator; |
|---|
| 27 | |
|---|
| 28 | /** |
|---|
| 29 | * Boolean flag to designate whether the simulation has been started or not. |
|---|
| 30 | */ |
|---|
| 31 | private boolean simulationStarted; |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Object used to count the number of seconds that the simulation has run. |
|---|
| 35 | * The value is initialized to 0, and is reset to 0 every time a new simulation |
|---|
| 36 | * is loaded, or the current simulation is stopped and reset. |
|---|
| 37 | */ |
|---|
| 38 | private long currentSimTime; |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | public SimulationControlManager(Coordinator coor) { |
|---|
| 42 | |
|---|
| 43 | theCoordinator = coor; |
|---|
| 44 | simulationStarted = false; |
|---|
| 45 | currentSimTime = 0; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | public boolean simulationStarted() { |
|---|
| 49 | return simulationStarted; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | public long getCurrentSimTime() { |
|---|
| 53 | return currentSimTime; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | public void gotoSimulationTime(long newSimTime) { |
|---|
| 57 | currentSimTime = newSimTime; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | public void startSimulation() { |
|---|
| 61 | |
|---|
| 62 | simTimer = new Timer(); |
|---|
| 63 | simTimer.scheduleAtFixedRate(new ClockTask(), 0, 1000); |
|---|
| 64 | |
|---|
| 65 | simulationStarted = true; |
|---|
| 66 | |
|---|
| 67 | theCoordinator.setScriptStatus(SCRIPT_STATUS.SCRIPT_RUNNING); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | public void pauseSimulation() { |
|---|
| 71 | |
|---|
| 72 | if(simTimer != null) |
|---|
| 73 | simTimer.cancel(); |
|---|
| 74 | |
|---|
| 75 | simulationStarted = false; |
|---|
| 76 | |
|---|
| 77 | theCoordinator.setScriptStatus(SCRIPT_STATUS.SCRIPT_PAUSED_STARTED); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | public void resetSimulation() throws RemoteException { |
|---|
| 81 | |
|---|
| 82 | if(simTimer != null) |
|---|
| 83 | simTimer.cancel(); |
|---|
| 84 | |
|---|
| 85 | currentSimTime = 0; |
|---|
| 86 | |
|---|
| 87 | theCoordinator.setScriptStatus(SCRIPT_STATUS.SCRIPT_STOPPED_NOT_STARTED); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | } |
|---|