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