| 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 | * |
|---|
| 12 | * @author John A. Torres |
|---|
| 13 | * @version 09/10/2017 |
|---|
| 14 | */ |
|---|
| 15 | public class ATMSDriver implements Runnable { |
|---|
| 16 | |
|---|
| 17 | /** ATMSDriver Error logger. */ |
|---|
| 18 | private static Logger ATMSDriverLogger = Logger.getLogger("atmsdriver"); |
|---|
| 19 | |
|---|
| 20 | private static final String CONFIG_FILE_NAME = "atms_driver_config.properties"; |
|---|
| 21 | |
|---|
| 22 | /** Properties object for the CADClient class. */ |
|---|
| 23 | private Properties ATMSDriverProperties; |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * Enumeration containing properties name values. See ATMSDriver class |
|---|
| 27 | * description for more information. |
|---|
| 28 | * |
|---|
| 29 | * @author John Torres |
|---|
| 30 | * @see ATMSDriver |
|---|
| 31 | */ |
|---|
| 32 | private static enum PROPERTIES { |
|---|
| 33 | |
|---|
| 34 | LDS_FILE_NAME("LDSFileName"), |
|---|
| 35 | LOOP_FILE_NAME("LoopFileName"), |
|---|
| 36 | NETWORK_FILE_NAME("NetworkFileName"), |
|---|
| 37 | EXCHANGE_FILE_NAME("ExchangeFileName"); |
|---|
| 38 | |
|---|
| 39 | public String name; |
|---|
| 40 | |
|---|
| 41 | private PROPERTIES(String n) { |
|---|
| 42 | name = n; |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | /** Network model. */ |
|---|
| 47 | final private Network network; |
|---|
| 48 | |
|---|
| 49 | /** Sleep Time (10 seconds). **/ |
|---|
| 50 | private static final int SLEEP_TIME = 10000; |
|---|
| 51 | |
|---|
| 52 | /** Exchange Reader */ |
|---|
| 53 | private ExchangeReader exchangeReader; |
|---|
| 54 | |
|---|
| 55 | @Override |
|---|
| 56 | public void run() { |
|---|
| 57 | // Check for packets and update the simulator |
|---|
| 58 | for (;;) { |
|---|
| 59 | // Flush the input file |
|---|
| 60 | ExchangeInfo exInfo = exchangeReader.parse(ATMSDriverProperties |
|---|
| 61 | .getProperty(PROPERTIES.EXCHANGE_FILE_NAME.name)); |
|---|
| 62 | network.toXML(); |
|---|
| 63 | // Update if packet is recieved |
|---|
| 64 | if (exInfo != null) { |
|---|
| 65 | System.out.println("Grabbed"); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | // Sleep |
|---|
| 69 | try { |
|---|
| 70 | Thread.sleep(SLEEP_TIME); |
|---|
| 71 | } catch (InterruptedException ie) { |
|---|
| 72 | |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | public ATMSDriver(String propertiesFile) { |
|---|
| 78 | |
|---|
| 79 | if (!verifyProperties(propertiesFile)) { |
|---|
| 80 | System.exit(0); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | network = new Network( |
|---|
| 84 | new File(ATMSDriverProperties.getProperty( |
|---|
| 85 | PROPERTIES.LDS_FILE_NAME.name)), |
|---|
| 86 | new File(ATMSDriverProperties.getProperty( |
|---|
| 87 | PROPERTIES.LOOP_FILE_NAME.name)), |
|---|
| 88 | new File(ATMSDriverProperties.getProperty( |
|---|
| 89 | PROPERTIES.NETWORK_FILE_NAME.name))); |
|---|
| 90 | network.toXML(); |
|---|
| 91 | exchangeReader = new ExchangeReader(); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | private boolean verifyProperties(String propertiesFile) { |
|---|
| 95 | // Load the properties file. |
|---|
| 96 | try { |
|---|
| 97 | ATMSDriverProperties = new Properties(); |
|---|
| 98 | ATMSDriverProperties.load(new FileInputStream(propertiesFile)); |
|---|
| 99 | } catch (Exception e) { |
|---|
| 100 | ATMSDriverLogger.logp(Level.SEVERE, "ATMSDriver", |
|---|
| 101 | "Constructor", "Exception in reading properties file.", e); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | return true; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | /** |
|---|
| 108 | * Runs the Paramics simulator. |
|---|
| 109 | */ |
|---|
| 110 | public static void main(String[] args) { |
|---|
| 111 | try { |
|---|
| 112 | if (System.getProperty("ATMSDRIVER_PROPERTIES") != null) { |
|---|
| 113 | new Thread(new ATMSDriver(System.getProperty( |
|---|
| 114 | "ATMSDRIVER_PROPERTIES"))).start(); |
|---|
| 115 | } else { |
|---|
| 116 | throw new Exception("ATMSDRIVER_PROPERTIES system property not defined."); |
|---|
| 117 | } |
|---|
| 118 | } catch (Exception e) { |
|---|
| 119 | ATMSDriverLogger.logp(Level.SEVERE, "ATMSDriver", "Main", |
|---|
| 120 | "Error occured initializing application", e); |
|---|
| 121 | System.exit(-1); |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | } |
|---|