| 1 | package atmsdriver; |
|---|
| 2 | |
|---|
| 3 | import atmsdriver.model.Highways; |
|---|
| 4 | import java.io.FileInputStream; |
|---|
| 5 | import java.util.Properties; |
|---|
| 6 | import java.util.logging.Level; |
|---|
| 7 | import java.util.logging.Logger; |
|---|
| 8 | import tmcsim.common.SimulationException; |
|---|
| 9 | |
|---|
| 10 | /** |
|---|
| 11 | * ATMS Driver reads the current simulation traffic conditions from the |
|---|
| 12 | * EXCHANGE.XML file and constructs the Highway Network status info in the |
|---|
| 13 | * format required by the FEP. It then sends this XML data over a socket to the |
|---|
| 14 | * FEP Simulator. |
|---|
| 15 | * |
|---|
| 16 | * @author John A. Torres |
|---|
| 17 | * @version 09/10/2017 |
|---|
| 18 | */ |
|---|
| 19 | public class ATMSDriver implements Runnable |
|---|
| 20 | { |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * ATMSDriver Error logger. |
|---|
| 24 | */ |
|---|
| 25 | private static Logger ATMSDriverLogger = Logger.getLogger("atmsdriver"); |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | * Properties object for the CADClient class. |
|---|
| 29 | */ |
|---|
| 30 | private Properties ATMSDriverProperties; |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * Enumeration containing properties name values. See ATMSDriver class |
|---|
| 34 | * description for more information. |
|---|
| 35 | * |
|---|
| 36 | * @author John Torres |
|---|
| 37 | * @see ATMSDriver |
|---|
| 38 | */ |
|---|
| 39 | private static enum PROPERTIES |
|---|
| 40 | { |
|---|
| 41 | HIGHWAYS_MAP_FILE_NAME("HighwaysMapFileName"), |
|---|
| 42 | EXCHANGE_FILE_NAME("ExchangeFileName"), |
|---|
| 43 | FEP_WRITER_HOST("FEPWriterHost"), |
|---|
| 44 | FEP_WRITER_PORT("FEPWriterPort"); |
|---|
| 45 | |
|---|
| 46 | public String name; |
|---|
| 47 | |
|---|
| 48 | private PROPERTIES(String n) |
|---|
| 49 | { |
|---|
| 50 | name = n; |
|---|
| 51 | } |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * Highways in traffic network |
|---|
| 56 | */ |
|---|
| 57 | final private Highways highways; |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * Sleep Time (30 seconds). * |
|---|
| 61 | */ |
|---|
| 62 | private static final int SLEEP_TIME = 30000; |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * Exchange Reader |
|---|
| 66 | */ |
|---|
| 67 | private ExchangeReader exchangeReader; |
|---|
| 68 | |
|---|
| 69 | @Override |
|---|
| 70 | public void run() |
|---|
| 71 | { |
|---|
| 72 | // Check for packets and update the simulator |
|---|
| 73 | while (true) |
|---|
| 74 | { |
|---|
| 75 | // Flush the input file |
|---|
| 76 | ExchangeInfo exInfo = exchangeReader.parse(ATMSDriverProperties |
|---|
| 77 | .getProperty(PROPERTIES.EXCHANGE_FILE_NAME.name)); |
|---|
| 78 | |
|---|
| 79 | try |
|---|
| 80 | { |
|---|
| 81 | highways.writeToFEP(); |
|---|
| 82 | } catch (SimulationException ex) |
|---|
| 83 | { |
|---|
| 84 | System.out.println("Skipping writeToFEP..."); |
|---|
| 85 | } |
|---|
| 86 | // Update if exchangeInfo is recieved |
|---|
| 87 | if (exInfo != null) |
|---|
| 88 | { |
|---|
| 89 | // TODO: handle this condition |
|---|
| 90 | Logger.getLogger("ATMSDriver").log(Level.INFO, "exInfo is not null"); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | // Wait for FEP Sim to process the data we just sent |
|---|
| 94 | try |
|---|
| 95 | { |
|---|
| 96 | Thread.sleep(SLEEP_TIME); |
|---|
| 97 | } catch (InterruptedException ie) |
|---|
| 98 | { |
|---|
| 99 | ie.printStackTrace(); |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | public ATMSDriver(String propertiesFile) |
|---|
| 105 | { |
|---|
| 106 | // verify properties file |
|---|
| 107 | if (!verifyProperties(propertiesFile)) |
|---|
| 108 | { |
|---|
| 109 | System.exit(0); |
|---|
| 110 | } |
|---|
| 111 | // create the highways model |
|---|
| 112 | highways = new Highways( |
|---|
| 113 | ATMSDriverProperties.getProperty( |
|---|
| 114 | PROPERTIES.HIGHWAYS_MAP_FILE_NAME.name), |
|---|
| 115 | ATMSDriverProperties.getProperty(PROPERTIES.FEP_WRITER_HOST.name), |
|---|
| 116 | Integer.parseInt(ATMSDriverProperties.getProperty( |
|---|
| 117 | PROPERTIES.FEP_WRITER_PORT.name))); |
|---|
| 118 | // create the exchange reader |
|---|
| 119 | exchangeReader = new ExchangeReader(); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | /** |
|---|
| 123 | * Verifies that the properties file has all necessary properties. |
|---|
| 124 | * |
|---|
| 125 | * @param propertiesFile |
|---|
| 126 | * @return |
|---|
| 127 | */ |
|---|
| 128 | private boolean verifyProperties(String propertiesFile) |
|---|
| 129 | { |
|---|
| 130 | // Load the properties file. |
|---|
| 131 | try |
|---|
| 132 | { |
|---|
| 133 | ATMSDriverProperties = new Properties(); |
|---|
| 134 | ATMSDriverProperties.load(new FileInputStream(propertiesFile)); |
|---|
| 135 | } catch (Exception e) |
|---|
| 136 | { |
|---|
| 137 | ATMSDriverLogger.logp(Level.SEVERE, "ATMSDriver", |
|---|
| 138 | "Constructor", "Exception in reading properties file.", e); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | return true; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | /** |
|---|
| 145 | * Runs the ATMS Driver. |
|---|
| 146 | */ |
|---|
| 147 | public static void main(String[] args) |
|---|
| 148 | { |
|---|
| 149 | try |
|---|
| 150 | { |
|---|
| 151 | if (System.getProperty("ATMSDRIVER_PROPERTIES") != null) |
|---|
| 152 | { |
|---|
| 153 | // Create and run the ATMSDriver thread |
|---|
| 154 | ATMSDriver atmsDriver = new ATMSDriver(System.getProperty("ATMSDRIVER_PROPERTIES")); |
|---|
| 155 | Thread ATMSDriverThread = new Thread(atmsDriver); |
|---|
| 156 | ATMSDriverThread.start(); |
|---|
| 157 | |
|---|
| 158 | // run the console driver, pass it the atmsDriver highways model |
|---|
| 159 | ConsoleTrafficDriver driver = new ConsoleTrafficDriver(atmsDriver.highways); |
|---|
| 160 | |
|---|
| 161 | } else |
|---|
| 162 | { |
|---|
| 163 | throw new Exception("ATMSDRIVER_PROPERTIES system property not defined."); |
|---|
| 164 | } |
|---|
| 165 | } catch (Exception e) |
|---|
| 166 | { |
|---|
| 167 | ATMSDriverLogger.logp(Level.SEVERE, "ATMSDriver", "Main", |
|---|
| 168 | "Error occured initializing application", e); |
|---|
| 169 | System.exit(-1); |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | } |
|---|