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

Revision 180, 5.0 KB checked in by jdalbey, 9 years ago (diff)

ATMSDriver.java Refactored for cleaner design. DotColor? enum moved to LoopDetector? class. ConsoleDriver? renamed ConsoleTrafficDriver? (and associated project configurations updated), build.xml package-jars target updated, ConsoleTrafficDriver? logic improved to allow more user control, runtraffic.bash script created for automating traffic display tests.

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