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