Changeset 112 in tmcsimulator for trunk/src


Ignore:
Timestamp:
10/13/2017 02:54:56 PM (9 years ago)
Author:
jdalbey
Message:

ATMSDriverClient.java Enhanced data validation and additional logic to launch events that are earlier than simulation time.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tmcsim/client/ATMSDriverClient.java

    r109 r112  
    1111import java.rmi.RemoteException; 
    1212import java.rmi.server.UnicastRemoteObject; 
     13import java.text.ParseException; 
     14import java.text.SimpleDateFormat; 
     15import java.util.Date; 
     16import java.util.InputMismatchException; 
    1317import java.util.LinkedList; 
    1418import java.util.Properties; 
     
    3438 * to fire update commands at the desired time. 
    3539 * Note: Sim Mgr must be running before starting this application. 
    36  * 
     40 * TODO: We probably want to be able to "override" a command, to force  
     41 * clearing an incident. 
     42 
    3743 * @author jdalbey 
    3844 */ 
     
    4248    private static final String CONFIG_FILE_NAME = "cad_client_config.properties"; 
    4349    private final static int ONE_SECOND = 1000; 
     50    private final static SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); 
    4451    /** 
    4552     * Error logger. 
     
    126133        // READ THE BATCH FILE OF COMMANDS and put in a queue 
    127134        readBatchFile(); 
    128         // Consider special cases:  1) time to fire first command 
    129         // has already past when application starts. 
    130         // 2.  Two commands have same fire time specified. 
    131         // 3.  How to "override" a command, to clear an incident. 
    132135         
    133136        // Create a timer that fetches the simulation time every second. 
     
    137140            { 
    138141                String currentClock = ""; 
     142                Date simClock = new Date();                 
     143                // Obtain the simulation time from the CAD server 
    139144                try 
    140145                { 
    141146                    long simtime = theCoorInt.getCurrentSimulationTime(); 
    142147                    currentClock = formatInterval(simtime); 
     148                    try { 
     149                        simClock = formatter.parse(currentClock); 
     150                    } catch (ParseException ex) { 
     151                        Logger.getLogger(ATMSDriverClient.class.getName()).log(Level.SEVERE, null, ex); 
     152                        System.out.println("Invalid simulation clock time found in ATMSDriverClient"); 
     153                        System.exit(-1); 
     154                    }                     
    143155                    System.out.println("Current clock: " + currentClock); 
    144156                } catch (RemoteException ex) 
     
    146158                    Logger.getLogger(ATMSDriverClient.class.getName()).log(Level.SEVERE, null, ex); 
    147159                } 
     160                // If we have any events left to process 
    148161                if (!eventQueue.isEmpty()) 
    149162                { 
    150                     // Check the queue of commands to see if the first 
    151                     // item matches the current time.  IF so,  
     163                    // Get the time to launch the next event 
     164                    String nextEvent = eventQueue.peek(); 
     165                    String eventTimeField = nextEvent.substring(0,8); 
     166                    Date eventTime = new Date(); 
     167                    try { 
     168                        eventTime = formatter.parse(eventTimeField); 
     169                    } catch (ParseException ex) { 
     170                        Logger.getLogger(ATMSDriverClient.class.getName()).log(Level.WARNING, null, ex); 
     171                        System.out.println("Unable to parse event time: " + nextEvent + " skipping."); 
     172                        eventQueue.remove(); 
     173                    } 
     174                    System.out.println("Next event will be launched at: " + formatter.format(eventTime)); 
     175                    // Check the queue of events to see if the first 
     176                    // item should be launched.  IF so,  
    152177                    // issue that command and remove it from queue. 
    153                     String nextEvent = eventQueue.peek(); 
    154                     String eventTime = nextEvent.substring(0,8); 
    155                     System.out.println("Next event will be launched at: " + eventTime); 
    156                     if (eventTime.equals(currentClock)) 
     178                    if (eventTime.before(simClock) || eventTime.equals(simClock)) 
    157179                    { 
    158180                        System.out.println("LAUNCHING EVENT at " + nextEvent ); 
    159181                        // Extract fields from event and prepare them  
    160182                        Scanner lineScan = new Scanner(nextEvent); 
     183                        try 
     184                        { 
    161185                        lineScan.next(); // skip time field 
    162186                        int routeNumber = lineScan.nextInt(); 
     
    169193                        // Remove this event from the queue, we're done with it. 
    170194                        eventQueue.remove(); 
     195                        } 
     196                        catch (InputMismatchException ex) 
     197                        { 
     198                            System.out.println("Wrong format data in batch event file: " + nextEvent + " \nskipping."); 
     199                            eventQueue.remove(); 
     200                        } 
    171201                    } 
    172202                } 
Note: See TracChangeset for help on using the changeset viewer.