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