| 1 | package tmcsim.client; |
|---|
| 2 | |
|---|
| 3 | import atmsdriver.ATMSDriver; |
|---|
| 4 | import atmsdriver.ConsoleDriver; |
|---|
| 5 | import atmsdriver.ExchangeInfo; |
|---|
| 6 | import atmsdriver.model.Highways; |
|---|
| 7 | import atmsdriver.model.Station; |
|---|
| 8 | import java.awt.event.ActionEvent; |
|---|
| 9 | import java.awt.event.ActionListener; |
|---|
| 10 | import java.io.FileInputStream; |
|---|
| 11 | import java.io.FileNotFoundException; |
|---|
| 12 | import java.rmi.Naming; |
|---|
| 13 | import java.rmi.RemoteException; |
|---|
| 14 | import java.rmi.server.UnicastRemoteObject; |
|---|
| 15 | import java.text.ParseException; |
|---|
| 16 | import java.text.SimpleDateFormat; |
|---|
| 17 | import java.util.Date; |
|---|
| 18 | import java.util.InputMismatchException; |
|---|
| 19 | import java.util.LinkedList; |
|---|
| 20 | import java.util.Properties; |
|---|
| 21 | import java.util.Queue; |
|---|
| 22 | import java.util.Scanner; |
|---|
| 23 | import java.util.concurrent.TimeUnit; |
|---|
| 24 | import java.util.logging.Level; |
|---|
| 25 | import java.util.logging.Logger; |
|---|
| 26 | import javax.swing.JOptionPane; |
|---|
| 27 | import javax.swing.JWindow; |
|---|
| 28 | import javax.swing.Timer; |
|---|
| 29 | import javax.swing.UIManager; |
|---|
| 30 | import tmcsim.common.SimulationException; |
|---|
| 31 | import tmcsim.interfaces.CADClientInterface; |
|---|
| 32 | import tmcsim.interfaces.CoordinatorInterface; |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * Skeleton for ATMS Driver that reads a "batch" file of highway |
|---|
| 36 | * status update commands. |
|---|
| 37 | * It operates as a client of the |
|---|
| 38 | * CAD server, using RMI to poll the server every second for the current |
|---|
| 39 | * simulation clock time. It uses the simulation clock time |
|---|
| 40 | * to fire update commands at the desired time. |
|---|
| 41 | * Note: Sim Mgr must be running before starting this application. |
|---|
| 42 | * TODO: We probably want to be able to "override" a command, to force |
|---|
| 43 | * clearing an incident. |
|---|
| 44 | |
|---|
| 45 | * @author jdalbey |
|---|
| 46 | */ |
|---|
| 47 | public class ATMSBatchDriver extends UnicastRemoteObject implements |
|---|
| 48 | CADClientInterface |
|---|
| 49 | { |
|---|
| 50 | private static final String CONFIG_FILE_NAME = "cad_client_config.properties"; |
|---|
| 51 | private final static int ONE_SECOND = 1000; |
|---|
| 52 | private static final int FEPSIM_INTERVAL = 30000; |
|---|
| 53 | private final static SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); |
|---|
| 54 | /** |
|---|
| 55 | * Error logger. |
|---|
| 56 | */ |
|---|
| 57 | private static Logger cadClientLogger = Logger.getLogger("tmcsim.client"); |
|---|
| 58 | |
|---|
| 59 | @Override |
|---|
| 60 | public void refresh() throws RemoteException |
|---|
| 61 | { |
|---|
| 62 | System.out.println("ATMSBatchDriver.refresh() was invoked."); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | * Enumeration containing properties name values. See CADClient class |
|---|
| 67 | * description for more information. |
|---|
| 68 | * |
|---|
| 69 | * @author Matthew Cechini |
|---|
| 70 | * @see CADClient |
|---|
| 71 | */ |
|---|
| 72 | private static enum PROPERTIES |
|---|
| 73 | { |
|---|
| 74 | CAD_SIM_HOST("CADSimulatorHost"), CAD_SIM_PORT("CADSimulatorSocketPort"), CAD_RMI_PORT( |
|---|
| 75 | "CADRmiPort"), CLIENT_CAD_POS("CADPosition"), CLIENT_USER_ID( |
|---|
| 76 | "CADUserID"), KEYBOARD_TYPE("KeyboardType"), DISPLAY_TYPE( |
|---|
| 77 | "DisplayType"); |
|---|
| 78 | public String name; |
|---|
| 79 | |
|---|
| 80 | private PROPERTIES(String n) |
|---|
| 81 | { |
|---|
| 82 | name = n; |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | /** |
|---|
| 86 | * CADClientSocket Object to handle socket communication between the Client |
|---|
| 87 | * and CAD Simulator. |
|---|
| 88 | */ |
|---|
| 89 | private CADClientSocket theClientSocket; |
|---|
| 90 | |
|---|
| 91 | /** |
|---|
| 92 | * Properties object for the CADClient class. |
|---|
| 93 | */ |
|---|
| 94 | private Properties cadClientProp; |
|---|
| 95 | /** |
|---|
| 96 | * RMI interface for communication with the remote Coordinator. |
|---|
| 97 | */ |
|---|
| 98 | private static CoordinatorInterface theCoorInt; |
|---|
| 99 | /** |
|---|
| 100 | * reference to itself to be used for disconnecting from CADSimulator |
|---|
| 101 | */ |
|---|
| 102 | private CADClientInterface client = this; |
|---|
| 103 | |
|---|
| 104 | /** |
|---|
| 105 | * Highways in traffic network |
|---|
| 106 | */ |
|---|
| 107 | final private Highways highways; |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * Queue of batch events |
|---|
| 111 | */ |
|---|
| 112 | private Queue<String> eventQueue; |
|---|
| 113 | |
|---|
| 114 | /** Instance of ConsoleDriver that contains the highway model */ |
|---|
| 115 | private ConsoleDriver console; |
|---|
| 116 | |
|---|
| 117 | /** |
|---|
| 118 | * Constructor. Initialize data from parsed properties file. Create a socket |
|---|
| 119 | * connection to the CADSimulator. |
|---|
| 120 | * |
|---|
| 121 | * @param propertiesFile File path (absolute or relative) to the properties |
|---|
| 122 | * file containing configuration data. |
|---|
| 123 | */ |
|---|
| 124 | public ATMSBatchDriver(String propertiesFile) throws SimulationException, |
|---|
| 125 | RemoteException |
|---|
| 126 | { |
|---|
| 127 | if (!verifyProperties(propertiesFile)) |
|---|
| 128 | { |
|---|
| 129 | System.exit(0); |
|---|
| 130 | } |
|---|
| 131 | highways = new Highways( |
|---|
| 132 | "config/vds_data/lds.txt", |
|---|
| 133 | "config/vds_data/loop.txt", |
|---|
| 134 | "config/vds_data/highwaysMeta.txt", |
|---|
| 135 | "localhost", 8080); |
|---|
| 136 | // Create console driver but don't start run() method |
|---|
| 137 | console = new ConsoleDriver(highways); |
|---|
| 138 | |
|---|
| 139 | connect(cadClientProp.getProperty(PROPERTIES.CAD_SIM_HOST.name).trim(), |
|---|
| 140 | cadClientProp.getProperty(PROPERTIES.CAD_RMI_PORT.name).trim()); |
|---|
| 141 | |
|---|
| 142 | // READ THE BATCH FILE OF COMMANDS and put in a queue |
|---|
| 143 | readBatchFile(); |
|---|
| 144 | // Create a timer that fetches the simulation time every second. |
|---|
| 145 | Timer timer = new Timer(ONE_SECOND, new ActionListener() |
|---|
| 146 | { |
|---|
| 147 | // Every second, see if an event should be launched |
|---|
| 148 | public void actionPerformed(ActionEvent e) |
|---|
| 149 | { |
|---|
| 150 | String currentClock = ""; |
|---|
| 151 | Date simClock = new Date(); |
|---|
| 152 | // Obtain the simulation time from the CAD server |
|---|
| 153 | try |
|---|
| 154 | { |
|---|
| 155 | long simtime = theCoorInt.getCurrentSimulationTime(); |
|---|
| 156 | currentClock = formatInterval(simtime); |
|---|
| 157 | try { |
|---|
| 158 | simClock = formatter.parse(currentClock); |
|---|
| 159 | } catch (ParseException ex) { |
|---|
| 160 | Logger.getLogger(ATMSBatchDriver.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 161 | System.out.println("Invalid simulation clock time found in ATMSDriverClient"); |
|---|
| 162 | System.exit(-1); |
|---|
| 163 | } |
|---|
| 164 | System.out.println("Current clock: " + currentClock); |
|---|
| 165 | } catch (RemoteException ex) |
|---|
| 166 | { |
|---|
| 167 | Logger.getLogger(ATMSBatchDriver.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 168 | } |
|---|
| 169 | // If we have any events left to process |
|---|
| 170 | if (!eventQueue.isEmpty()) |
|---|
| 171 | { |
|---|
| 172 | // Get the time to launch the next event |
|---|
| 173 | String nextEvent = eventQueue.peek(); |
|---|
| 174 | String eventTimeField = nextEvent.substring(0,8); |
|---|
| 175 | Date eventTime = new Date(); |
|---|
| 176 | try { |
|---|
| 177 | eventTime = formatter.parse(eventTimeField); |
|---|
| 178 | } catch (ParseException ex) { |
|---|
| 179 | Logger.getLogger(ATMSBatchDriver.class.getName()).log(Level.WARNING, null, ex); |
|---|
| 180 | System.out.println("Unable to parse event time: " + nextEvent + " skipping."); |
|---|
| 181 | eventQueue.remove(); |
|---|
| 182 | } |
|---|
| 183 | System.out.println("Next event will be launched at: " + formatter.format(eventTime)); |
|---|
| 184 | // Check the queue of events to see if the first |
|---|
| 185 | // item should be launched. IF so, |
|---|
| 186 | // issue that command and remove it from queue. |
|---|
| 187 | if (eventTime.before(simClock) || eventTime.equals(simClock)) |
|---|
| 188 | { |
|---|
| 189 | System.out.println("LAUNCHING EVENT at " + nextEvent ); |
|---|
| 190 | // Extract fields from event and prepare them |
|---|
| 191 | Scanner lineScan = new Scanner(nextEvent); |
|---|
| 192 | try |
|---|
| 193 | { |
|---|
| 194 | lineScan.next(); // skip time field |
|---|
| 195 | int routeNumber = lineScan.nextInt(); |
|---|
| 196 | Station.DIRECTION dir = Station.DIRECTION.toDirection(lineScan.next()); |
|---|
| 197 | double postmile = lineScan.nextDouble(); |
|---|
| 198 | int range = lineScan.nextInt(); |
|---|
| 199 | ConsoleDriver.DOTCOLOR dotcolor = ConsoleDriver.DOTCOLOR.toDotColor(lineScan.next()); |
|---|
| 200 | // apply colorization to highways |
|---|
| 201 | console.applyColorToHighwayStretch(routeNumber, dir, postmile, range, dotcolor); |
|---|
| 202 | // Remove this event from the queue, we're done with it. |
|---|
| 203 | eventQueue.remove(); |
|---|
| 204 | } |
|---|
| 205 | catch (InputMismatchException ex) |
|---|
| 206 | { |
|---|
| 207 | System.out.println("Wrong format data in batch event file: " + nextEvent + " \nskipping."); |
|---|
| 208 | eventQueue.remove(); |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | } |
|---|
| 212 | } |
|---|
| 213 | }); |
|---|
| 214 | timer.start(); |
|---|
| 215 | |
|---|
| 216 | // Start the FEP thread (to update ATMS every 30 sec). (See class def below) |
|---|
| 217 | new WriteToFEPThread().run(); |
|---|
| 218 | |
|---|
| 219 | ensureProperShutdown(); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | private void readBatchFile() |
|---|
| 223 | { |
|---|
| 224 | FileInputStream fis; |
|---|
| 225 | try { |
|---|
| 226 | fis = new FileInputStream("config/vds_data/atmsBatchEvents.txt"); |
|---|
| 227 | Scanner scan = new Scanner(fis); |
|---|
| 228 | eventQueue = new LinkedList<String>(); |
|---|
| 229 | while (scan.hasNext()) |
|---|
| 230 | { |
|---|
| 231 | eventQueue.add(scan.nextLine()); |
|---|
| 232 | } |
|---|
| 233 | } catch (FileNotFoundException ex) { |
|---|
| 234 | Logger.getLogger(ATMSBatchDriver.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 235 | } |
|---|
| 236 | System.out.println("Events file read, " + eventQueue.size() + " events queued."); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | /** |
|---|
| 240 | * Connect to the Coordinator's RMI object, and register this object for |
|---|
| 241 | * callback with the Coordinator. |
|---|
| 242 | * |
|---|
| 243 | * @param hostname Host name of the CAD Simulator. |
|---|
| 244 | * @param portNumber Port number of the CAD Simulator RMI communication. |
|---|
| 245 | * @throws SimulationException if there is an error creating the RMI |
|---|
| 246 | * connection. |
|---|
| 247 | */ |
|---|
| 248 | protected void connect(String hostname, String portNumber) |
|---|
| 249 | throws SimulationException |
|---|
| 250 | { |
|---|
| 251 | |
|---|
| 252 | String coorIntURL = ""; |
|---|
| 253 | |
|---|
| 254 | try |
|---|
| 255 | { |
|---|
| 256 | coorIntURL = "rmi://" + hostname + ":" + portNumber |
|---|
| 257 | + "/coordinator"; |
|---|
| 258 | theCoorInt = (CoordinatorInterface) Naming.lookup(coorIntURL); |
|---|
| 259 | theCoorInt.registerForCallback(this); |
|---|
| 260 | } catch (Exception e) |
|---|
| 261 | { |
|---|
| 262 | throw new SimulationException(SimulationException.CAD_SIM_CONNECT, |
|---|
| 263 | e); |
|---|
| 264 | } |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | /** |
|---|
| 268 | * This method verifies that the CAD Simulator Host and Port values are not |
|---|
| 269 | * null. Also, if a CAD Position or User ID do not exist in the properties |
|---|
| 270 | * file, the user is prompted to enter values. These values are written to |
|---|
| 271 | * the properties file. If the user cancels the process of entering these |
|---|
| 272 | * values, the verification fails. |
|---|
| 273 | * |
|---|
| 274 | * @param propertiesFile File path (absolute or relative) to the properties |
|---|
| 275 | * file containing configuration data. |
|---|
| 276 | * @return True if the properties file is valid, false if not. |
|---|
| 277 | * @throws SimulationException if there is an exception in verifying the |
|---|
| 278 | * properties file, or if the user cancels input. |
|---|
| 279 | */ |
|---|
| 280 | private boolean verifyProperties(String propertiesFile) |
|---|
| 281 | throws SimulationException |
|---|
| 282 | { |
|---|
| 283 | |
|---|
| 284 | // Load the properties file. |
|---|
| 285 | try |
|---|
| 286 | { |
|---|
| 287 | cadClientProp = new Properties(); |
|---|
| 288 | cadClientProp.load(new FileInputStream(propertiesFile)); |
|---|
| 289 | } catch (Exception e) |
|---|
| 290 | { |
|---|
| 291 | cadClientLogger.logp(Level.SEVERE, "SimulationManager", |
|---|
| 292 | "Constructor", "Exception in reading properties file.", e); |
|---|
| 293 | |
|---|
| 294 | throw new SimulationException(SimulationException.INITIALIZE_ERROR, |
|---|
| 295 | e); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | // Ensure that the properties file does not have null values for the |
|---|
| 300 | // CAD Simulator's connection information. |
|---|
| 301 | if (cadClientProp.getProperty(PROPERTIES.CAD_SIM_HOST.name) == null |
|---|
| 302 | || cadClientProp.getProperty(PROPERTIES.CAD_SIM_PORT.name) == null) |
|---|
| 303 | { |
|---|
| 304 | cadClientLogger.logp(Level.SEVERE, "SimulationManager", |
|---|
| 305 | "Constructor", "Null value in properties file."); |
|---|
| 306 | throw new SimulationException(SimulationException.INITIALIZE_ERROR); |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | return true; |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | /** |
|---|
| 313 | * Format a time in seconds as HH:MM:SS |
|---|
| 314 | * |
|---|
| 315 | * @param l |
|---|
| 316 | * @return |
|---|
| 317 | */ |
|---|
| 318 | private String formatInterval(final long l) |
|---|
| 319 | { |
|---|
| 320 | final long hr = TimeUnit.SECONDS.toHours(l); |
|---|
| 321 | final long min = TimeUnit.SECONDS.toMinutes(l - TimeUnit.HOURS.toSeconds(hr)); |
|---|
| 322 | final long sec = TimeUnit.SECONDS.toSeconds(l - TimeUnit.HOURS.toSeconds(hr) - TimeUnit.MINUTES.toSeconds(min)); |
|---|
| 323 | return String.format("%02d:%02d:%02d", hr, min, sec); |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | public void ensureProperShutdown() |
|---|
| 327 | { |
|---|
| 328 | Runtime.getRuntime().addShutdownHook(new Thread() |
|---|
| 329 | { |
|---|
| 330 | public void run() |
|---|
| 331 | { |
|---|
| 332 | try |
|---|
| 333 | { |
|---|
| 334 | theCoorInt.unregisterForCallback(client); |
|---|
| 335 | } catch (RemoteException e) |
|---|
| 336 | { |
|---|
| 337 | e.printStackTrace(); |
|---|
| 338 | } |
|---|
| 339 | } |
|---|
| 340 | }); |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | /** |
|---|
| 344 | * Construct the CADClient with the properties file path, either from the |
|---|
| 345 | * command line arguments or default. |
|---|
| 346 | * |
|---|
| 347 | * @param args Command line arguments. |
|---|
| 348 | */ |
|---|
| 349 | public static void main(String[] args) |
|---|
| 350 | { |
|---|
| 351 | if (System.getProperty("CONFIG_DIR") == null) |
|---|
| 352 | { |
|---|
| 353 | System.setProperty("CONFIG_DIR", "config"); |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | try |
|---|
| 357 | { |
|---|
| 358 | UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
|---|
| 359 | new ATMSBatchDriver(System.getProperty("CONFIG_DIR") + System.getProperty("file.separator") + CONFIG_FILE_NAME); |
|---|
| 360 | |
|---|
| 361 | } catch (Exception e) |
|---|
| 362 | { |
|---|
| 363 | cadClientLogger.logp(Level.SEVERE, "SimulationManager", "Main", |
|---|
| 364 | "Error initializing application."); |
|---|
| 365 | |
|---|
| 366 | JOptionPane.showMessageDialog(new JWindow(), e.getMessage(), |
|---|
| 367 | "Error - Program Exiting", JOptionPane.ERROR_MESSAGE); |
|---|
| 368 | |
|---|
| 369 | System.exit(-1); |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | class WriteToFEPThread extends Thread |
|---|
| 375 | { |
|---|
| 376 | |
|---|
| 377 | public void run() |
|---|
| 378 | { |
|---|
| 379 | System.out.println("WriteToFEP Thread starting."); |
|---|
| 380 | // Run indefinitely |
|---|
| 381 | while (true) |
|---|
| 382 | { |
|---|
| 383 | // Write the highway network status to the FEP Simulator |
|---|
| 384 | highways.writeToFEP(); |
|---|
| 385 | |
|---|
| 386 | // Wait for FEP Sim to process the data we just sent |
|---|
| 387 | try |
|---|
| 388 | { |
|---|
| 389 | Thread.sleep(FEPSIM_INTERVAL); |
|---|
| 390 | } |
|---|
| 391 | catch (InterruptedException ie) |
|---|
| 392 | { |
|---|
| 393 | ie.printStackTrace(); |
|---|
| 394 | } |
|---|
| 395 | } |
|---|
| 396 | |
|---|
| 397 | } |
|---|
| 398 | } |
|---|
| 399 | } |
|---|