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

Revision 14, 7.5 KB checked in by jdalbey, 10 years ago (diff)

Change config settings for paramics communicator logging to localhost for easier dev testing. Removed setproperty from Sim Mgr, so now you must provide it as JVM argument

Line 
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.SimulationException;
15
16/**
17 * Simulation Manager is the main class for this module. The Simulation Manager
18 * is used to control the view and control the simulation. Simulation incidents
19 * are loaded, removed, reschedule, and added from the Simulation Manager. The
20 * Simulation Manager provides functionality for connecting to the paramics
21 * communicator and applying diversions. A history of all events is shown as
22 * well.<br>
23 * The SimulationManager may be started at any point before, during, or after a
24 * simulation has begun. The SimulationManager connects to the CADSimulator and
25 * communicates through Java RMI methods. If two SimulationManagers are started,
26 * the second one started, chronologically, will receive communication from the
27 * CADSimulator. <br><br>
28 * The properties file for the SimulationManager class contains the following
29 * data.<br>
30 * <code>
31 * -----------------------------------------------------------------------------<br>
32 * Host Name The host name where the CADSimulator is located.<br>
33 * Error File The target file to use for error logging.<br>
34 * -----------------------------------------------------------------------------<br>
35 * Example File: <br>
36 * CADSimulatorHost = localhost <br>
37 * ErrorFile = sim_mgr_error.xml <br>
38 * -----------------------------------------------------------------------------<br>
39 * </code>
40 *
41 * @author Matthew Cechini (mcechini@calpoly.edu)
42 * @version $Date: 2009/04/17 16:27:47 $ $Revision: 1.7
43 */
44public class SimulationManager
45{
46
47    /**
48     * Error logger.
49     */
50    private static Logger simManLogger = Logger.getLogger("tmcsim.simulationmanager");
51
52    /**
53     * Enumeration containing property names.
54     *
55     * @author Matthew Cechini
56     */
57    private static enum PROPERTIES
58    {
59
60        CAD_SIM_HOST("CADSimulatorHost"),
61        CAD_SIM_PORT("CADSimulatorRMIPort"),
62        SCRIPT_DIR("ScriptDir"),
63        FAKE_PARAMICS("FakeParamicsConnection");
64        public String name;
65
66        private PROPERTIES(String n)
67        {
68            name = n;
69        }
70    }
71    /**
72     * Instance of the SimulationManagerModel which communicates with the CAD
73     * Simulator to display the current simulation information. This model class
74     * contains the data that is displayed by the SimulationManagerView class.
75     * The View purely provides a GUI interface for the data contained within
76     * the model.
77     */
78    SimulationManagerModel theSimManagerModel;
79    /**
80     * Instance of the SimulationManagerView class which provides a GUI for the
81     * user to view the current simulation information and to manage the
82     * simulation. The view communicates to the SimulationManagerModel class to
83     * get and set data.
84     */
85    SimulationManagerView theSimManagerView;
86    /**
87     * The Properties object for the Simulation Manager.
88     */
89    private Properties simManagerProperties;
90
91    /**
92     * Constructor. Set communication data members from properties file.
93     * Instantiate the SimulationManager Model and View objects, and set
94     * visibility to true.
95     *
96     * @param propertiesFile Properties file containing info for Simulation
97     * Manager.
98     */
99    public SimulationManager(String propertiesFile) throws SimulationException
100    {
101
102        try
103        {
104            simManagerProperties = new Properties();
105            simManagerProperties.load(new FileInputStream(new File(propertiesFile)));
106
107            SimulationManagerView.SCRIPT_DIR =
108                    simManagerProperties.getProperty(PROPERTIES.SCRIPT_DIR.name).trim();
109
110            //make sure properties aren't null
111            if (simManagerProperties.getProperty(PROPERTIES.CAD_SIM_HOST.name) == null)
112            {
113                throw new Exception("CAD Simulator host property is null.");
114            }
115
116            if (simManagerProperties.getProperty(PROPERTIES.CAD_SIM_PORT.name) == null)
117            {
118                throw new Exception("CAD Simulator port property is null.");
119            }
120
121        } catch (Exception e)
122        {
123            simManLogger.logp(Level.SEVERE, "SimulationManager", "Constructor",
124                    "Exception in reading properties file.", e);
125
126            throw new SimulationException(SimulationException.INITIALIZE_ERROR, e);
127        }
128
129        //Construct the SimulationManagerModel
130        try
131        {
132            theSimManagerModel = new SimulationManagerModel(
133                    simManagerProperties.getProperty(PROPERTIES.CAD_SIM_HOST.name).trim(),
134                    simManagerProperties.getProperty(PROPERTIES.CAD_SIM_PORT.name).trim());
135
136            //Construct the SimulationManagerView and set up the Model-View references.
137            theSimManagerView = new SimulationManagerView(theSimManagerModel);
138            theSimManagerModel.setView(theSimManagerView);
139        } catch (RemoteException re)
140        {
141            simManLogger.logp(Level.SEVERE, "SimulationManager", "Constructor",
142                    "Unable to establish RMI ", re);
143
144            throw new SimulationException(SimulationException.CAD_SIM_CONNECT, re);
145        }
146
147        theSimManagerView.addWindowListener(new WindowListener()
148        {
149            public void windowClosed(WindowEvent e)
150            {
151            }
152
153            public void windowOpened(WindowEvent e)
154            {
155            }
156
157            public void windowIconified(WindowEvent e)
158            {
159            }
160
161            public void windowDeiconified(WindowEvent e)
162            {
163            }
164
165            public void windowActivated(WindowEvent e)
166            {
167            }
168
169            public void windowDeactivated(WindowEvent e)
170            {
171            }
172
173            public void windowClosing(WindowEvent e)
174            {
175                theSimManagerModel.disconnect();
176                System.exit(0);
177            }
178        });
179
180        if (Boolean.parseBoolean(simManagerProperties.getProperty(
181                PROPERTIES.FAKE_PARAMICS.name).trim()))
182        {
183            theSimManagerView.setParamicsStatus(PARAMICS_STATUS.CONNECTED);
184        }
185
186        //Show the SimulationManager
187        theSimManagerView.setVisible(true);
188    }
189
190    /**
191     * Main class.
192     *
193     * @param args Command line arguments.
194     */
195    static public void main(String[] args)
196    {
197        //System.setProperty("SIM_MGR_PROPERTIES", "config/sim_manager_config.properties");
198        //System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
199
200        try
201        {
202            if (System.getProperty("SIM_MGR_PROPERTIES") != null)
203            {
204                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
205
206                new SimulationManager(System.getProperty("SIM_MGR_PROPERTIES"));
207            } else
208            {
209                throw new Exception("SIM_MGR_PROPERTIES system property not defined.");
210            }
211        } catch (Exception e)
212        {
213            simManLogger.logp(Level.SEVERE, "SimulationManager", "Main",
214                    "Error occured initializing application", e);
215
216            JOptionPane.showMessageDialog(null, e.getMessage(),
217                    "Error - Program Exiting", JOptionPane.ERROR_MESSAGE);
218
219            System.exit(-1);
220        }
221
222    }
223}
Note: See TracBrowser for help on using the repository browser.