source: tmcsimulator/trunk/src/tmcsim/simulationmanager/SimulationManagerModel.java @ 2

Revision 2, 24.9 KB checked in by jdalbey, 10 years ago (diff)

Initial Import of project files

Line 
1package tmcsim.simulationmanager;
2
3import java.io.File;
4import java.rmi.Naming;
5import java.rmi.RemoteException;
6import java.rmi.server.UnicastRemoteObject;
7import java.util.TreeMap;
8import java.util.TreeSet;
9import java.util.Vector;
10import java.util.logging.Level;
11import java.util.logging.Logger;
12
13import tmcsim.cadmodels.CMSDiversion;
14import tmcsim.cadmodels.CMSInfo;
15import tmcsim.client.cadclientgui.data.Incident;
16import tmcsim.client.cadclientgui.data.IncidentEvent;
17import tmcsim.common.ScriptException;
18import tmcsim.common.SimulationException;
19import tmcsim.common.CADEnums.PARAMICS_STATUS;
20import tmcsim.common.CADEnums.SCRIPT_STATUS;
21import tmcsim.interfaces.CoordinatorInterface;
22import tmcsim.interfaces.SimulationManagerInterface;
23
24
25/**
26 * SimulationManagerModel is the model class for the Simulation Manager.  All communication
27 * between the Coordinator and the Simulation Manager passes through this object.  The view
28 * passes requests through local methods, and the Coordinator calls remote functions in this
29 * object to update the viewed simulation data.
30 * <br/>
31 * At construction, the SimulationManagerModel registers itself with the coordinator. 
32 * Administrative commands for the simulation are received from the SimlationManagerView
33 * class, and then the appropriate Coordinator remote method is executed.  During a
34 * simulation, the Coordinator calls remote methods contained in the SimulationManagerInterface. 
35 * For a description of those methods, see the interface's documentation.  After construction,
36 * this model class must have its setView() method called to set the reference to its view class.
37 *
38 * @see SimulationManagerView
39 * @see SimulationManagerInterface
40 * @author Matthew Cechini
41 * @version $Revision: 1.3 $ $Date: 2006/06/06 20:46:41 $
42 */
43@SuppressWarnings("serial")
44public class SimulationManagerModel extends UnicastRemoteObject
45    implements SimulationManagerInterface {
46   
47    /** Error Logger. */
48    private Logger simManagerLogger = Logger.getLogger("tmcsim.simulationmanager");
49   
50    /** RMI interface for communication with the remote Coordinator. */ 
51    private static CoordinatorInterface theCoorInt;
52   
53    /** The SimulationManagerView object. */
54    private SimulationManagerView theSimManagerView;   
55   
56
57    /**
58     * Constructor.  Establishes the RMI communication with the Coordinator.
59     *
60     * @param hostname Host name of the CAD Simulator.   
61     * @param portNumber Port number of the CAD Simulator RMI communication.
62     * @throws RemoteException if error in RMI communication
63     * @throws SimulationException if there is an error in registering RMI methods.
64     */
65    public SimulationManagerModel(String hostname, String portNumber) 
66            throws RemoteException, SimulationException {
67        super();                               
68                                       
69        connect(hostname, portNumber);
70    }       
71   
72    /**
73     * Connect to the Coordinator's RMI object, and register this object for
74     * callback with the Coordinator.
75     * @param hostname Host name of the CAD Simulator.   
76     * @param portNumber Port number of the CAD Simulator RMI communication.
77     * @throws SimulationException if there is an error creating the RMI connection.
78     */ 
79    protected void connect(String hostname, String portNumber) 
80        throws SimulationException {
81       
82        String coorIntURL = "";
83       
84        try { 
85            coorIntURL = "rmi://" + hostname + ":" + portNumber + "/coordinator"; 
86           
87            theCoorInt = (CoordinatorInterface)Naming.lookup(coorIntURL);
88            theCoorInt.registerForCallback(this);           
89           
90        }
91        catch (Exception e) {
92            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
93                    "establishRMIConnection", "Unable to establish RMI " +
94                    "communication with the CAD Simulator.  URL <" + coorIntURL + ">", e);
95           
96            throw new SimulationException(SimulationException.CAD_SIM_CONNECT, e);
97        }   
98    }
99   
100    /**
101     * This method unregisters this SimulationManager from the Coordinator and closes
102     * the RMI communication.
103     */
104    public void disconnect() {
105        try {
106            theCoorInt.unregisterForCallback(this);
107            theCoorInt = null;
108        }
109        catch (Exception e) {
110            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
111                    "closeSimManager", "Exception in unregistering Simulation" +
112                    "Manager from the CAD Simulator.", e);
113        }
114    }
115   
116    /**
117     * Set the local reference to the SimulationManagerView object.  The view
118     * is updated with the current simulation time, script status, and Paramics
119     * connection status.
120     *
121     * @param newView The instance of the SimulationManagerView class.
122     * @throws SimulationException if there is an error in RMI communication to the CAD Simulator.
123     */
124    public void setView(SimulationManagerView newView) throws SimulationException {
125        try {
126            theSimManagerView = newView;
127           
128            theSimManagerView.tick(theCoorInt.getCurrentSimulationTime()); 
129            theSimManagerView.setScriptStatus(theCoorInt.getScriptStatus());
130            theSimManagerView.setParamicsStatus(theCoorInt.getParamicsStatus());
131           
132            initialize();
133        }
134        catch (RemoteException re) {
135            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
136                    "startSimulation", "Unable to communicate with the " +
137                    "CAD Simulator.", re);
138           
139            throw new SimulationException(SimulationException.CAD_SIM_COMM, re);
140        }   
141       
142    }   
143   
144    /**
145     * This method initializes the SimulationManager with all current simulation
146     * data from the Coordinator.  If the simulation is running when the Simulation
147     * Manager is initialized, all previously occured incidents, events, and current
148     * diversion must be displayed.  This method first loads the list of current
149     * incidents from the coordinator, then the triggered events, followed by
150     * all CMSInfo objects.  The SimulationManagerView is notified of any current
151     * diversions within the CMSInfo objects. 
152     *
153     * @throws SimulationException if there is an error in RMI communication or if the
154     *         SimulationManagerView reference has not been set.
155     */
156    protected void initialize() throws SimulationException {
157       
158        try {
159       
160            //Load all incidents from Coordinator
161            loadIncidents(); 
162           
163            //Load all triggered incidents from Coordinator
164            TreeMap<Integer, Vector<IncidentEvent>> tempEvents = theCoorInt.getTriggeredEvents();
165            for(Integer key : tempEvents.keySet())  {           
166                for(IncidentEvent ie : tempEvents.get(key)) 
167                    eventOccured(key, ie);           
168            }           
169                 
170            //Load all current diversions from the Coordinator
171            TreeSet<String> cmsIDs = theCoorInt.getCMSIDs();
172            CMSInfo cmsinfo  = null;
173            for(String cms_id : cmsIDs) {
174                cmsinfo = theCoorInt.getCMSDiversionInfo(cms_id);
175               
176                for(CMSDiversion div : cmsinfo.possibleDiversions) {
177                    if(div.getCurrDiv() != 0) {                     
178                        theSimManagerView.addDiversion(cmsinfo, div);
179                    }       
180                }
181            }   
182           
183            //Send the list of CMS IDs to the View.
184            theSimManagerView.setCMS_IDList(cmsIDs.toArray());
185        }
186        catch (Exception e) {
187            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
188                    "initialize", "Unable to initialize the SimulationManager.", e);
189           
190            throw new SimulationException(SimulationException.CAD_SIM_COMM, e);
191        }   
192    }
193
194    public void tick(long theTime) throws RemoteException {
195        if(theSimManagerView != null)
196            theSimManagerView.tick(theTime);   
197    }
198   
199    public void incidentAdded(Incident newIncident) throws RemoteException {
200        if(theSimManagerView != null) {
201            Integer logNum = new Integer(newIncident.getLogNumber());
202           
203            theSimManagerView.addIncident(newIncident);         
204            theSimManagerView.addIncidentTab(logNum);
205        }
206    }
207
208    public void incidentStarted(Integer logNumber) throws RemoteException {
209        if(theSimManagerView != null) {
210            theSimManagerView.startIncident(logNumber);
211        }
212    }
213
214    public void incidentRemoved(Integer logNumber) throws RemoteException {
215        if(theSimManagerView != null) {
216            theSimManagerView.removeIncident(logNumber);
217            theSimManagerView.removeIncidentTab(logNumber);
218        }
219    }
220   
221    public void eventOccured(Integer logNumber, IncidentEvent theEvent) throws RemoteException {
222        if(theSimManagerView != null) {
223            theSimManagerView.addIncidentEvent(logNumber, theEvent);
224        }
225    }
226   
227    public void setScriptStatus(SCRIPT_STATUS newStatus) throws RemoteException {
228        if(theSimManagerView != null)
229            theSimManagerView.setScriptStatus(newStatus);       
230    }
231
232    public void setParamicsStatus(PARAMICS_STATUS newStatus) throws RemoteException {
233        if(theSimManagerView != null)
234            theSimManagerView.setParamicsStatus(newStatus);     
235       
236    }
237   
238    /**
239     * This method passes the view's request to start the simulation
240     * on to the remote coordinator.
241     *
242     * @throws ScriptException if an error occurs in started the simulation.
243     * @throws SimulationException if there is an error in RMI communication to the CAD Simulator.
244     */ 
245    public void startSimulation() throws ScriptException, SimulationException {
246
247        try {
248            theCoorInt.startSimulation();
249        }
250        catch (RemoteException re) {
251            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
252                    "startSimulation", "Unable to communicate with the " +
253                    "CAD Simulator.", re);
254           
255            throw new SimulationException(SimulationException.CAD_SIM_COMM, re);
256        }   
257
258    }
259   
260    /**
261     * This method passes the view's request to reset the simulation
262     * on to the remote coordinator.  The View's simulation time is reset to 0.
263     * The view's incident tabs are cleared and incident list reset and initialized
264     * with the current list of incidents.  The list of diversions is also reset.
265     *
266     * @throws SimulationException if there is an error in RMI communication to the CAD Simulator.
267     */
268    public void resetSimulation() throws SimulationException {     
269
270        try {
271            theCoorInt.resetSimulation();
272           
273            tick(0);
274            theSimManagerView.resetIncidentTabs();
275            theSimManagerView.resetIncidents();
276            theSimManagerView.resetDiversions();
277
278            loadIncidents(); 
279        }
280        catch (RemoteException re) {
281            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
282                    "resetSimulation", "Unable to communicate with the " +
283                    "CAD Simulator.", re);
284           
285            throw new SimulationException(SimulationException.CAD_SIM_COMM, re);
286        }   
287
288    }
289   
290    /**
291     * This method passes the view's request to pause the simulation
292     * on to the remote coordinator.
293     *     
294     * @throws SimulationException if there is an error in RMI communication to the CAD Simulator.
295     */
296    public void pauseSimulation() throws SimulationException {
297
298        try {
299            theCoorInt.pauseSimulation();
300         }
301        catch (RemoteException re) {
302            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
303                    "pauseSimulation", "Unable to communicate with the " +
304                    "CAD Simulator.", re);
305           
306            throw new SimulationException(SimulationException.CAD_SIM_COMM, re);
307        }   
308
309    }
310   
311    /**
312     * This method passes the view's request to goto a new simulation
313     * time on to the remote coordinator.
314     *
315     * @param time Simulation time
316     * @throws SimulationException if there is an error in RMI communication to the CAD Simulator.
317     */
318    public void gotoSimulationTime(long time) throws SimulationException {
319       
320        try {
321            theCoorInt.gotoSimulationTime(time);
322        }
323        catch (RemoteException re) {
324            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
325                    "gotoSimulationTime", "Unable to communicate with the " +
326                    "CAD Simulator.", re);
327           
328            throw new SimulationException(SimulationException.CAD_SIM_COMM, re);
329        }   
330    }
331   
332    /**
333     * This method passes the view's request to load a script file on to the
334     * remote Coordinator.  If this is successful, the View's incident tabs
335     * are cleared and incident list reset and initialized with the current
336     * list of incidents.  If the load is not successful, the View's incident tabs
337     * and incident list are cleared.  The list of diversions is also reset.
338     *
339     * @param scriptFile the File chosen by the user in the open file dialog.
340     * @throws ScriptException if an error occurs in reading the script file.
341     * @throws SimulationException if there is an error in RMI communication to the CAD Simulator.
342     */
343    public void loadScript(File scriptFile) throws ScriptException, SimulationException{   
344       
345        try {
346            theCoorInt.loadScriptFile(scriptFile);
347           
348            tick(0);
349            theSimManagerView.resetIncidentTabs();
350            theSimManagerView.resetIncidents();
351            theSimManagerView.resetDiversions();
352           
353            loadIncidents();
354         } 
355         catch(ScriptException se) {
356            theSimManagerView.resetIncidentTabs();
357            theSimManagerView.resetIncidents();
358            theSimManagerView.resetDiversions();
359            throw se;
360         }
361        catch (RemoteException re) {
362            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
363                    "loadScript", "Unable to communicate with the " +
364                    "CAD Simulator.", re);
365           
366            throw new SimulationException(SimulationException.CAD_SIM_COMM, re);
367        }           
368    }
369   
370     /**
371      * This method passes the view's request to create a connection between
372      * the CADSimulator and the Paramics Communicator on to the remote Coordinator.
373      *
374      * @throws SimulationException if there is an error in RMI communication to the CAD Simulator.
375      */
376     public void connectToParamics() throws SimulationException {
377
378        try {
379            theCoorInt.connectToParamics();
380        } 
381        catch (RemoteException re) {
382            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
383                    "connectToParamics", "Unable to communicate with the " +
384                    "CAD Simulator.", re);
385           
386            throw new SimulationException(SimulationException.CAD_SIM_COMM, re);
387        }
388     }
389     
390     /**
391      * This method passes the view's request to drop the connection between
392      * the CADSimulator and the Paramics Communicator on to the
393      * remote Coordinator.
394      *
395      * @throws SimulationException if there is an error in RMI communication to the CAD Simulator.
396      */
397     public void disconnectFromParamics() throws SimulationException { 
398
399        try {
400            theCoorInt.disconnectFromParamics();
401        } 
402        catch (RemoteException re) {
403            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
404                    "disconnectFromParamics", "Unable to communicate with the " +
405                    "CAD Simulator.", re);
406           
407            throw new SimulationException(SimulationException.CAD_SIM_COMM, re);
408        }
409     }
410     
411     /**
412      * This method passes the view's request to load a paramics
413      * network on to the remote Coordinator.
414      *
415      * @param networkID The unique network ID that is being loaded
416      *
417      * @throws ScriptException if there is an error in loading the network
418      * @throws SimulationException if there is an error in RMI communication to the CAD Simulator.
419      */
420     public void loadParamicsNetwork(int networkID) throws ScriptException, SimulationException {
421        try {
422             theCoorInt.loadParamicsNetwork(networkID);         
423        } catch (RemoteException re) {
424            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
425                    "loadParamicsNetwork", "Unable to communicate with the " +
426                    "CAD Simulator.", re);
427           
428            throw new SimulationException(SimulationException.CAD_SIM_COMM, re);
429        }
430     }
431         
432     /**
433      * This method passes the view's request to get the value of the
434      * currently loaded paramics network on to the remote Coordinator.
435      *
436      * @return Value of the loaded paramics network. 
437      * @throws SimulationException if there is an error in RMI communication to the CAD Simulator.
438      */
439     public int getParamicsNetworkLoaded() throws SimulationException {
440         try {
441            return theCoorInt.getParamicsNetworkLoaded();           
442        } catch (RemoteException re) {
443            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
444                    "getParamicsNetworkLoaded", "Unable to communicate with the " +
445                    "CAD Simulator.", re);
446           
447            throw new SimulationException(SimulationException.CAD_SIM_COMM, re);
448        }
449     }
450   
451    /**
452     * This method passes the view's request to trigger an incident
453     * on to the remote Coordinator.
454     *
455     * @throws ScriptException if an error occurs in triggering an event.
456     * @throws SimulationException if there is an error in RMI communication to the CAD Simulator.
457     */
458    public void triggerIncident(int logNumber) throws ScriptException, SimulationException {
459        try {
460            theCoorInt.triggerIncident(logNumber);
461           
462        } catch (RemoteException re) {
463            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
464                    "startIncident", "Unable to communicate with the " +
465                    "CAD Simulator.", re);
466           
467            throw new SimulationException(SimulationException.CAD_SIM_COMM, re);
468        }
469    }
470
471     /**
472      * This method passes the view's request to add an incident into the
473      * simulation on to the remote Coordinator.
474      *
475      * @param newIncident Incident to add to the simulation
476      * @throws SimulationException if there is an error in RMI communication to the CAD Simulator.
477      */
478     public void addIncident(final Incident newIncident) throws SimulationException {       
479
480        try {
481            theCoorInt.addIncident(newIncident);
482   
483        } catch (RemoteException re) {
484            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
485                    "addIncident", "Unable to communicate with the " +
486                    "CAD Simulator.", re);
487           
488            throw new SimulationException(SimulationException.CAD_SIM_COMM, re);
489        }       
490
491    }
492   
493    /**
494     * This method passes the view's request to delete an incident from the
495     * simulation on to the remote Coordinator.
496     *
497     * @throws ScriptException if an error occurs in deleting an event.
498     * @throws SimulationException if there is an error in RMI communication to the CAD Simulator.
499     */
500    public void deleteIncident(int logNumber) throws ScriptException, SimulationException {
501
502        try {
503            theCoorInt.deleteIncident(logNumber);   
504   
505        } catch (RemoteException re) {
506            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
507                    "deleteIncident", "Unable to communicate with the " +
508                    "CAD Simulator.", re);
509           
510            throw new SimulationException(SimulationException.CAD_SIM_COMM, re);
511        }       
512    }   
513   
514   
515    /**
516     * This method passes the view's request to reschedule an incident on to
517     * the remote Coordinator.
518     *
519     * @param newTime New simulation time (in seconds).
520     * @throws ScriptException if the Incident has already started or the time for
521     *         recheduling has already passed.
522     * @throws SimulationException if there is an error in RMI communication to the CAD Simulator.
523     */
524     public void rescheduleIncident(long newTime, int logNumber) throws ScriptException, SimulationException {
525           
526        try {
527            theCoorInt.rescheduleIncident(logNumber, newTime);
528        } catch (RemoteException re) {
529            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
530                    "rescheduleIncident", "Unable to communicate with the " +
531                    "CAD Simulator.", re);
532           
533            throw new SimulationException(SimulationException.CAD_SIM_COMM, re);
534        }               
535     }
536     
537    /**
538     * This method passes the view's request to get the current list of
539     * Incidents loaded into the simulation on to the remote Coordinator.
540     *
541     * @return Vector The Vector of currently loaded incidents.
542     * @throws SimulationException if there is an error in RMI communication to the CAD Simulator.
543     */
544    public Vector<Incident> getIncidentList() throws SimulationException {
545
546        try {
547            return theCoorInt.getIncidentList();
548           
549        } catch (RemoteException re) {
550            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
551                    "getIncidentList", "Unable to communicate with the " +
552                    "CAD Simulator.", re);
553           
554            throw new SimulationException(SimulationException.CAD_SIM_COMM, re);
555        }
556    }   
557             
558    /**
559     * This method passes the view's request to get the CMSInfo object
560     * that corresponds to a unique CMS ID String on to the remote Coordinator.
561     *
562     * @param cms_id Unique CMS ID String.
563     * @return CMSInfo object corresponding to the parameter CMS id.
564     * @throws SimulationException if there is an error in RMI communication to the CAD Simulator.
565     */
566    public CMSInfo getCMSDiversionInfo(String cms_id) throws SimulationException {
567       
568        try {
569            return theCoorInt.getCMSDiversionInfo(cms_id);
570        } catch (RemoteException re) {
571            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
572                    "getCMSDiversionInfo", "Unable to communicate with the " +
573                    "CAD Simulator.", re);
574           
575            throw new SimulationException(SimulationException.CAD_SIM_COMM, re);
576        }
577    }
578     
579    /**
580     * This method passes the view's request to update diversions for a
581     * CMS on to the remote Coordinator.
582     *
583     * @param diversion CMS diversions information to apply.
584     * @throws SimulationException if there is an error in RMI communication to the CAD Simulator.
585     */
586    public void applyDiversions(CMSInfo diversion) throws SimulationException {
587       
588        try {
589            theCoorInt.applyDiversions(diversion);
590
591        } catch (RemoteException re) {
592            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
593                    "applyDiversions", "Unable to communicate with the " +
594                    "CAD Simulator.", re);
595           
596            throw new SimulationException(SimulationException.CAD_SIM_COMM, re);
597        }
598    }
599   
600    /**
601     * This method loads the current list of incidents from the Coordinator into the
602     * SimulationManager.  For each incident in the simulation, notify the view to
603     * add a new incident tab.  If the Incident has begun in the simulation,
604     * call the incidentStarted() method to update the view accordingly.
605     *
606     * @throws SimulationException if there is an error in RMI communication to the CAD Simulator.
607     */ 
608    private void loadIncidents() throws SimulationException {       
609       
610        try {           
611            for(Incident inc : theCoorInt.getIncidentList()) {
612   
613                Integer logNum = new Integer(inc.getLogNumber());
614               
615                theSimManagerView.addIncidentTab(logNum);
616                theSimManagerView.addIncident(inc);
617           
618                if(inc.getSecondsToStart() < theCoorInt.getCurrentSimulationTime()) {
619                    incidentStarted(logNum);
620                }
621            }
622        } catch (RemoteException re) {
623            simManagerLogger.logp(Level.SEVERE, "SimulationManagerModel", 
624                    "loadIncidentListTable", "Unable to communicate with the " +
625                    "CAD Simulator.", re);
626           
627            throw new SimulationException(SimulationException.CAD_SIM_COMM, re);
628        }
629       
630    }
631   
632
633}
Note: See TracBrowser for help on using the repository browser.