| 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 | * Error logger. |
|---|
| 51 | */ |
|---|
| 52 | private static Logger simManLogger = Logger.getLogger("tmcsim.simulationmanager"); |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * Enumeration containing property names. |
|---|
| 56 | * |
|---|
| 57 | * @author Matthew Cechini |
|---|
| 58 | */ |
|---|
| 59 | private static enum PROPERTIES |
|---|
| 60 | { |
|---|
| 61 | |
|---|
| 62 | CAD_SIM_HOST("CADSimulatorHost"), |
|---|
| 63 | CAD_SIM_PORT("CADSimulatorRMIPort"), |
|---|
| 64 | SCRIPT_DIR("ScriptDir"), |
|---|
| 65 | FAKE_PARAMICS("FakeParamicsConnection"); |
|---|
| 66 | public String name; |
|---|
| 67 | |
|---|
| 68 | private PROPERTIES(String n) |
|---|
| 69 | { |
|---|
| 70 | name = n; |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | /** |
|---|
| 74 | * Instance of the SimulationManagerModel which communicates with the CAD |
|---|
| 75 | * Simulator to display the current simulation information. This model class |
|---|
| 76 | * contains the data that is displayed by the SimulationManagerView class. |
|---|
| 77 | * The View purely provides a GUI interface for the data contained within |
|---|
| 78 | * the model. |
|---|
| 79 | */ |
|---|
| 80 | SimulationManagerModel theSimManagerModel; |
|---|
| 81 | /** |
|---|
| 82 | * Instance of the SimulationManagerView class which provides a GUI for the |
|---|
| 83 | * user to view the current simulation information and to manage the |
|---|
| 84 | * simulation. The view communicates to the SimulationManagerModel class to |
|---|
| 85 | * get and set data. |
|---|
| 86 | */ |
|---|
| 87 | SimulationManagerView theSimManagerView; |
|---|
| 88 | /** |
|---|
| 89 | * The Properties object for the Simulation Manager. |
|---|
| 90 | */ |
|---|
| 91 | private Properties simManagerProperties; |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 94 | * Constructor. Set communication data members from properties file. |
|---|
| 95 | * Instantiate the SimulationManager Model and View objects, and set |
|---|
| 96 | * visibility to true. |
|---|
| 97 | * |
|---|
| 98 | * @param propertiesFile Properties file containing info for Simulation |
|---|
| 99 | * Manager. |
|---|
| 100 | */ |
|---|
| 101 | public SimulationManager(String propertiesFile) throws SimulationException |
|---|
| 102 | { |
|---|
| 103 | |
|---|
| 104 | try |
|---|
| 105 | { |
|---|
| 106 | simManagerProperties = new Properties(); |
|---|
| 107 | simManagerProperties.load(new FileInputStream(new File(propertiesFile))); |
|---|
| 108 | |
|---|
| 109 | SimulationManagerView.SCRIPT_DIR = |
|---|
| 110 | simManagerProperties.getProperty(PROPERTIES.SCRIPT_DIR.name).trim(); |
|---|
| 111 | |
|---|
| 112 | //make sure properties aren't null |
|---|
| 113 | if (simManagerProperties.getProperty(PROPERTIES.CAD_SIM_HOST.name) == null) |
|---|
| 114 | { |
|---|
| 115 | throw new Exception("CAD Simulator host property is null."); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | if (simManagerProperties.getProperty(PROPERTIES.CAD_SIM_PORT.name) == null) |
|---|
| 119 | { |
|---|
| 120 | throw new Exception("CAD Simulator port property is null."); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | } catch (Exception e) |
|---|
| 124 | { |
|---|
| 125 | simManLogger.logp(Level.SEVERE, "SimulationManager", "Constructor", |
|---|
| 126 | "Exception in reading properties file.", e); |
|---|
| 127 | |
|---|
| 128 | throw new SimulationException(SimulationException.INITIALIZE_ERROR, e); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | //Construct the SimulationManagerModel |
|---|
| 132 | try |
|---|
| 133 | { |
|---|
| 134 | theSimManagerModel = new SimulationManagerModel( |
|---|
| 135 | simManagerProperties.getProperty(PROPERTIES.CAD_SIM_HOST.name).trim(), |
|---|
| 136 | simManagerProperties.getProperty(PROPERTIES.CAD_SIM_PORT.name).trim()); |
|---|
| 137 | |
|---|
| 138 | //Construct the SimulationManagerView and set up the Model-View references. |
|---|
| 139 | theSimManagerView = new SimulationManagerView(theSimManagerModel); |
|---|
| 140 | theSimManagerModel.setView(theSimManagerView); |
|---|
| 141 | } catch (RemoteException re) |
|---|
| 142 | { |
|---|
| 143 | simManLogger.logp(Level.SEVERE, "SimulationManager", "Constructor", |
|---|
| 144 | "Unable to establish RMI ", re); |
|---|
| 145 | |
|---|
| 146 | throw new SimulationException(SimulationException.CAD_SIM_CONNECT, re); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | theSimManagerView.addWindowListener(new WindowListener() |
|---|
| 150 | { |
|---|
| 151 | public void windowClosed(WindowEvent e) |
|---|
| 152 | { |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | public void windowOpened(WindowEvent e) |
|---|
| 156 | { |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | public void windowIconified(WindowEvent e) |
|---|
| 160 | { |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | public void windowDeiconified(WindowEvent e) |
|---|
| 164 | { |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | public void windowActivated(WindowEvent e) |
|---|
| 168 | { |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | public void windowDeactivated(WindowEvent e) |
|---|
| 172 | { |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | public void windowClosing(WindowEvent e) |
|---|
| 176 | { |
|---|
| 177 | theSimManagerModel.disconnect(); |
|---|
| 178 | System.exit(0); |
|---|
| 179 | } |
|---|
| 180 | }); |
|---|
| 181 | |
|---|
| 182 | if (Boolean.parseBoolean(simManagerProperties.getProperty( |
|---|
| 183 | PROPERTIES.FAKE_PARAMICS.name).trim())) |
|---|
| 184 | { |
|---|
| 185 | theSimManagerView.setParamicsStatus(PARAMICS_STATUS.CONNECTED); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | //Show the SimulationManager |
|---|
| 189 | theSimManagerView.setVisible(true); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | /** |
|---|
| 193 | * Load a simulation script from the specified file. |
|---|
| 194 | * |
|---|
| 195 | * @param scriptFile the XML file containing the simulation control script |
|---|
| 196 | * to be run. |
|---|
| 197 | * @throws ScriptException if the script throws an exception |
|---|
| 198 | * @throws SimulationException if the simulation throws an exception |
|---|
| 199 | */ |
|---|
| 200 | public void loadScript(File scriptFile) throws ScriptException, SimulationException |
|---|
| 201 | { |
|---|
| 202 | theSimManagerModel.loadScript(scriptFile); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | /** |
|---|
| 206 | * Main class. |
|---|
| 207 | * |
|---|
| 208 | * @param args Command line arguments. |
|---|
| 209 | */ |
|---|
| 210 | static public void main(String[] args) |
|---|
| 211 | { |
|---|
| 212 | //System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); |
|---|
| 213 | if (System.getProperty("CONFIG_DIR") == null) |
|---|
| 214 | { |
|---|
| 215 | System.setProperty("CONFIG_DIR", "config"); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | try |
|---|
| 219 | { |
|---|
| 220 | UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
|---|
| 221 | new SimulationManager(System.getProperty("CONFIG_DIR") + System.getProperty("file.separator") + CONFIG_FILE_NAME); |
|---|
| 222 | } catch (Exception e) |
|---|
| 223 | { |
|---|
| 224 | simManLogger.logp(Level.SEVERE, "SimulationManager", "Main", |
|---|
| 225 | "Error occured initializing application", e); |
|---|
| 226 | |
|---|
| 227 | JOptionPane.showMessageDialog(null, e.getMessage(), |
|---|
| 228 | "Error - Program Exiting", JOptionPane.ERROR_MESSAGE); |
|---|
| 229 | |
|---|
| 230 | System.exit(-1); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | } |
|---|
| 234 | } |
|---|