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