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

source: tmcsimulator/trunk/src/tmcsim/simulationmanager/SimulationManager.java @ 658

Revision 658, 9.2 KB checked in by jdalbey, 4 years ago (diff)

Replace hard-code scenario folder name in Simulation Manager with config property.

RevLine 
1package tmcsim.simulationmanager;
2
3import java.awt.event.WindowEvent;
4import java.awt.event.WindowListener;
5import java.io.File;
6import java.io.FileInputStream;
7import java.rmi.RemoteException;
8import java.util.Properties;
9import java.util.logging.Level;
10import java.util.logging.Logger;
11import javax.swing.JOptionPane;
12import javax.swing.UIManager;
13import tmcsim.common.CADEnums.PARAMICS_STATUS;
14import tmcsim.common.ScriptException;
15import 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 */
45public class SimulationManager
46{
47
48    private static final String CONFIG_FILE_NAME = "sim_manager_config.properties";
49    /*
50     * Default name of folder that contains Scenario xml files.
51     * Actual name retrieved from config file.
52    */
53    public static String SCENARIOS_DIR = "Scenarios";
54    /**
55     * Error logger.
56     */
57    private static Logger simManLogger = Logger.getLogger("tmcsim.simulationmanager");
58
59    /**
60     * Enumeration containing property names.
61     *
62     * @author Matthew Cechini
63     */
64    private static enum PROPERTIES
65    {
66
67        CAD_SIM_HOST("CADSimulatorHost"),
68        CAD_SIM_PORT("CADSimulatorRMIPort"),
69        SCENARIOS_DIR("ScenariosDir"),
70        FAKE_PARAMICS("FakeParamicsConnection");
71        public String name;
72
73        private PROPERTIES(String n)
74        {
75            name = n;
76        }
77    }
78    /**
79     * Instance of the SimulationManagerModel which communicates with the CAD
80     * Simulator to display the current simulation information. This model class
81     * contains the data that is displayed by the SimulationManagerView class.
82     * The View purely provides a GUI interface for the data contained within
83     * the model.
84     */
85    SimulationManagerModel theSimManagerModel;
86    /**
87     * Instance of the SimulationManagerView class which provides a GUI for the
88     * user to view the current simulation information and to manage the
89     * simulation. The view communicates to the SimulationManagerModel class to
90     * get and set data.
91     */
92    SimulationManagerView theSimManagerView;
93    /**
94     * The Properties object for the Simulation Manager.
95     */
96    private Properties simManagerProperties;
97
98    /**
99     * Constructor. Set communication data members from properties file.
100     * Instantiate the SimulationManager Model and View objects, and set
101     * visibility to true.
102     *
103     * @param propertiesFile Properties file containing info for Simulation
104     * Manager.
105     */
106    public SimulationManager(String propertiesFile) throws SimulationException
107    {
108
109        try
110        {
111            simManagerProperties = new Properties();
112            simManagerProperties.load(new FileInputStream(new File(propertiesFile)));
113
114            SCENARIOS_DIR =
115                    simManagerProperties.getProperty(PROPERTIES.SCENARIOS_DIR.name).trim();
116
117            //make sure properties aren't null
118            if (simManagerProperties.getProperty(PROPERTIES.CAD_SIM_HOST.name) == null)
119            {
120                throw new Exception("CAD Simulator host property is null.");
121            }
122
123            if (simManagerProperties.getProperty(PROPERTIES.CAD_SIM_PORT.name) == null)
124            {
125                throw new Exception("CAD Simulator port property is null.");
126            }
127
128        } catch (Exception e)
129        {
130            simManLogger.logp(Level.SEVERE, "SimulationManager", "Constructor",
131                    "Exception in reading properties file.", e);
132
133            throw new SimulationException(SimulationException.INITIALIZE_ERROR, e);
134        }
135
136        //Construct the SimulationManagerModel
137        try
138        {
139            theSimManagerModel = new SimulationManagerModel(
140                    simManagerProperties.getProperty(PROPERTIES.CAD_SIM_HOST.name).trim(),
141                    simManagerProperties.getProperty(PROPERTIES.CAD_SIM_PORT.name).trim());
142
143            //Construct the SimulationManagerView and set up the Model-View references.
144            theSimManagerView = new SimulationManagerView(theSimManagerModel);
145            theSimManagerModel.setView(theSimManagerView);
146        } catch (RemoteException re)
147        {
148            simManLogger.logp(Level.SEVERE, "SimulationManager", "Constructor",
149                    "Unable to establish RMI ", re);
150
151            throw new SimulationException(SimulationException.CAD_SIM_CONNECT, re);
152        }
153
154        theSimManagerView.addWindowListener(new WindowListener()
155        {
156            public void windowClosed(WindowEvent e)
157            {
158            }
159
160            public void windowOpened(WindowEvent e)
161            {
162            }
163
164            public void windowIconified(WindowEvent e)
165            {
166            }
167
168            public void windowDeiconified(WindowEvent e)
169            {
170            }
171
172            public void windowActivated(WindowEvent e)
173            {
174            }
175
176            public void windowDeactivated(WindowEvent e)
177            {
178            }
179
180            public void windowClosing(WindowEvent e)
181            {
182                theSimManagerModel.disconnect();
183                System.exit(0);
184            }
185        });
186
187        if (Boolean.parseBoolean(simManagerProperties.getProperty(
188                PROPERTIES.FAKE_PARAMICS.name).trim()))
189        {
190            theSimManagerView.setParamicsStatus(PARAMICS_STATUS.CONNECTED);
191        }
192
193        //Show the SimulationManager
194        theSimManagerView.setVisible(true);
195    }
196
197    /**
198     * Load a simulation script from the specified file.
199     *
200     * @param scriptFile the XML file containing the simulation control script
201     * to be run.
202     * @throws ScriptException if the script throws an exception
203     * @throws SimulationException if the simulation throws an exception
204     */
205    public void loadScript(File scriptFile) throws ScriptException, SimulationException
206    {
207        theSimManagerModel.loadScript(scriptFile);
208    }
209
210    /**
211     * Main class.
212     *
213     * @param args Command line arguments.
214     */
215    static public void main(String[] args)
216    {
217        //System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
218        if (System.getProperty("CONFIG_DIR") == null)
219        {
220            System.setProperty("CONFIG_DIR", "config");
221        }
222        SimulationManager app;
223        try
224        {
225            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
226            app = new SimulationManager(System.getProperty("CONFIG_DIR") + System.getProperty("file.separator") + CONFIG_FILE_NAME);
227            // Check if a script filename has been given as an argument
228            String arg1;
229            if (args.length > 0)
230            {
231                arg1 = args[0];
232                File scriptFile = new File(SCENARIOS_DIR+ System.getProperty("file.separator") + arg1);
233                // if the script file exists, load it and show name in title bar
234                if (scriptFile.exists())
235                {
236                    app.loadScript(scriptFile);
237                    // Implement ticket #197
238                    app.theSimManagerView.setTitle("Simulation Manager: " + arg1);
239                    simManLogger.logp(Level.INFO,"SimulationManager","Main",
240                            "Sim Mgr starting with scenario file: " + arg1 + ".");
241                }
242                else
243                {
244                    simManLogger.logp(Level.INFO,"SimulationManager","Main",
245                            "Scenario file not found: " + arg1 +
246                            ". Starting with no scenario.");
247                }
248            }
249        } catch (Exception e)
250        {
251            simManLogger.logp(Level.SEVERE, "SimulationManager", "Main",
252                    "Error occured initializing application", e);
253
254            JOptionPane.showMessageDialog(null, e.getMessage(),
255                    "Error - Program Exiting", JOptionPane.ERROR_MESSAGE);
256
257            System.exit(-1);
258        }
259    }
260}
Note: See TracBrowser for help on using the repository browser.