Changeset 654 in tmcsimulator


Ignore:
Timestamp:
09/09/2022 04:11:08 PM (4 years ago)
Author:
jdalbey
Message:

TrafficModelManager?.java changed timer from Swing timer to java.util.timer to avoid any potential thread locking issues.
CADServer.java moved call to traffic mgr run() until after setVisible ... fix obscure bug on laptop NetBeans.

Location:
trunk
Files:
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/IDE_metadata/NetBeans/TMCSim/nbproject/build-impl.xml

    r650 r654  
    10341034    </target> 
    10351035    <target depends="init,compile,-pre-jar,-do-jar-without-libraries,-do-jar-with-libraries,-post-jar" name="-do-jar"/> 
     1036    <!--When you do "Clean and Build" I think it does this jar target, and sets as the main file in the manifest 
     1037        whichever project configuration is current. --> 
    10361038    <target depends="init,compile,-pre-jar,-do-jar,-post-jar" description="Build JAR." name="jar"/> 
    10371039    <!-- 
  • trunk/IDE_metadata/NetBeans/TMCSim/nbproject/project.properties

    r653 r654  
    5555platform.active=default_platform 
    5656javac.compilerargs= 
    57 main.class=tmcsim.RunSim 
     57main.class=tmcsim.cadsimulator.CADServer 
    5858dist.jar=${dist.dir}/TMCSim.jar 
    5959javac.test.processorpath=${javac.test.classpath} 
  • trunk/src/tmcsim/cadsimulator/CADServer.java

    r455 r654  
    291291                    theCoordinator); 
    292292            theTrafficMgr.addObserver(theViewer); 
    293             theTrafficMgr.run(); 
    294  
     293     
    295294            theMediaMgr = new MediaManager( 
    296295                    cadServerProperties.getProperty( 
     
    346345 
    347346        theViewer.setVisible(true); 
     347        // I think the traffic mgr has to run AFTER viewer is set visible, because 
     348        // inside run() it loads traffic events and wants to notify the view 
     349        // to update itself ... so the view must be visible first? 
     350        // When this stmt was prior to setVisible, we hung on setVisible. 
     351        theTrafficMgr.run(); 
    348352 
    349353    } 
  • trunk/src/tmcsim/cadsimulator/managers/TrafficModelManager.java

    r524 r654  
    3030import javax.swing.JWindow; 
    3131import javax.swing.Timer; 
     32import java.util.TimerTask; 
    3233import javax.swing.UIManager; 
    3334import tmcsim.cadsimulator.Coordinator; 
     
    146147        loadEvents(); 
    147148 
     149        java.util.Timer t = new java.util.Timer(); 
     150 
    148151        // Create a timer that fetches the simulation time every second. 
    149         Timer timer = new Timer(ONE_SECOND, new ActionListener() 
    150         { 
    151             // Every second, see if an event should be launched 
    152             public void actionPerformed(ActionEvent e) 
     152        t.scheduleAtFixedRate( 
     153            new TimerTask() 
    153154            { 
    154                 String currentATMStime = ""; 
    155                 Date simClock = new Date(); 
    156                 // Obtain the simulation time from the CAD server 
    157                 try 
     155                // Every second, see if an event should be launched 
     156                public void run() 
    158157                { 
    159                     long simtime = theCoordinator.getCurrentSimulationTime(); 
    160                     currentClock = theCoordinator.formatTimeInSeconds(simtime); 
    161                     // For Debugging, show the ATMS time 
    162 //                    long ATMStime = theCoorInt.getATMStime();        
    163 //                    Date atmsdate = new Date(ATMStime); 
    164 //                    currentATMStime = formatter.format(atmsdate); 
     158                    String currentATMStime = ""; 
     159                    Date simClock = new Date(); 
     160                    // Obtain the simulation time from the CAD server 
    165161                    try 
    166162                    { 
    167                         simClock = formatter.parse(currentClock); 
     163                        long simtime = theCoordinator.getCurrentSimulationTime(); 
     164                        currentClock = theCoordinator.formatTimeInSeconds(simtime); 
     165                        // For Debugging, show the ATMS time 
     166    //                    long ATMStime = theCoorInt.getATMStime();        
     167    //                    Date atmsdate = new Date(ATMStime); 
     168    //                    currentATMStime = formatter.format(atmsdate); 
     169                        try 
     170                        { 
     171                            simClock = formatter.parse(currentClock); 
     172                        } 
     173                        catch (ParseException ex) 
     174                        { 
     175                            Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null, ex); 
     176                            System.out.println("Invalid simulation clock time found"); 
     177                            System.exit(-1); 
     178                        } 
    168179                    } 
    169                     catch (ParseException ex) 
     180                    catch (RemoteException ex) 
    170181                    { 
     182                        System.out.println("Remote Exception reading sim or ATMS clock time"); 
    171183                        Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null, ex); 
    172                         System.out.println("Invalid simulation clock time found"); 
    173                         System.exit(-1); 
     184                    } 
     185                    // If we have any events left to process 
     186                    if (!eventQueue.isEmpty()) 
     187                    { 
     188                        // Get the time to launch the next event 
     189                        TrafficEvent nextEvent = eventQueue.peek(); 
     190                        Date eventTime = nextEvent.eventDate; 
     191                        // Check the queue of events to see if the first 
     192                        // item should be launched.  IF so,  
     193                        // issue that command and remove it from queue. 
     194                        if (eventTime.before(simClock) || eventTime.equals(simClock)) 
     195                        { 
     196                            System.out.println("LAUNCHING EVENT: " + nextEvent.toString()); 
     197                            // apply colorization to highways 
     198                            highways.applyColorToHighwayStretch(nextEvent.routeNumber, nextEvent.dir, 
     199                                    nextEvent.postmile, nextEvent.range, nextEvent.color); 
     200                            // Remove this event from the queue, we're done with it. 
     201                            eventQueue.remove(); 
     202                            setChanged(); 
     203                            // Send updated list to view 
     204                            // notifyObservers(getEventQueue()); 
     205                            // Notify view it should scroll to next event 
     206                            notifyObservers(new Integer(0)); 
     207                        } 
     208                        setChanged(); 
     209                        notifyObservers(currentClock); 
    174210                    } 
    175211                } 
    176                 catch (RemoteException ex) 
    177                 { 
    178                     System.out.println("Remote Exception reading sim or ATMS clock time"); 
    179                     Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null, ex); 
    180                 } 
    181                 // If we have any events left to process 
    182                 if (!eventQueue.isEmpty()) 
    183                 { 
    184                     // Get the time to launch the next event 
    185                     TrafficEvent nextEvent = eventQueue.peek(); 
    186                     Date eventTime = nextEvent.eventDate; 
    187                     // Check the queue of events to see if the first 
    188                     // item should be launched.  IF so,  
    189                     // issue that command and remove it from queue. 
    190                     if (eventTime.before(simClock) || eventTime.equals(simClock)) 
    191                     { 
    192                         System.out.println("LAUNCHING EVENT: " + nextEvent.toString()); 
    193                         // apply colorization to highways 
    194                         highways.applyColorToHighwayStretch(nextEvent.routeNumber, nextEvent.dir, 
    195                                 nextEvent.postmile, nextEvent.range, nextEvent.color); 
    196                         // Remove this event from the queue, we're done with it. 
    197                         eventQueue.remove(); 
    198                         setChanged(); 
    199                         // Send updated list to view 
    200                         // notifyObservers(getEventQueue()); 
    201                         // Notify view it should scroll to next event 
    202                         notifyObservers(new Integer(0)); 
    203                     } 
    204                     setChanged(); 
    205                     notifyObservers(currentClock); 
    206                 } 
    207             } 
    208         }); 
    209         timer.start(); 
    210  
    211         // Always write to json for google map display 
    212         Thread wtJson = new WriteToJsonThread(); 
     212            }, 
     213            0,  
     214            ONE_SECOND 
     215            ); 
     216 
     217        // Also write to json for google map display, every ten seconds. 
     218        WriteToJsonTask wtJson = new WriteToJsonTask(); 
    213219        wtJson.start(); 
     220        System.out.println("Traffic Model Mgr init complete."); 
    214221    } 
    215222    /** Accessor to event queue 
     
    401408     *  by Google Maps. 
    402409     */ 
    403     class WriteToJsonThread extends Thread 
    404     { 
    405  
    406         public void run() 
    407         { 
    408             System.out.println("WriteToJson Thread starting."); 
    409             // Run indefinitely 
    410             while (true) 
    411             { 
    412                  // Write the highway network status to Json 
    413                 String geojson = highways.toJson(); 
    414                 PrintWriter out; 
    415                 try 
     410    class WriteToJsonTask 
     411    { 
     412        void start() 
     413        { 
     414            System.out.println("WriteToJson Task starting."); 
     415            java.util.Timer tasktimer = new java.util.Timer(); 
     416            tasktimer.scheduleAtFixedRate( 
     417                new TimerTask() 
    416418                { 
    417                     // currently writes to local file 
    418                     out = new PrintWriter(jsonPath); 
    419                     out.print(geojson); 
    420                     out.close(); 
    421                 } 
    422                 catch (FileNotFoundException ex) 
    423                 { 
    424                     Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null, ex); 
    425                 } 
    426                 // Wait for Google Map to process the data we just sent 
    427                 try 
    428                 { 
    429                     Thread.sleep(10000); 
    430                 } 
    431                 catch (InterruptedException ie) 
    432                 { 
    433                     ie.printStackTrace(); 
    434                 } 
    435             } 
    436  
     419                    public void run() 
     420                    { 
     421                        String geojson = highways.toJson(); 
     422                        PrintWriter out; 
     423                        try 
     424                        { 
     425                            // currently writes to local file 
     426                            out = new PrintWriter(jsonPath); 
     427                            out.print(geojson); 
     428                            out.close(); 
     429                        } 
     430                        catch (FileNotFoundException ex) 
     431                        { 
     432                            Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null, ex); 
     433                        } 
     434                    }     
     435                }, 
     436                0,   // run first occurrence now 
     437                10000); // run every ten seconds 
    437438        } 
    438439    } 
Note: See TracChangeset for help on using the changeset viewer.