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