| 1 | package tmcsim.cadsimulator; |
|---|
| 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.Naming; |
|---|
| 8 | import java.rmi.RemoteException; |
|---|
| 9 | import java.rmi.registry.LocateRegistry; |
|---|
| 10 | import java.util.Calendar; |
|---|
| 11 | import java.util.Properties; |
|---|
| 12 | import java.util.logging.FileHandler; |
|---|
| 13 | import java.util.logging.Level; |
|---|
| 14 | import java.util.logging.Logger; |
|---|
| 15 | |
|---|
| 16 | import javax.swing.JOptionPane; |
|---|
| 17 | import javax.swing.JWindow; |
|---|
| 18 | import javax.swing.UIManager; |
|---|
| 19 | import javax.xml.parsers.DocumentBuilderFactory; |
|---|
| 20 | |
|---|
| 21 | import tmcsim.cadsimulator.db.CMSDiversionDB; |
|---|
| 22 | import tmcsim.cadsimulator.managers.ATMSManager; |
|---|
| 23 | import tmcsim.cadsimulator.managers.IncidentManager; |
|---|
| 24 | import tmcsim.cadsimulator.managers.MediaManager; |
|---|
| 25 | import tmcsim.cadsimulator.managers.ParamicsSimulationManager; |
|---|
| 26 | import tmcsim.cadsimulator.managers.SimulationControlManager; |
|---|
| 27 | import tmcsim.cadsimulator.viewer.CADSimulatorViewer; |
|---|
| 28 | import tmcsim.client.cadclientgui.data.CADData; |
|---|
| 29 | import tmcsim.common.SimulationException; |
|---|
| 30 | import tmcsim.simulationmanager.SimulationManager; |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * CADSimulator is main class for the CAD Simulator application. At |
|---|
| 35 | * construction the Coordinator, CoordinatorViewer, and all CAD Simulator |
|---|
| 36 | * Managers are initialized and data relationships are established. |
|---|
| 37 | * Simulation control is managed through the Coordinator and Managers. |
|---|
| 38 | * The CADSimulator contains the instances of all Manager Objects that are used |
|---|
| 39 | * to control the Simulation flow of data.<br> |
|---|
| 40 | * <br> |
|---|
| 41 | * |
|---|
| 42 | * The CADSimulator is initialized with a properties file containing the |
|---|
| 43 | * following data items:<br> |
|---|
| 44 | * <code> |
|---|
| 45 | * -----------------------------------------------------------------------------<br> |
|---|
| 46 | * CADClientPort The port number to use for remote CAD Client connections.<br> |
|---|
| 47 | * CoordinatorRMIPort The port number to use for binding the Coordinator.<br> |
|---|
| 48 | * CMSDiversionXML The filepath for the xml file containing initialization data for the Diversion "database." |
|---|
| 49 | * AudioFileLocation The root directory path where audio files are referenced from.<br> |
|---|
| 50 | * ParamicsProperties The filepath for the properties file to initialize the ParamicsControlManager.<br> |
|---|
| 51 | * ATMSProperties The filepath for the properties file to initialize the ATMSManager.<br> |
|---|
| 52 | * MediaProperties The filepath for the properties file to initialize the MediaManager.<br> |
|---|
| 53 | * ErrorFile The filename of the error file used for logging errors.<br> |
|---|
| 54 | * ----------------------------------------------------------------------------<br> |
|---|
| 55 | * Example File:<br> |
|---|
| 56 | * CADClientPort = 4444<br> |
|---|
| 57 | * CoordinatorRMIPort = 4445<br> |
|---|
| 58 | * CMSDiversionXML = ../data/cmsdiversions.xml<br> |
|---|
| 59 | * AudioFileLocation = ../audio/<br> |
|---|
| 60 | * ParamicsProperties = ../config/paramics.properties<br> |
|---|
| 61 | * ATMSProperties = ../config/atms.properties<br> |
|---|
| 62 | * MediaProperties = ../config/media.properties<br> |
|---|
| 63 | * ErrorFile = cad_sim_error.xml<br> |
|---|
| 64 | * </code> |
|---|
| 65 | * |
|---|
| 66 | * @author Matthew Cechini (mcechini@calpoly.edu) |
|---|
| 67 | * @version $Date: 2009/04/17 16:27:46 $ $Revision: 1.5 $ |
|---|
| 68 | */ |
|---|
| 69 | public class CADSimulator { |
|---|
| 70 | |
|---|
| 71 | /** Error logger. */ |
|---|
| 72 | private static Logger cadSimLogger = Logger.getLogger("tmcsim.cadsimulator"); |
|---|
| 73 | |
|---|
| 74 | /** |
|---|
| 75 | * Enumeration containing properties name values. See CADSimulator class |
|---|
| 76 | * description for more information. |
|---|
| 77 | * @author Matthew |
|---|
| 78 | * @see CADSimulator |
|---|
| 79 | */ |
|---|
| 80 | private static enum CAD_PROPERTIES { |
|---|
| 81 | /** RMI port to accept CAD Client connections. */ |
|---|
| 82 | CLIENT_PORT ("CADClientPort"), |
|---|
| 83 | /** RMI port to bind the Coordinator to for RMI communication. */ |
|---|
| 84 | COOR_RMI_PORT ("CoordinatorRMIPort"), |
|---|
| 85 | |
|---|
| 86 | CAD_RMI_PORT ("CADRmiPort"), |
|---|
| 87 | /** Filepath for xml file containing diversion data. */ |
|---|
| 88 | CMS_XML_FILE ("CMSDiversionXML"), |
|---|
| 89 | /** Filepath for xml file containing dvd control data. */ |
|---|
| 90 | DVD_XML_FILE ("DVDPlayerXML"), |
|---|
| 91 | /** Filepath for xml file containing still image control data. */ |
|---|
| 92 | IMAGE_XML_FILE ("StillImagesXML"), |
|---|
| 93 | /** Root directory path where audio files are referenced from. */ |
|---|
| 94 | AUDIO_LOCATION ("AudioFileLocation"), |
|---|
| 95 | /** Filepath for the properties file to initialize the media manager. */ |
|---|
| 96 | MEDIA_PROP_FILE ("MediaProperties"), |
|---|
| 97 | /** Filepath for the properties file to initialize the paramics control manager. */ |
|---|
| 98 | PARAMICS_PROP_FILE ("ParamicsProperties"), |
|---|
| 99 | /** Filepath for the properties file to initialize the atms manager. */ |
|---|
| 100 | ATMS_PROP_FILE ("ATMSProperties"); |
|---|
| 101 | |
|---|
| 102 | public String name; |
|---|
| 103 | |
|---|
| 104 | private CAD_PROPERTIES(String n) { |
|---|
| 105 | name = n; |
|---|
| 106 | } |
|---|
| 107 | }; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | /** CADSimulatorViewer instance. */ |
|---|
| 111 | protected static CADSimulatorViewer theViewer; |
|---|
| 112 | |
|---|
| 113 | /** Coordinator instance. */ |
|---|
| 114 | protected static Coordinator theCoordinator; |
|---|
| 115 | |
|---|
| 116 | /** SoundPlayer instance. */ |
|---|
| 117 | protected static SoundPlayer theSoundPlayer = null; |
|---|
| 118 | |
|---|
| 119 | /** SimulationControlManager instance. */ |
|---|
| 120 | protected static SimulationControlManager theSimulationCntrlMgr = null; |
|---|
| 121 | |
|---|
| 122 | /** ParamicsSimulationManager instance. */ |
|---|
| 123 | protected static ParamicsSimulationManager theParamicsSimMgr = null; |
|---|
| 124 | |
|---|
| 125 | /** IncidentManager instance. */ |
|---|
| 126 | protected static IncidentManager theIncidentMgr = null; |
|---|
| 127 | |
|---|
| 128 | /** MediaManager instance. */ |
|---|
| 129 | protected static MediaManager theMediaMgr = null; |
|---|
| 130 | |
|---|
| 131 | /** ATMSManager instance. */ |
|---|
| 132 | protected static ATMSManager theATMSMgr = null; |
|---|
| 133 | |
|---|
| 134 | /** Properties file for the CADSimulator. */ |
|---|
| 135 | private Properties cadSimulatorProperties; |
|---|
| 136 | |
|---|
| 137 | private static final String CONFIG_FILE_NAME = "cad_simulator_config.properties"; |
|---|
| 138 | |
|---|
| 139 | /** |
|---|
| 140 | * Constructor. Load the Properties file and initialize all CAD Simulator |
|---|
| 141 | * Managers and establish Manager data relationships. A |
|---|
| 142 | * CADSimulatorSocketHandler is instantiated and started to being |
|---|
| 143 | * listening for remote CAD connections. The CMSDiversionDB is initialized |
|---|
| 144 | * with the XML data(incomplete design). |
|---|
| 145 | * |
|---|
| 146 | * @param propertiesFile Filename of CAD Simulator properties file. |
|---|
| 147 | * @throws SimulationException if there is an error in initializing the CAD Simulator. |
|---|
| 148 | */ |
|---|
| 149 | public CADSimulator(String propertiesFile) throws SimulationException { |
|---|
| 150 | |
|---|
| 151 | try { |
|---|
| 152 | cadSimulatorProperties = new Properties(); |
|---|
| 153 | cadSimulatorProperties.load(new FileInputStream(propertiesFile)); |
|---|
| 154 | } |
|---|
| 155 | catch (Exception e) { |
|---|
| 156 | cadSimLogger.logp(Level.SEVERE, "CADSimulator", "Constructor", |
|---|
| 157 | "Exception in reading properties file.", e); |
|---|
| 158 | |
|---|
| 159 | throw new SimulationException(SimulationException.INITIALIZE_ERROR, e); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | //Create the Coordinator and register it for RMI communicator. Start the |
|---|
| 163 | //CAD Simulator Socket Handler to begin to accept connections from CAD Clients. |
|---|
| 164 | try { |
|---|
| 165 | theViewer = new CADSimulatorViewer(); |
|---|
| 166 | theCoordinator = new Coordinator(); |
|---|
| 167 | startRegistry(Integer.parseInt( |
|---|
| 168 | cadSimulatorProperties.getProperty( |
|---|
| 169 | CAD_PROPERTIES.COOR_RMI_PORT.name).trim())); |
|---|
| 170 | startRegistry(Integer.parseInt( |
|---|
| 171 | cadSimulatorProperties.getProperty( |
|---|
| 172 | CAD_PROPERTIES.CAD_RMI_PORT.name).trim())); |
|---|
| 173 | |
|---|
| 174 | theSimulationCntrlMgr = new SimulationControlManager(theCoordinator); |
|---|
| 175 | |
|---|
| 176 | theATMSMgr = new ATMSManager( |
|---|
| 177 | cadSimulatorProperties.getProperty( |
|---|
| 178 | CAD_PROPERTIES.ATMS_PROP_FILE.name)); |
|---|
| 179 | |
|---|
| 180 | theMediaMgr = new MediaManager( |
|---|
| 181 | cadSimulatorProperties.getProperty( |
|---|
| 182 | CAD_PROPERTIES.MEDIA_PROP_FILE.name), |
|---|
| 183 | theATMSMgr, theViewer); |
|---|
| 184 | |
|---|
| 185 | theParamicsSimMgr = new ParamicsSimulationManager( |
|---|
| 186 | cadSimulatorProperties.getProperty( |
|---|
| 187 | CAD_PROPERTIES.PARAMICS_PROP_FILE.name), |
|---|
| 188 | theCoordinator, theMediaMgr); |
|---|
| 189 | |
|---|
| 190 | theSoundPlayer = new SoundPlayer( |
|---|
| 191 | cadSimulatorProperties.getProperty( |
|---|
| 192 | CAD_PROPERTIES.AUDIO_LOCATION.name)); |
|---|
| 193 | theSoundPlayer.start(); |
|---|
| 194 | |
|---|
| 195 | theIncidentMgr = new IncidentManager(theCoordinator, theSoundPlayer); |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | //Begin accepting Client connections |
|---|
| 199 | CADSimulatorSocketHandler tmsh = new CADSimulatorSocketHandler( |
|---|
| 200 | Integer.parseInt(cadSimulatorProperties.getProperty( |
|---|
| 201 | CAD_PROPERTIES.CLIENT_PORT.name).trim())); |
|---|
| 202 | tmsh.start(); |
|---|
| 203 | } |
|---|
| 204 | catch (RemoteException e) { |
|---|
| 205 | cadSimLogger.logp(Level.SEVERE, "CADSimulator", "Constructor", |
|---|
| 206 | "Exception in starting Coordinator.", e); |
|---|
| 207 | |
|---|
| 208 | throw new SimulationException(SimulationException.BINDING, e); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | //Load CMS Diversion Information from the XML file |
|---|
| 212 | try { |
|---|
| 213 | if(cadSimulatorProperties.getProperty( |
|---|
| 214 | CAD_PROPERTIES.CMS_XML_FILE.name) != null) { |
|---|
| 215 | CMSDiversionDB.getInstance().loadFromXML( |
|---|
| 216 | DocumentBuilderFactory.newInstance().newDocumentBuilder() |
|---|
| 217 | .parse(new File(cadSimulatorProperties.getProperty( |
|---|
| 218 | CAD_PROPERTIES.CMS_XML_FILE.name)))); |
|---|
| 219 | } |
|---|
| 220 | } |
|---|
| 221 | catch (Exception e) |
|---|
| 222 | { |
|---|
| 223 | cadSimLogger.logp(Level.SEVERE, "CADSimulator", "Constructor", |
|---|
| 224 | "Exception in parsing CMSDiversion xml file.", e); |
|---|
| 225 | |
|---|
| 226 | JOptionPane.showMessageDialog(new JWindow(), "Unable to open " + |
|---|
| 227 | cadSimulatorProperties.getProperty(CAD_PROPERTIES.CMS_XML_FILE.name), |
|---|
| 228 | "Initialization Error", JOptionPane.WARNING_MESSAGE); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | theViewer.addWindowListener(new WindowListener() { |
|---|
| 232 | public void windowClosed(WindowEvent e) {} |
|---|
| 233 | public void windowOpened(WindowEvent e) {} |
|---|
| 234 | public void windowIconified(WindowEvent e) {} |
|---|
| 235 | public void windowDeiconified(WindowEvent e) {} |
|---|
| 236 | public void windowActivated(WindowEvent e) {} |
|---|
| 237 | public void windowDeactivated(WindowEvent e) {} |
|---|
| 238 | public void windowClosing(WindowEvent e) { |
|---|
| 239 | System.exit(0); |
|---|
| 240 | } |
|---|
| 241 | }); |
|---|
| 242 | |
|---|
| 243 | theViewer.setVisible(true); |
|---|
| 244 | |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | /** |
|---|
| 248 | * Binds the Coordinator to an RMI port so that the SimulationManager |
|---|
| 249 | * can communicate with it, and so that the Coordinator can perform RMI |
|---|
| 250 | * callback method calls. The port numbers and RMI designators are parsed from |
|---|
| 251 | * the properties file file. |
|---|
| 252 | * |
|---|
| 253 | * @param theCoor A reference to the Coordinator object. |
|---|
| 254 | * @throws SimulationException if there are errors in binding the RMI to |
|---|
| 255 | * a port and name. |
|---|
| 256 | */ |
|---|
| 257 | private void startRegistry(Integer regPort) throws SimulationException { |
|---|
| 258 | |
|---|
| 259 | try{ |
|---|
| 260 | LocateRegistry.createRegistry(regPort); |
|---|
| 261 | |
|---|
| 262 | String registryURL = "rmi://localhost:" + regPort + "/coordinator"; |
|---|
| 263 | Naming.rebind(registryURL, theCoordinator); |
|---|
| 264 | } |
|---|
| 265 | catch (Exception e) { |
|---|
| 266 | throw new SimulationException(SimulationException.BINDING, e); |
|---|
| 267 | } |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | /** |
|---|
| 271 | * Method returns a String represetnation of the current time. String format |
|---|
| 272 | * is HHMM |
|---|
| 273 | * |
|---|
| 274 | * @return String representation of the current time. |
|---|
| 275 | */ |
|---|
| 276 | public static String getCADTime() { |
|---|
| 277 | String time = new String(); |
|---|
| 278 | |
|---|
| 279 | Calendar rightNow = Calendar.getInstance(); |
|---|
| 280 | |
|---|
| 281 | if(rightNow.get(Calendar.HOUR_OF_DAY) < 10) |
|---|
| 282 | time += "0"; |
|---|
| 283 | |
|---|
| 284 | time += (String.valueOf(rightNow.get(Calendar.HOUR_OF_DAY))); |
|---|
| 285 | |
|---|
| 286 | if(rightNow.get(Calendar.MINUTE) < 10) |
|---|
| 287 | time += "0"; |
|---|
| 288 | |
|---|
| 289 | time += (String.valueOf(rightNow.get(Calendar.MINUTE))); |
|---|
| 290 | |
|---|
| 291 | return time; |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | /** |
|---|
| 295 | * Returns a string representation of the current date. String format is: |
|---|
| 296 | * MMDDYY |
|---|
| 297 | * |
|---|
| 298 | * @return String format of the date. |
|---|
| 299 | */ |
|---|
| 300 | public static String getCADDate() { |
|---|
| 301 | String date = new String(); |
|---|
| 302 | |
|---|
| 303 | Calendar rightNow = Calendar.getInstance(); |
|---|
| 304 | |
|---|
| 305 | //Months are zero referenced |
|---|
| 306 | if(rightNow.get(Calendar.MONTH) + 1 < 10) |
|---|
| 307 | date += "0"; |
|---|
| 308 | |
|---|
| 309 | date += (String.valueOf(rightNow.get(Calendar.MONTH)+ 1)); |
|---|
| 310 | |
|---|
| 311 | if(rightNow.get(Calendar.DAY_OF_MONTH) < 10) |
|---|
| 312 | date += "0"; |
|---|
| 313 | |
|---|
| 314 | date += (String.valueOf(rightNow.get(Calendar.DAY_OF_MONTH))); |
|---|
| 315 | |
|---|
| 316 | if(rightNow.get(Calendar.YEAR) % 1000 < 10) |
|---|
| 317 | date += "0"; |
|---|
| 318 | |
|---|
| 319 | date += (String.valueOf(rightNow.get(Calendar.YEAR) % 1000)); |
|---|
| 320 | |
|---|
| 321 | return date; |
|---|
| 322 | |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | /** |
|---|
| 326 | * Main class. Instantiate a CAD Simulator with the properties file |
|---|
| 327 | * specified on the command line or the default properties file |
|---|
| 328 | * |
|---|
| 329 | * @param args Command line arguments. |
|---|
| 330 | */ |
|---|
| 331 | public static void main(String[] args) { |
|---|
| 332 | if(System.getProperty("CONFIG_DIR") == null){ |
|---|
| 333 | System.setProperty("CONFIG_DIR", "config"); |
|---|
| 334 | } |
|---|
| 335 | try { |
|---|
| 336 | UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
|---|
| 337 | new CADSimulator(System.getProperty("CONFIG_DIR") + System.getProperty("file.separator") + CONFIG_FILE_NAME); |
|---|
| 338 | } |
|---|
| 339 | catch (Exception e) { |
|---|
| 340 | cadSimLogger.logp(Level.SEVERE, "CADSimulator", "Main", |
|---|
| 341 | "Error initializing application.", e); |
|---|
| 342 | |
|---|
| 343 | JOptionPane.showMessageDialog(new JWindow(), e.getMessage(), |
|---|
| 344 | "Error - Program Exiting", JOptionPane.ERROR_MESSAGE); |
|---|
| 345 | |
|---|
| 346 | System.exit(-1); |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | } |
|---|
| 350 | } |
|---|