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

Revision 33, 7.4 KB checked in by bokumura, 10 years ago (diff)

Directory Restructure: All system properties have been deprecated and replaced by the system property: "CONFIG_DIR". If CONFIG_DIR is not specified at run time, it will default to the "config/" directory.

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