Changeset 183 in tmcsimulator for trunk/src/tmcsim/client/ATMSBatchDriver.java
- Timestamp:
- 10/28/2017 03:51:23 PM (9 years ago)
- File:
-
- 1 edited
-
trunk/src/tmcsim/client/ATMSBatchDriver.java (modified) (23 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/tmcsim/client/ATMSBatchDriver.java
r180 r183 1 1 package tmcsim.client; 2 2 3 import atmsdriver.ATMSDriver;4 3 import atmsdriver.ConsoleTrafficDriver; 5 import atmsdriver.ExchangeInfo;6 4 import atmsdriver.model.Highways; 7 import atmsdriver.model. Station;5 import atmsdriver.model.TrafficEvent; 8 6 import atmsdriver.model.LoopDetector.DOTCOLOR; 9 7 import java.awt.event.ActionEvent; … … 17 15 import java.text.SimpleDateFormat; 18 16 import java.util.ArrayList; 17 import java.util.Collections; 19 18 import java.util.Date; 20 19 import java.util.HashMap; 21 import java.util.InputMismatchException;22 20 import java.util.LinkedList; 23 21 import java.util.List; 24 22 import java.util.Map; 25 23 import java.util.Properties; 26 import java.util.Queue;27 24 import java.util.Scanner; 28 25 import java.util.concurrent.TimeUnit; … … 33 30 import javax.swing.Timer; 34 31 import javax.swing.UIManager; 35 import tmcsim.cadsimulator.CADServer;36 32 import tmcsim.common.SimulationException; 37 33 import tmcsim.interfaces.CADClientInterface; … … 39 35 40 36 /** 41 * Skeleton for ATMS Driver that reads a "batch" file of highway 42 * status update commands. 43 * It operates as a client of the 44 * CAD server, using RMI to poll the server every second for the current 45 * simulation clock time. It uses the simulation clock time 46 * to fire update commands at the desired time. 47 * Note: Sim Mgr must be running before starting this application. 48 * TODO: We probably want to be able to "override" a command, to force 49 * clearing an incident. 50 37 * Skeleton for ATMS Driver that reads a "batch" file of highway status update 38 * commands. It operates as a client of the CAD server, using RMI to poll the 39 * server every second for the current simulation clock time. It uses the 40 * simulation clock time to fire update commands at the desired time. Note: Sim 41 * Mgr must be running before starting this application. TODO: We probably want 42 * to be able to "override" a command, to force clearing an incident. 43 * 51 44 * @author jdalbey 52 45 */ … … 54 47 CADClientInterface 55 48 { 49 56 50 private static final String CONFIG_FILE_NAME = "cad_client_config.properties"; 57 51 private final static int ONE_SECOND = 1000; … … 79 73 { 80 74 CAD_SIM_HOST("CADSimulatorHost"), CAD_SIM_PORT("CADSimulatorSocketPort"), CAD_RMI_PORT( 81 "CADRmiPort"), CLIENT_CAD_POS("CADPosition"), CLIENT_USER_ID(82 "CADUserID"), KEYBOARD_TYPE("KeyboardType"), DISPLAY_TYPE(83 "DisplayType");75 "CADRmiPort"), CLIENT_CAD_POS("CADPosition"), CLIENT_USER_ID( 76 "CADUserID"), KEYBOARD_TYPE("KeyboardType"), DISPLAY_TYPE( 77 "DisplayType"); 84 78 public String name; 85 79 … … 112 106 */ 113 107 final private Highways highways; 114 115 /** 116 * Queueof batch events117 */ 118 private Queue<String> eventQueue;108 109 /** 110 * LinkedList of batch events 111 */ 112 private LinkedList<TrafficEvent> eventQueue; 119 113 /** 120 114 * Map of incidents to events 121 115 */ 122 private Map<String, List<String>> incidents; 123 124 /** Instance of ConsoleTrafficDriver that contains the highway model */ 116 private Map<String, List<TrafficEvent>> incidents; 117 118 /** 119 * Instance of ConsoleTrafficDriver that contains the highway model 120 */ 125 121 private ConsoleTrafficDriver console; 126 127 /** GUI for this driver */ 122 123 /** 124 * GUI for this driver 125 */ 128 126 private ATMSBatchViewer theView; 129 127 130 128 /** 131 129 * Constructor. Initialize data from parsed properties file. Create a socket … … 143 141 } 144 142 // Initialize the highway model 145 incidents = new HashMap<String, List< String>>();143 incidents = new HashMap<String, List<TrafficEvent>>(); 146 144 highways = new Highways( 147 "config/vds_data/lds.txt",148 "config/vds_data/loop.txt",149 "config/vds_data/highwaysMeta.txt",150 // "192.168.251.46", 8080); //IP address of FEP Sim Linux VM151 "localhost", 8080);152 145 "config/vds_data/lds.txt", 146 "config/vds_data/loop.txt", 147 "config/vds_data/highwaysMeta.txt", 148 // "192.168.251.46", 8080); //IP address of FEP Sim Linux VM 149 "localhost", 8080); 150 153 151 connect(cadClientProp.getProperty(PROPERTIES.CAD_SIM_HOST.name).trim(), 154 152 cadClientProp.getProperty(PROPERTIES.CAD_RMI_PORT.name).trim()); … … 169 167 String currentClock = ""; 170 168 String currentATMStime = ""; 171 Date simClock = new Date(); 169 Date simClock = new Date(); 172 170 // Obtain the simulation time from the CAD server 173 171 try … … 179 177 // Date atmsdate = new Date(ATMStime); 180 178 // currentATMStime = formatter.format(atmsdate); 181 try { 179 try 180 { 182 181 simClock = formatter.parse(currentClock); 183 } catch (ParseException ex) { 182 } 183 catch (ParseException ex) 184 { 184 185 Logger.getLogger(ATMSBatchDriver.class.getName()).log(Level.SEVERE, null, ex); 185 186 System.out.println("Invalid simulation clock time found in ATMSDriverClient"); 186 187 System.exit(-1); 187 } 188 } 188 189 //System.out.println("Current clock: " + currentClock); 189 } catch (RemoteException ex) 190 } 191 catch (RemoteException ex) 190 192 { 191 193 System.out.println("Remote Exception reading sim or ATMS clock time"); … … 196 198 { 197 199 // Get the time to launch the next event 198 String nextEvent = eventQueue.peek(); 199 // String eventTimeField = nextEvent.substring(0,8); 200 Scanner evtScan = new Scanner(nextEvent); 201 String inci = evtScan.next(); 202 String eventTimeField = evtScan.next(); 203 Date eventTime = new Date(); 204 try { 205 eventTime = formatter.parse(eventTimeField); 206 } catch (ParseException ex) { 207 Logger.getLogger(ATMSBatchDriver.class.getName()).log(Level.WARNING, null, ex); 208 System.out.println("Unable to parse event time: " + nextEvent + " skipping."); 209 eventQueue.remove(); 210 } 200 TrafficEvent nextEvent = eventQueue.peek(); 201 Date eventTime = nextEvent.eventDate; 211 202 //System.out.println("Next event will be launched at: " + formatter.format(eventTime)); 212 203 // Check the queue of events to see if the first … … 215 206 if (eventTime.before(simClock) || eventTime.equals(simClock)) 216 207 { 217 System.out.println("LAUNCHING EVENT at " + nextEvent ); 218 // Extract fields from event and prepare them 219 Scanner lineScan = new Scanner(nextEvent); 220 try 221 { 222 lineScan.next(); // skip incident number field 223 lineScan.next(); // skip time field 224 int routeNumber = lineScan.nextInt(); 225 Station.DIRECTION dir = Station.DIRECTION.toDirection(lineScan.next()); 226 double postmile = lineScan.nextDouble(); 227 double range = lineScan.nextDouble(); 228 DOTCOLOR dotcolor = DOTCOLOR.toDotColor(lineScan.next()); 208 System.out.println("LAUNCHING EVENT: " + nextEvent.toString()); 229 209 // apply colorization to highways 230 highways.applyColorToHighwayStretch(routeNumber, dir, postmile, range, dotcolor); 210 highways.applyColorToHighwayStretch(nextEvent.routeNumber, nextEvent.dir, 211 nextEvent.postmile, nextEvent.range, nextEvent.color); 231 212 // Remove this event from the queue, we're done with it. 232 213 eventQueue.remove(); 233 } 234 catch (InputMismatchException ex) 235 { 236 System.out.println("Wrong format data in batch event file: " + nextEvent + " \nskipping."); 237 eventQueue.remove(); 238 } 239 } 240 214 } 215 241 216 theView.update(currentClock, currentATMStime, eventQueue); 242 217 } … … 255 230 { 256 231 FileInputStream fis; 257 try { 232 try 233 { 258 234 fis = new FileInputStream("config/vds_data/atmsBatchEvents.txt"); 259 eventQueue = new LinkedList< String>();235 eventQueue = new LinkedList<TrafficEvent>(); 260 236 // Read all lines from the file of events 261 237 Scanner scan = new Scanner(fis); … … 263 239 { 264 240 // Read a line and add it to the event queue 265 String line = scan.nextLine() ;241 String line = scan.nextLine().trim(); 266 242 if (line.charAt(0) != '#') 267 243 { 268 eventQueue.add(line); 269 // Parse the incident from the line 270 Scanner lineScan = new Scanner(line); 271 String incident = lineScan.next(); 272 // Add the line to the list for the corresponding incident 273 List evtList; 274 if (incidents.containsKey(incident)) 275 { 276 evtList = incidents.get(incident); 277 } 278 else 279 { 280 evtList = new ArrayList<String>(); 281 } 282 evtList.add(line); 283 // and put it back in the map 284 incidents.put(incident, evtList); 244 TrafficEvent evt; 245 try 246 { 247 evt = new TrafficEvent(line); 248 eventQueue.add(evt); 249 String incident = evt.incident; 250 // Add the line to the list for the corresponding incident 251 List evtList; 252 if (incidents.containsKey(evt.incident)) 253 { 254 evtList = incidents.get(evt.incident); 255 } 256 else 257 { 258 evtList = new ArrayList<String>(); 259 } 260 evtList.add(evt); 261 // and put it back in the map 262 incidents.put(incident, evtList); 263 } 264 catch (ParseException ex) 265 { 266 Logger.getLogger(ATMSBatchDriver.class.getName()).log(Level.SEVERE, null, ex); 267 System.out.println("Wrong format data in batch event file: " + line + " \nskipping."); 268 System.out.println("Skipping badly formatted event."); 269 } 285 270 } 286 271 } 287 } catch (FileNotFoundException ex) { 272 } 273 catch (FileNotFoundException ex) 274 { 288 275 Logger.getLogger(ATMSBatchDriver.class.getName()).log(Level.SEVERE, null, ex); 289 276 } 290 277 System.out.println("Events file read, " + eventQueue.size() + " events queued."); 291 } 292 293 /** Clear an incident. For each event associated with an incident, 294 * turn the dots in its range Green and remove it from the event queue. 278 // Put the events in chronological order 279 Collections.sort(eventQueue); 280 } 281 282 /** 283 * Clear an incident. For each event associated with an incident, turn the 284 * dots in its range Green and remove it from the event queue. 285 * 295 286 * @param incidentNumber incident to be cleared. 296 287 */ … … 298 289 { 299 290 boolean ok = incidents.containsKey(incidentNumber); 300 if (!ok) 291 if (!ok) 301 292 { 302 293 System.out.println("Sorry, that incident number isn't found."); … … 304 295 } 305 296 System.out.println("Clearing incident " + incidentNumber); 306 List< String> events = incidents.get(incidentNumber);297 List<TrafficEvent> events = incidents.get(incidentNumber); 307 298 // Process each event associated with this incident 308 for ( String event: events)299 for (TrafficEvent event : events) 309 300 { 310 301 System.out.println("Event: " + event + " cleared."); 311 302 eventQueue.remove(event); 312 // Extract fields from event and prepare them 313 Scanner lineScan = new Scanner(event); 314 try 315 { 316 lineScan.next(); // skip incident number field 317 lineScan.next(); // skip time field 318 int routeNumber = lineScan.nextInt(); 319 Station.DIRECTION dir = Station.DIRECTION.toDirection(lineScan.next()); 320 double postmile = lineScan.nextDouble(); 321 double range = lineScan.nextDouble(); 322 // apply colorization to highways, forcing to green, indicating cleared 323 highways.applyColorToHighwayStretch(routeNumber, dir, postmile, range, DOTCOLOR.GREEN); 324 } 325 catch (InputMismatchException ex) 326 { 327 System.out.println("Internal error, please report to programmers." + event); 328 } 329 303 304 // apply colorization to highways, forcing to green, indicating cleared 305 highways.applyColorToHighwayStretch(event.routeNumber, event.dir, 306 event.postmile, event.range, DOTCOLOR.GREEN); 307 330 308 } 331 309 // Now refresh the view with the updated queue of events 332 310 theView.update("0:00", "0:00", eventQueue); 333 311 } 334 312 335 313 /** 336 314 * Connect to the Coordinator's RMI object, and register this object for … … 354 332 theCoorInt = (CoordinatorInterface) Naming.lookup(coorIntURL); 355 333 theCoorInt.registerForCallback(this); 356 } catch (Exception e) 334 } 335 catch (Exception e) 357 336 { 358 337 throw new SimulationException(SimulationException.CAD_SIM_CONNECT, 359 338 e); 360 339 361 340 } 362 341 } … … 384 363 cadClientProp = new Properties(); 385 364 cadClientProp.load(new FileInputStream(propertiesFile)); 386 } catch (Exception e) 365 } 366 catch (Exception e) 387 367 { 388 368 cadClientLogger.logp(Level.SEVERE, "SimulationManager", … … 392 372 e); 393 373 } 394 395 374 396 375 // Ensure that the properties file does not have null values for the … … 430 409 { 431 410 theCoorInt.unregisterForCallback(client); 432 } catch (RemoteException e) 411 } 412 catch (RemoteException e) 433 413 { 434 414 e.printStackTrace(); … … 456 436 new ATMSBatchDriver(System.getProperty("CONFIG_DIR") + System.getProperty("file.separator") + CONFIG_FILE_NAME); 457 437 458 } catch (Exception e) 438 } 439 catch (Exception e) 459 440 { 460 441 cadClientLogger.logp(Level.SEVERE, "SimulationManager", "Main", … … 478 459 while (true) 479 460 { 480 try { 461 try 462 { 481 463 // Write the highway network status to the FEP Simulator 482 464 highways.writeToFEP(); 483 } catch (SimulationException ex) 465 } 466 catch (SimulationException ex) 484 467 { 485 468 // Ask user if they want to proceed without FEP Sim connection … … 487 470 if (reply == JOptionPane.NO_OPTION) 488 471 { 489 System.exit(0);472 System.exit(0); 490 473 } 491 474 System.out.println("Skipping writeToFEP...");
Note: See TracChangeset
for help on using the changeset viewer.
