source: tmcsimulator/trunk/src/atmsdriver/ATMSDriver.java @ 103

Revision 103, 4.8 KB checked in by jtorres, 9 years ago (diff)

trunk/src/atmsdriver/ConsoleDriver.java: Created console driver for ATMSDriver, completed and very nice. Still need to apply correct vol/occ values to actually change the colors. Still need to write a JUnit test for it. trunk/src/atmsdriver/ATMSDriver.java: Refactored to run the console driver. ATMSDriver runs in thread, console driver runs, and they share the highways instance. Renamed NetworkLoader?.java to FEPLineLoader.java. trunk/src/atmsdriver/model/Highways.java: refactored loadHighways() method to conform to new undirected highway abstraction. trunk/src/atmsdriver/model/Station.java: added updateByDirection(DIRECTION dir) method and supporting utility methods. trunk/test/atmsdriver/model/LoadHighwaysTest.java: Conformed LoadHighways? test to new undirected highway abstraction. trunk/test/atmsdriver/model/StationTest.java: Conformed StationTest?.java to new changes - very minor stuff. Went through all model classes and changed any final privates with a getter method to final public, for good OOP practice and simplicity. Went through ALL FILES and commented everything very well. minor application custom configuration changes. Removed all .class or .o.d files from svn repository

Line 
1package atmsdriver;
2
3import atmsdriver.model.Highways;
4import java.io.FileInputStream;
5import java.util.Properties;
6import java.util.logging.Level;
7import 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 */
18public 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}
Note: See TracBrowser for help on using the repository browser.