Warning: Can't use blame annotator:
svn blame failed on trunk/src/tmcsim/cadsimulator/CADServer.java: ("Can't find a temporary directory: Internal error", 20014)

source: tmcsimulator/trunk/src/tmcsim/cadsimulator/CADServer.java @ 188

Revision 188, 15.1 KB checked in by jdalbey, 9 years ago (diff)

TrafficModelManager?.java: Now integrated into CAD Server. Passes smoke test, needs system testing.

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