| 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 | import javax.swing.JOptionPane; |
|---|
| 12 | import javax.swing.UIManager; |
|---|
| 13 | import tmcsim.common.CADEnums.PARAMICS_STATUS; |
|---|
| 14 | import tmcsim.common.ScriptException; |
|---|
| 15 | import tmcsim.common.SimulationException; |
|---|
| 16 | |
|---|
| 17 | /** |
|---|
| 18 | * Simulation Manager is the main class for this module. The Simulation Manager |
|---|
| 19 | * is used to control the view and control the simulation. Simulation incidents |
|---|
| 20 | * are loaded, removed, reschedule, and added from the Simulation Manager. The |
|---|
| 21 | * Simulation Manager provides functionality for connecting to the paramics |
|---|
| 22 | * communicator and applying diversions. A history of all events is shown as |
|---|
| 23 | * well.<br> |
|---|
| 24 | * The SimulationManager may be started at any point before, during, or after a |
|---|
| 25 | * simulation has begun. The SimulationManager connects to the CADSimulator and |
|---|
| 26 | * 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 |
|---|
| 30 | * data.<br> |
|---|
| 31 | * <code> |
|---|
| 32 | * -----------------------------------------------------------------------------<br> |
|---|
| 33 | * Host Name The host name where the CADSimulator is located.<br> |
|---|
| 34 | * Error File The target file to use for error logging.<br> |
|---|
| 35 | * -----------------------------------------------------------------------------<br> |
|---|
| 36 | * Example File: <br> |
|---|
| 37 | * CADSimulatorHost = localhost <br> |
|---|
| 38 | * ErrorFile = sim_mgr_error.xml <br> |
|---|
| 39 | * -----------------------------------------------------------------------------<br> |
|---|
| 40 | * </code> |
|---|
| 41 | * |
|---|
| 42 | * @author Matthew Cechini (mcechini@calpoly.edu) |
|---|
| 43 | * @version $Date: 2009/04/17 16:27:47 $ $Revision: 1.7 |
|---|
| 44 | */ |
|---|
| 45 | public class SimulationManager |
|---|
| 46 | { |
|---|
| 47 | |
|---|
| 48 | private static final String CONFIG_FILE_NAME = "sim_manager_config.properties"; |
|---|
| 49 | /* |
|---|
| 50 | * Name of filesystem folder that contains Scenario xml files. |
|---|
| 51 | */ |
|---|
| 52 | public static final String kScenarioFolder = "Scenarios"; |
|---|
| 53 | /** |
|---|
| 54 | * Error logger. |
|---|
| 55 | */ |
|---|
| 56 | private static Logger simManLogger = Logger.getLogger("tmcsim.simulationmanager"); |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * Enumeration containing property names. |
|---|
| 60 | * |
|---|
| 61 | * @author Matthew Cechini |
|---|
| 62 | */ |
|---|
| 63 | private static enum PROPERTIES |
|---|
| 64 | { |
|---|
| 65 | |
|---|
| 66 | CAD_SIM_HOST("CADSimulatorHost"), |
|---|
| 67 | CAD_SIM_PORT("CADSimulatorRMIPort"), |
|---|
| 68 | SCRIPT_DIR("ScriptDir"), |
|---|
| 69 | FAKE_PARAMICS("FakeParamicsConnection"); |
|---|
| 70 | public String name; |
|---|
| 71 | |
|---|
| 72 | private PROPERTIES(String n) |
|---|
| 73 | { |
|---|
| 74 | name = n; |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | /** |
|---|
| 78 | * Instance of the SimulationManagerModel which communicates with the CAD |
|---|
| 79 | * Simulator to display the current simulation information. This model class |
|---|
| 80 | * contains the data that is displayed by the SimulationManagerView class. |
|---|
| 81 | * The View purely provides a GUI interface for the data contained within |
|---|
| 82 | * the model. |
|---|
| 83 | */ |
|---|
| 84 | SimulationManagerModel theSimManagerModel; |
|---|
| 85 | /** |
|---|
| 86 | * Instance of the SimulationManagerView class which provides a GUI for the |
|---|
| 87 | * user to view the current simulation information and to manage the |
|---|
| 88 | * simulation. The view communicates to the SimulationManagerModel class to |
|---|
| 89 | * get and set data. |
|---|
| 90 | */ |
|---|
| 91 | SimulationManagerView theSimManagerView; |
|---|
| 92 | /** |
|---|
| 93 | * The Properties object for the Simulation Manager. |
|---|
| 94 | */ |
|---|
| 95 | private Properties simManagerProperties; |
|---|
| 96 | |
|---|
| 97 | /** |
|---|
| 98 | * Constructor. Set communication data members from properties file. |
|---|
| 99 | * Instantiate the SimulationManager Model and View objects, and set |
|---|
| 100 | * visibility to true. |
|---|
| 101 | * |
|---|
| 102 | * @param propertiesFile Properties file containing info for Simulation |
|---|
| 103 | * Manager. |
|---|
| 104 | */ |
|---|
| 105 | public SimulationManager(String propertiesFile) throws SimulationException |
|---|
| 106 | { |
|---|
| 107 | |
|---|
| 108 | try |
|---|
| 109 | { |
|---|
| 110 | simManagerProperties = new Properties(); |
|---|
| 111 | simManagerProperties.load(new FileInputStream(new File(propertiesFile))); |
|---|
| 112 | |
|---|
| 113 | SimulationManagerView.SCRIPT_DIR = |
|---|
| 114 | simManagerProperties.getProperty(PROPERTIES.SCRIPT_DIR.name).trim(); |
|---|
| 115 | |
|---|
| 116 | //make sure properties aren't null |
|---|
| 117 | if (simManagerProperties.getProperty(PROPERTIES.CAD_SIM_HOST.name) == null) |
|---|
| 118 | { |
|---|
| 119 | throw new Exception("CAD Simulator host property is null."); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | if (simManagerProperties.getProperty(PROPERTIES.CAD_SIM_PORT.name) == null) |
|---|
| 123 | { |
|---|
| 124 | throw new Exception("CAD Simulator port property is null."); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | } catch (Exception e) |
|---|
| 128 | { |
|---|
| 129 | simManLogger.logp(Level.SEVERE, "SimulationManager", "Constructor", |
|---|
| 130 | "Exception in reading properties file.", e); |
|---|
| 131 | |
|---|
| 132 | throw new SimulationException(SimulationException.INITIALIZE_ERROR, e); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | //Construct the SimulationManagerModel |
|---|
| 136 | try |
|---|
| 137 | { |
|---|
| 138 | theSimManagerModel = new SimulationManagerModel( |
|---|
| 139 | simManagerProperties.getProperty(PROPERTIES.CAD_SIM_HOST.name).trim(), |
|---|
| 140 | simManagerProperties.getProperty(PROPERTIES.CAD_SIM_PORT.name).trim()); |
|---|
| 141 | |
|---|
| 142 | //Construct the SimulationManagerView and set up the Model-View references. |
|---|
| 143 | theSimManagerView = new SimulationManagerView(theSimManagerModel); |
|---|
| 144 | theSimManagerModel.setView(theSimManagerView); |
|---|
| 145 | } catch (RemoteException re) |
|---|
| 146 | { |
|---|
| 147 | simManLogger.logp(Level.SEVERE, "SimulationManager", "Constructor", |
|---|
| 148 | "Unable to establish RMI ", re); |
|---|
| 149 | |
|---|
| 150 | throw new SimulationException(SimulationException.CAD_SIM_CONNECT, re); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | theSimManagerView.addWindowListener(new WindowListener() |
|---|
| 154 | { |
|---|
| 155 | public void windowClosed(WindowEvent e) |
|---|
| 156 | { |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | public void windowOpened(WindowEvent e) |
|---|
| 160 | { |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | public void windowIconified(WindowEvent e) |
|---|
| 164 | { |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | public void windowDeiconified(WindowEvent e) |
|---|
| 168 | { |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | public void windowActivated(WindowEvent e) |
|---|
| 172 | { |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | public void windowDeactivated(WindowEvent e) |
|---|
| 176 | { |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | public void windowClosing(WindowEvent e) |
|---|
| 180 | { |
|---|
| 181 | theSimManagerModel.disconnect(); |
|---|
| 182 | System.exit(0); |
|---|
| 183 | } |
|---|
| 184 | }); |
|---|
| 185 | |
|---|
| 186 | if (Boolean.parseBoolean(simManagerProperties.getProperty( |
|---|
| 187 | PROPERTIES.FAKE_PARAMICS.name).trim())) |
|---|
| 188 | { |
|---|
| 189 | theSimManagerView.setParamicsStatus(PARAMICS_STATUS.CONNECTED); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | //Show the SimulationManager |
|---|
| 193 | theSimManagerView.setVisible(true); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | /** |
|---|
| 197 | * Load a simulation script from the specified file. |
|---|
| 198 | * |
|---|
| 199 | * @param scriptFile the XML file containing the simulation control script |
|---|
| 200 | * to be run. |
|---|
| 201 | * @throws ScriptException if the script throws an exception |
|---|
| 202 | * @throws SimulationException if the simulation throws an exception |
|---|
| 203 | */ |
|---|
| 204 | public void loadScript(File scriptFile) throws ScriptException, SimulationException |
|---|
| 205 | { |
|---|
| 206 | theSimManagerModel.loadScript(scriptFile); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | /** |
|---|
| 210 | * Main class. |
|---|
| 211 | * |
|---|
| 212 | * @param args Command line arguments. |
|---|
| 213 | */ |
|---|
| 214 | static public void main(String[] args) |
|---|
| 215 | { |
|---|
| 216 | //System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); |
|---|
| 217 | if (System.getProperty("CONFIG_DIR") == null) |
|---|
| 218 | { |
|---|
| 219 | System.setProperty("CONFIG_DIR", "config"); |
|---|
| 220 | } |
|---|
| 221 | SimulationManager app; |
|---|
| 222 | try |
|---|
| 223 | { |
|---|
| 224 | UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
|---|
| 225 | app = new SimulationManager(System.getProperty("CONFIG_DIR") + System.getProperty("file.separator") + CONFIG_FILE_NAME); |
|---|
| 226 | // Check if a script filename has been given as an argument |
|---|
| 227 | String arg1; |
|---|
| 228 | if (args.length > 0) |
|---|
| 229 | { |
|---|
| 230 | arg1 = args[0]; |
|---|
| 231 | File scriptFile = new File(kScenarioFolder+ System.getProperty("file.separator") + arg1); |
|---|
| 232 | // if the script file exists, load it and show name in title bar |
|---|
| 233 | if (scriptFile.exists()) |
|---|
| 234 | { |
|---|
| 235 | app.loadScript(scriptFile); |
|---|
| 236 | // Implement ticket #197 |
|---|
| 237 | app.theSimManagerView.setTitle("Simulation Manager: " + arg1); |
|---|
| 238 | simManLogger.logp(Level.INFO,"SimulationManager","Main", |
|---|
| 239 | "Sim Mgr starting with scenario file: " + arg1 + "."); |
|---|
| 240 | } |
|---|
| 241 | else |
|---|
| 242 | { |
|---|
| 243 | simManLogger.logp(Level.INFO,"SimulationManager","Main", |
|---|
| 244 | "Scenario file not found: " + arg1 + |
|---|
| 245 | ". Starting with no scenario."); |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | } catch (Exception e) |
|---|
| 249 | { |
|---|
| 250 | simManLogger.logp(Level.SEVERE, "SimulationManager", "Main", |
|---|
| 251 | "Error occured initializing application", e); |
|---|
| 252 | |
|---|
| 253 | JOptionPane.showMessageDialog(null, e.getMessage(), |
|---|
| 254 | "Error - Program Exiting", JOptionPane.ERROR_MESSAGE); |
|---|
| 255 | |
|---|
| 256 | System.exit(-1); |
|---|
| 257 | } |
|---|
| 258 | } |
|---|
| 259 | } |
|---|