| 1 | package atmsdriver; |
|---|
| 2 | |
|---|
| 3 | import tmcsim.client.*; |
|---|
| 4 | import atmsdriver.model.Highways; |
|---|
| 5 | import atmsdriver.model.TrafficEvent; |
|---|
| 6 | import atmsdriver.model.LoopDetector.DOTCOLOR; |
|---|
| 7 | import java.awt.event.ActionEvent; |
|---|
| 8 | import java.awt.event.ActionListener; |
|---|
| 9 | import java.io.FileInputStream; |
|---|
| 10 | import java.io.FileNotFoundException; |
|---|
| 11 | import java.rmi.RemoteException; |
|---|
| 12 | import java.rmi.server.UnicastRemoteObject; |
|---|
| 13 | import java.text.ParseException; |
|---|
| 14 | import java.text.SimpleDateFormat; |
|---|
| 15 | import java.util.ArrayList; |
|---|
| 16 | import java.util.Collections; |
|---|
| 17 | import java.util.Date; |
|---|
| 18 | import java.util.HashMap; |
|---|
| 19 | import java.util.LinkedList; |
|---|
| 20 | import java.util.List; |
|---|
| 21 | import java.util.Map; |
|---|
| 22 | import java.util.Properties; |
|---|
| 23 | import java.util.Scanner; |
|---|
| 24 | import java.util.concurrent.TimeUnit; |
|---|
| 25 | import java.util.logging.Level; |
|---|
| 26 | import java.util.logging.Logger; |
|---|
| 27 | import javax.swing.JOptionPane; |
|---|
| 28 | import javax.swing.JWindow; |
|---|
| 29 | import javax.swing.Timer; |
|---|
| 30 | import javax.swing.UIManager; |
|---|
| 31 | import tmcsim.common.SimulationException; |
|---|
| 32 | import tmcsim.interfaces.CADClientInterface; |
|---|
| 33 | import tmcsim.interfaces.CoordinatorInterface; |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * Skeleton for ATMS Driver that reads a "batch" file of highway status update |
|---|
| 37 | * commands. A console display of the highway network is output |
|---|
| 38 | * for each event. |
|---|
| 39 | * @author jdalbey |
|---|
| 40 | */ |
|---|
| 41 | public class TrafficModelEventDriver extends UnicastRemoteObject implements |
|---|
| 42 | CADClientInterface |
|---|
| 43 | { |
|---|
| 44 | private final static SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); |
|---|
| 45 | /** |
|---|
| 46 | * Error logger. |
|---|
| 47 | */ |
|---|
| 48 | private static Logger cadClientLogger = Logger.getLogger("tmcsim.client"); |
|---|
| 49 | |
|---|
| 50 | @Override |
|---|
| 51 | public void refresh() throws RemoteException |
|---|
| 52 | { |
|---|
| 53 | System.out.println("ATMSBatchDriver.refresh() was invoked."); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | /** |
|---|
| 57 | * Highways in traffic network |
|---|
| 58 | */ |
|---|
| 59 | final private Highways highways; |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * LinkedList of batch events |
|---|
| 63 | */ |
|---|
| 64 | private LinkedList<TrafficEvent> eventQueue; |
|---|
| 65 | /** |
|---|
| 66 | * Map of incidents to events |
|---|
| 67 | */ |
|---|
| 68 | private Map<String, List<TrafficEvent>> incidents; |
|---|
| 69 | |
|---|
| 70 | /** |
|---|
| 71 | * Constructor. Initialize data from parsed properties file. Create a socket |
|---|
| 72 | * connection to the CADSimulator. |
|---|
| 73 | * |
|---|
| 74 | * @param propertiesFile File path (absolute or relative) to the properties |
|---|
| 75 | * file containing configuration data. |
|---|
| 76 | */ |
|---|
| 77 | public TrafficModelEventDriver() throws RemoteException |
|---|
| 78 | { |
|---|
| 79 | // Initialize the highway model |
|---|
| 80 | incidents = new HashMap<String, List<TrafficEvent>>(); |
|---|
| 81 | highways = new Highways( |
|---|
| 82 | "config/vds_data/highways_fullmap.txt", |
|---|
| 83 | // "192.168.251.46", 8080); //IP address of FEP Sim Linux VM |
|---|
| 84 | "localhost", 8080); |
|---|
| 85 | |
|---|
| 86 | // READ THE BATCH FILE OF COMMANDS and put in a queue |
|---|
| 87 | readBatchFile(); |
|---|
| 88 | |
|---|
| 89 | // If we have any events left to process |
|---|
| 90 | while (!eventQueue.isEmpty()) |
|---|
| 91 | { |
|---|
| 92 | // Get the time to launch the next event |
|---|
| 93 | TrafficEvent nextEvent = eventQueue.peek(); |
|---|
| 94 | System.out.println("LAUNCHING EVENT: " + nextEvent.toString()); |
|---|
| 95 | // apply colorization to highways |
|---|
| 96 | highways.applyColorToHighwayStretch(nextEvent.routeNumber, nextEvent.dir, |
|---|
| 97 | nextEvent.postmile, nextEvent.range, nextEvent.color); |
|---|
| 98 | System.out.println(highways.toString()); |
|---|
| 99 | // Remove this event from the queue, we're done with it. |
|---|
| 100 | eventQueue.remove(); |
|---|
| 101 | |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | private void readBatchFile() |
|---|
| 107 | { |
|---|
| 108 | FileInputStream fis; |
|---|
| 109 | try |
|---|
| 110 | { |
|---|
| 111 | fis = new FileInputStream("config/vds_data/atmsBatchEvents.txt"); |
|---|
| 112 | eventQueue = new LinkedList<TrafficEvent>(); |
|---|
| 113 | // Read all lines from the file of events |
|---|
| 114 | Scanner scan = new Scanner(fis); |
|---|
| 115 | while (scan.hasNext()) |
|---|
| 116 | { |
|---|
| 117 | // Read a line and add it to the event queue |
|---|
| 118 | String line = scan.nextLine().trim(); |
|---|
| 119 | if (line.charAt(0) != '#') |
|---|
| 120 | { |
|---|
| 121 | TrafficEvent evt; |
|---|
| 122 | try |
|---|
| 123 | { |
|---|
| 124 | evt = new TrafficEvent(line); |
|---|
| 125 | eventQueue.add(evt); |
|---|
| 126 | String incident = evt.incident; |
|---|
| 127 | // Add the line to the list for the corresponding incident |
|---|
| 128 | List evtList; |
|---|
| 129 | if (incidents.containsKey(evt.incident)) |
|---|
| 130 | { |
|---|
| 131 | evtList = incidents.get(evt.incident); |
|---|
| 132 | } |
|---|
| 133 | else |
|---|
| 134 | { |
|---|
| 135 | evtList = new ArrayList<String>(); |
|---|
| 136 | } |
|---|
| 137 | evtList.add(evt); |
|---|
| 138 | // and put it back in the map |
|---|
| 139 | incidents.put(incident, evtList); |
|---|
| 140 | } |
|---|
| 141 | catch (ParseException ex) |
|---|
| 142 | { |
|---|
| 143 | Logger.getLogger(TrafficModelEventDriver.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 144 | System.out.println("Wrong format data in batch event file: " + line + " \nskipping."); |
|---|
| 145 | System.out.println("Skipping badly formatted event."); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | catch (FileNotFoundException ex) |
|---|
| 151 | { |
|---|
| 152 | Logger.getLogger(TrafficModelEventDriver.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 153 | } |
|---|
| 154 | System.out.println("Events file read, " + eventQueue.size() + " events queued."); |
|---|
| 155 | // Put the events in chronological order |
|---|
| 156 | Collections.sort(eventQueue); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | /** |
|---|
| 160 | * Format a time in seconds as HH:MM:SS |
|---|
| 161 | * |
|---|
| 162 | * @param l |
|---|
| 163 | * @return |
|---|
| 164 | */ |
|---|
| 165 | private String formatInterval(final long l) |
|---|
| 166 | { |
|---|
| 167 | final long hr = TimeUnit.SECONDS.toHours(l); |
|---|
| 168 | final long min = TimeUnit.SECONDS.toMinutes(l - TimeUnit.HOURS.toSeconds(hr)); |
|---|
| 169 | final long sec = TimeUnit.SECONDS.toSeconds(l - TimeUnit.HOURS.toSeconds(hr) - TimeUnit.MINUTES.toSeconds(min)); |
|---|
| 170 | return String.format("%02d:%02d:%02d", hr, min, sec); |
|---|
| 171 | } |
|---|
| 172 | /** |
|---|
| 173 | * Construct the CADClient with the properties file path, either from the |
|---|
| 174 | * command line arguments or default. |
|---|
| 175 | * |
|---|
| 176 | * @param args Command line arguments. |
|---|
| 177 | */ |
|---|
| 178 | public static void main(String[] args) |
|---|
| 179 | { |
|---|
| 180 | if (System.getProperty("CONFIG_DIR") == null) |
|---|
| 181 | { |
|---|
| 182 | System.setProperty("CONFIG_DIR", "config"); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | try |
|---|
| 186 | { |
|---|
| 187 | UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
|---|
| 188 | new TrafficModelEventDriver(); |
|---|
| 189 | |
|---|
| 190 | } |
|---|
| 191 | catch (Exception e) |
|---|
| 192 | { |
|---|
| 193 | cadClientLogger.logp(Level.SEVERE, "SimulationManager", "Main", |
|---|
| 194 | "Error initializing application."); |
|---|
| 195 | |
|---|
| 196 | JOptionPane.showMessageDialog(new JWindow(), e.getMessage(), |
|---|
| 197 | "Error - Program Exiting", JOptionPane.ERROR_MESSAGE); |
|---|
| 198 | |
|---|
| 199 | System.exit(-1); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | } |
|---|