source: tmcsimulator/trunk/src/tmcsim/cadsimulator/CADSimulator.java @ 2

Revision 2, 14.3 KB checked in by jdalbey, 10 years ago (diff)

Initial Import of project files

Line 
1package tmcsim.cadsimulator;
2
3import java.awt.event.WindowEvent;
4import java.awt.event.WindowListener;
5import java.io.File;
6import java.io.FileInputStream;
7import java.rmi.Naming;
8import java.rmi.RemoteException;
9import java.rmi.registry.LocateRegistry;
10import java.util.Calendar;
11import java.util.Properties;
12import java.util.logging.FileHandler;
13import java.util.logging.Level;
14import java.util.logging.Logger;
15
16import javax.swing.JOptionPane;
17import javax.swing.JWindow;
18import javax.swing.UIManager;
19import javax.xml.parsers.DocumentBuilderFactory;
20
21import tmcsim.cadsimulator.db.CMSDiversionDB;
22import tmcsim.cadsimulator.managers.ATMSManager;
23import tmcsim.cadsimulator.managers.IncidentManager;
24import tmcsim.cadsimulator.managers.MediaManager;
25import tmcsim.cadsimulator.managers.ParamicsSimulationManager;
26import tmcsim.cadsimulator.managers.SimulationControlManager;
27import tmcsim.cadsimulator.viewer.CADSimulatorViewer;
28import tmcsim.client.cadclientgui.data.CADData;
29import tmcsim.common.SimulationException;
30import 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 */
69public class CADSimulator {
70   
71    /** Error logger. */
72    private static Logger cadSimLogger = Logger.getLogger("tmcsim.simulationmanager");
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
138    /**
139     * Constructor.  Load the Properties file and initialize all CAD Simulator
140     * Managers and establish Manager data relationships.  A
141     * CADSimulatorSocketHandler is instantiated and started to being
142     * listening for remote CAD connections.  The CMSDiversionDB is initialized
143     * with the XML data(incomplete design).
144     *
145     * @param propertiesFile Filename of CAD Simulator properties file.
146     * @throws SimulationException if there is an error in initializing the CAD Simulator.
147     */
148    public CADSimulator(String propertiesFile) throws SimulationException {
149       
150        try {
151            cadSimulatorProperties = new Properties();
152            cadSimulatorProperties.load(new FileInputStream(propertiesFile));
153        }
154        catch (Exception e) {           
155            cadSimLogger.logp(Level.SEVERE, "CADSimulator", "Constructor", 
156                    "Exception in reading properties file.", e);
157           
158            throw new SimulationException(SimulationException.INITIALIZE_ERROR, e); 
159        }
160   
161        //Create the Coordinator and register it for RMI communicator.  Start the
162        //CAD Simulator Socket Handler to begin to accept connections from CAD Clients.
163        try {         
164            theViewer             = new CADSimulatorViewer();
165            theCoordinator        = new Coordinator();
166            startRegistry(Integer.parseInt(
167                    cadSimulatorProperties.getProperty(
168                            CAD_PROPERTIES.COOR_RMI_PORT.name).trim()));
169            startRegistry(Integer.parseInt(
170                    cadSimulatorProperties.getProperty(
171                            CAD_PROPERTIES.CAD_RMI_PORT.name).trim()));
172
173            theSimulationCntrlMgr = new SimulationControlManager(theCoordinator);   
174           
175            theATMSMgr            = new ATMSManager(
176                    cadSimulatorProperties.getProperty(
177                            CAD_PROPERTIES.ATMS_PROP_FILE.name));
178           
179            theMediaMgr           = new MediaManager(
180                    cadSimulatorProperties.getProperty(
181                            CAD_PROPERTIES.MEDIA_PROP_FILE.name),
182                            theATMSMgr, theViewer);
183           
184            theParamicsSimMgr     = new ParamicsSimulationManager(
185                    cadSimulatorProperties.getProperty(
186                            CAD_PROPERTIES.PARAMICS_PROP_FILE.name),
187                            theCoordinator, theMediaMgr);   
188           
189            theSoundPlayer        = new SoundPlayer(
190                    cadSimulatorProperties.getProperty(
191                            CAD_PROPERTIES.AUDIO_LOCATION.name));
192            theSoundPlayer.start();
193           
194            theIncidentMgr        = new IncidentManager(theCoordinator, theSoundPlayer);   
195           
196
197            //Begin accepting Client connections
198            CADSimulatorSocketHandler tmsh = new CADSimulatorSocketHandler(
199                    Integer.parseInt(cadSimulatorProperties.getProperty(
200                            CAD_PROPERTIES.CLIENT_PORT.name).trim()));
201            tmsh.start();   
202        }       
203        catch (RemoteException e) {
204            cadSimLogger.logp(Level.SEVERE, "CADSimulator", "Constructor", 
205                    "Exception in starting Coordinator.", e);
206           
207            throw new SimulationException(SimulationException.BINDING, e);
208        }       
209       
210        //Load CMS Diversion Information from the XML file
211        try {       
212            if(cadSimulatorProperties.getProperty(
213                    CAD_PROPERTIES.CMS_XML_FILE.name) != null) {
214                    CMSDiversionDB.getInstance().loadFromXML(
215                        DocumentBuilderFactory.newInstance().newDocumentBuilder()
216                            .parse(new File(cadSimulatorProperties.getProperty(
217                                    CAD_PROPERTIES.CMS_XML_FILE.name))));
218            }           
219        }
220        catch (Exception e) 
221        {
222            cadSimLogger.logp(Level.SEVERE, "CADSimulator", "Constructor", 
223                    "Exception in parsing CMSDiversion xml file.", e);
224           
225            JOptionPane.showMessageDialog(new JWindow(), "Unable to open " + 
226                    cadSimulatorProperties.getProperty(CAD_PROPERTIES.CMS_XML_FILE.name), 
227                "Initialization Error", JOptionPane.WARNING_MESSAGE);   
228        }
229             
230        theViewer.addWindowListener(new WindowListener() {
231            public void windowClosed(WindowEvent e)  {}
232            public void windowOpened(WindowEvent e)  {}           
233            public void windowIconified(WindowEvent e)  {}         
234            public void windowDeiconified(WindowEvent e)  {}   
235            public void windowActivated(WindowEvent e)  {}                             
236            public void windowDeactivated(WindowEvent e)  {}         
237            public void windowClosing(WindowEvent e)  {   
238                System.exit(0);
239            }           
240        });
241       
242        theViewer.setVisible(true);
243
244    } 
245   
246    /**
247     * Binds the Coordinator to an RMI port so that the SimulationManager
248     * can communicate with it, and so that the Coordinator can perform RMI
249     * callback method calls.  The port numbers and RMI designators are parsed from
250     * the properties file file.
251     *
252     * @param theCoor A reference to the Coordinator object.
253     * @throws SimulationException if there are errors in binding the RMI to
254     * a port and name.
255     */
256    private void startRegistry(Integer regPort) throws SimulationException {
257       
258        try{
259            LocateRegistry.createRegistry(regPort);                             
260           
261            String registryURL = "rmi://localhost:" + regPort + "/coordinator";
262            Naming.rebind(registryURL, theCoordinator);
263        }
264        catch (Exception e) {           
265            throw new SimulationException(SimulationException.BINDING, e);
266        }
267    }
268
269    /**
270     * Method returns a String represetnation of the current time.  String format
271     * is HHMM
272     *
273     * @return String representation of the current time.
274     */
275    public static String getCADTime() {
276        String time = new String();     
277
278        Calendar rightNow = Calendar.getInstance();
279       
280        if(rightNow.get(Calendar.HOUR_OF_DAY) < 10)
281            time += "0";
282       
283        time += (String.valueOf(rightNow.get(Calendar.HOUR_OF_DAY)));
284       
285        if(rightNow.get(Calendar.MINUTE) < 10)
286            time += "0";
287       
288        time += (String.valueOf(rightNow.get(Calendar.MINUTE)));       
289                           
290        return time;       
291    }           
292   
293    /**
294     * Returns a string representation of the current date.  String format is:
295     * MMDDYY
296     *
297     * @return String format of the date.
298     */
299    public static String getCADDate() {
300        String date = new String();
301       
302        Calendar rightNow = Calendar.getInstance();
303       
304        //Months are zero referenced
305        if(rightNow.get(Calendar.MONTH) + 1 < 10)
306            date += "0";
307       
308        date += (String.valueOf(rightNow.get(Calendar.MONTH)+ 1));
309       
310        if(rightNow.get(Calendar.DAY_OF_MONTH) < 10)
311            date += "0";
312       
313        date += (String.valueOf(rightNow.get(Calendar.DAY_OF_MONTH)));     
314                           
315        if(rightNow.get(Calendar.YEAR) % 1000 < 10)
316            date += "0";
317       
318        date += (String.valueOf(rightNow.get(Calendar.YEAR) % 1000));                               
319                           
320        return date;   
321       
322    }
323       
324    /**
325     * Main class.  Instantiate a CAD Simulator with the properties file
326     * specified on the command line or the default properties file
327     *
328     * @param args Command line arguments.
329     */
330    public static void main(String[] args) {
331        System.setProperty("CAD_SIM_PROPERTIES",  "config/cad_simulator_config.properties");
332       
333        try 
334        {
335            if(System.getProperty("CAD_SIM_PROPERTIES") != null)
336            {
337                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
338               
339                new CADSimulator(System.getProperty("CAD_SIM_PROPERTIES"));
340            }
341            else
342            {
343                throw new Exception ("CAD_SIM_PROPERTIES system property not defined.");
344            }
345        }
346        catch (Exception e) {
347            cadSimLogger.logp(Level.SEVERE, "CADSimulator", "Main", 
348                    "Error initializing application.", e);
349           
350            JOptionPane.showMessageDialog(new JWindow(), e.getMessage(), 
351                    "Error - Program Exiting", JOptionPane.ERROR_MESSAGE); 
352           
353            System.exit(-1);
354        }
355       
356    }
357} 
Note: See TracBrowser for help on using the repository browser.