| 1 | package tmcsim.simulationmanager; |
|---|
| 2 | |
|---|
| 3 | import java.awt.event.WindowEvent; |
|---|
| 4 | import java.awt.event.WindowListener; |
|---|
| 5 | import java.io.File; |
|---|
| 6 | import java.io.FileInputStream; |
|---|
| 7 | import java.rmi.RemoteException; |
|---|
| 8 | import java.util.Properties; |
|---|
| 9 | import java.util.logging.Level; |
|---|
| 10 | import java.util.logging.Logger; |
|---|
| 11 | |
|---|
| 12 | import javax.swing.JOptionPane; |
|---|
| 13 | import javax.swing.UIManager; |
|---|
| 14 | |
|---|
| 15 | import tmcsim.common.SimulationException; |
|---|
| 16 | import tmcsim.common.CADEnums.PARAMICS_STATUS; |
|---|
| 17 | |
|---|
| 18 | /** |
|---|
| 19 | * Simulation Manager is the main class for this module. The Simulation Manager |
|---|
| 20 | * is used to control the view and control the simulation. Simulation incidents |
|---|
| 21 | * are loaded, removed, reschedule, and added from the Simulation Manager. The |
|---|
| 22 | * Simulation Manager provides functionality for connecting to the paramics |
|---|
| 23 | * communicator and applying diversions. A history of all events is shown as well.<br> |
|---|
| 24 | * The SimulationManager may be started at any point before, during, or after |
|---|
| 25 | * a simulation has begun. The SimulationManager connects to the CADSimulator |
|---|
| 26 | * and communicates through Java RMI methods. If two SimulationManagers are started, |
|---|
| 27 | * the second one started, chronologically, will receive communication from the |
|---|
| 28 | * CADSimulator. <br><br> |
|---|
| 29 | * The properties file for the SimulationManager class contains the following data.<br> |
|---|
| 30 | * <code> |
|---|
| 31 | * -----------------------------------------------------------------------------<br> |
|---|
| 32 | * Host Name The host name where the CADSimulator is located.<br> |
|---|
| 33 | * Error File The target file to use for error logging.<br> |
|---|
| 34 | * -----------------------------------------------------------------------------<br> |
|---|
| 35 | * Example File: <br> |
|---|
| 36 | * CADSimulatorHost = localhost <br> |
|---|
| 37 | * ErrorFile = sim_mgr_error.xml <br> |
|---|
| 38 | * -----------------------------------------------------------------------------<br> |
|---|
| 39 | * </code> |
|---|
| 40 | * |
|---|
| 41 | * @author Matthew Cechini (mcechini@calpoly.edu) |
|---|
| 42 | * @version $Date: 2009/04/17 16:27:47 $ $Revision: 1.7 |
|---|
| 43 | */ |
|---|
| 44 | public class SimulationManager { |
|---|
| 45 | |
|---|
| 46 | /** Error logger. */ |
|---|
| 47 | private static Logger simManLogger = Logger.getLogger("tmcsim.simulationmanager"); |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * Enumeration containing property names. |
|---|
| 51 | * @author Matthew Cechini |
|---|
| 52 | */ |
|---|
| 53 | private static enum PROPERTIES { |
|---|
| 54 | CAD_SIM_HOST ("CADSimulatorHost"), |
|---|
| 55 | CAD_SIM_PORT ("CADSimulatorRMIPort"), |
|---|
| 56 | SCRIPT_DIR ("ScriptDir"), |
|---|
| 57 | FAKE_PARAMICS ("FakeParamicsConnection"); |
|---|
| 58 | |
|---|
| 59 | public String name; |
|---|
| 60 | |
|---|
| 61 | private PROPERTIES(String n) { |
|---|
| 62 | name = n; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | /** |
|---|
| 68 | * Instance of the SimulationManagerModel which communicates with the |
|---|
| 69 | * CAD Simulator to display the current simulation information. This |
|---|
| 70 | * model class contains the data that is displayed by the SimulationManagerView |
|---|
| 71 | * class. The View purely provides a GUI interface for the data contained within |
|---|
| 72 | * the model. |
|---|
| 73 | */ |
|---|
| 74 | private SimulationManagerModel theSimManagerModel; |
|---|
| 75 | |
|---|
| 76 | /** |
|---|
| 77 | * Instance of the SimulationManagerView class which provides a GUI for the user |
|---|
| 78 | * to view the current simulation information and to manage the simulation. The |
|---|
| 79 | * view communicates to the SimulationManagerModel class to get and set data. |
|---|
| 80 | */ |
|---|
| 81 | SimulationManagerView theSimManagerView; |
|---|
| 82 | |
|---|
| 83 | /** The Properties object for the Simulation Manager. */ |
|---|
| 84 | private Properties simManagerProperties; |
|---|
| 85 | |
|---|
| 86 | /** |
|---|
| 87 | * Constructor. Set communication data members from properties file. Instantiate |
|---|
| 88 | * the SimulationManager Model and View objects, and set visibility to true. |
|---|
| 89 | * |
|---|
| 90 | * @param propertiesFile Properties file containing info for Simulation Manager. |
|---|
| 91 | */ |
|---|
| 92 | public SimulationManager(String propertiesFile) throws SimulationException { |
|---|
| 93 | |
|---|
| 94 | try { |
|---|
| 95 | simManagerProperties = new Properties(); |
|---|
| 96 | simManagerProperties.load(new FileInputStream(new File(propertiesFile))); |
|---|
| 97 | |
|---|
| 98 | SimulationManagerView.SCRIPT_DIR = |
|---|
| 99 | simManagerProperties.getProperty(PROPERTIES.SCRIPT_DIR.name).trim(); |
|---|
| 100 | |
|---|
| 101 | //make sure properties aren't null |
|---|
| 102 | if(simManagerProperties.getProperty(PROPERTIES.CAD_SIM_HOST.name) == null) |
|---|
| 103 | throw new Exception("CAD Simulator host property is null."); |
|---|
| 104 | |
|---|
| 105 | if(simManagerProperties.getProperty(PROPERTIES.CAD_SIM_PORT.name) == null) |
|---|
| 106 | throw new Exception("CAD Simulator port property is null."); |
|---|
| 107 | |
|---|
| 108 | } |
|---|
| 109 | catch (Exception e) |
|---|
| 110 | { |
|---|
| 111 | simManLogger.logp(Level.SEVERE, "SimulationManager", "Constructor", |
|---|
| 112 | "Exception in reading properties file.", e); |
|---|
| 113 | |
|---|
| 114 | throw new SimulationException(SimulationException.INITIALIZE_ERROR, e); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | //Construct the SimulationManagerModel |
|---|
| 118 | try |
|---|
| 119 | { |
|---|
| 120 | theSimManagerModel = new SimulationManagerModel( |
|---|
| 121 | simManagerProperties.getProperty(PROPERTIES.CAD_SIM_HOST.name).trim(), |
|---|
| 122 | simManagerProperties.getProperty(PROPERTIES.CAD_SIM_PORT.name).trim()); |
|---|
| 123 | |
|---|
| 124 | //Construct the SimulationManagerView and set up the Model-View references. |
|---|
| 125 | theSimManagerView = new SimulationManagerView(theSimManagerModel); |
|---|
| 126 | theSimManagerModel.setView(theSimManagerView); |
|---|
| 127 | } |
|---|
| 128 | catch (RemoteException re) |
|---|
| 129 | { |
|---|
| 130 | simManLogger.logp(Level.SEVERE, "SimulationManager", "Constructor", |
|---|
| 131 | "Unable to establish RMI ", re); |
|---|
| 132 | |
|---|
| 133 | throw new SimulationException(SimulationException.CAD_SIM_CONNECT, re); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | theSimManagerView.addWindowListener(new WindowListener() { |
|---|
| 137 | public void windowClosed(WindowEvent e) {} |
|---|
| 138 | public void windowOpened(WindowEvent e) {} |
|---|
| 139 | public void windowIconified(WindowEvent e) {} |
|---|
| 140 | public void windowDeiconified(WindowEvent e) {} |
|---|
| 141 | public void windowActivated(WindowEvent e) {} |
|---|
| 142 | public void windowDeactivated(WindowEvent e) {} |
|---|
| 143 | public void windowClosing(WindowEvent e) { |
|---|
| 144 | theSimManagerModel.disconnect(); |
|---|
| 145 | System.exit(0); |
|---|
| 146 | } |
|---|
| 147 | }); |
|---|
| 148 | |
|---|
| 149 | if(Boolean.parseBoolean(simManagerProperties.getProperty( |
|---|
| 150 | PROPERTIES.FAKE_PARAMICS.name).trim())) |
|---|
| 151 | { |
|---|
| 152 | theSimManagerView.setParamicsStatus(PARAMICS_STATUS.CONNECTED); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | //Show the SimulationManager |
|---|
| 156 | theSimManagerView.setVisible(true); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | /** |
|---|
| 160 | * Main class. |
|---|
| 161 | * |
|---|
| 162 | * @param args Command line arguments. |
|---|
| 163 | */ |
|---|
| 164 | static public void main(String[] args) { |
|---|
| 165 | System.setProperty("SIM_MGR_PROPERTIES", "config/sim_manager_config.properties"); |
|---|
| 166 | |
|---|
| 167 | try { |
|---|
| 168 | if(System.getProperty("SIM_MGR_PROPERTIES") != null) |
|---|
| 169 | { |
|---|
| 170 | UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
|---|
| 171 | |
|---|
| 172 | new SimulationManager(System.getProperty("SIM_MGR_PROPERTIES")); |
|---|
| 173 | } |
|---|
| 174 | else |
|---|
| 175 | { |
|---|
| 176 | throw new Exception ("SIM_MGR_PROPERTIES system property not defined."); |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | catch (Exception e) |
|---|
| 180 | { |
|---|
| 181 | simManLogger.logp(Level.SEVERE, "SimulationManager", "Main", |
|---|
| 182 | "Error occured initializing application", e); |
|---|
| 183 | |
|---|
| 184 | JOptionPane.showMessageDialog(null, e.getMessage(), |
|---|
| 185 | "Error - Program Exiting", JOptionPane.ERROR_MESSAGE); |
|---|
| 186 | |
|---|
| 187 | System.exit(-1); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|