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 @ 664

Revision 664, 8.6 KB checked in by jdalbey, 4 years ago (diff)

Multifile commit - revise source to match revisions to config filenames. Fix broken system tests. Fix defect #160.

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