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

Revision 186, 4.9 KB checked in by jtorres, 9 years ago (diff)

Highways.java, FEPLine.java, LoopDetector?.java, Station.java: reconfigured the getHighwaysMeta() function to a toCondensedFormat(boolean MetaDataOnly?) method. By specifying MetaDataOnly? as true, you get the same output as the previous getHighwaysMeta() function. By specifying MetaDataOnly? as false, you get the new condensed format needed to send to the FEPSim over the socket. This allows us to use the same function to get a meta data dump, as well as get the highways data format needed for FEPSim socket. HighwaysParser?.cpp: removed the old tinyxml code, set up skeleton for new parser. NetworkReader?.h: deleted, and added HighwaysParser?.h. LoadSadDotsTest?.java and StationTest?.java: configured to use new toCondensedFormat method instead of getHighwaysMeta.

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    /**
23     * ATMSDriver Error logger.
24     */
25    private static Logger ATMSDriverLogger = Logger.getLogger("atmsdriver");
26
27    /**
28     * Properties object for the CADClient class.
29     */
30    private Properties ATMSDriverProperties;
31
32    /**
33     * Enumeration containing properties name values. See ATMSDriver class
34     * description for more information.
35     *
36     * @author John Torres
37     * @see ATMSDriver
38     */
39    private static enum PROPERTIES
40    {
41        HIGHWAYS_MAP_FILE_NAME("HighwaysMapFileName"),
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        {
50            name = n;
51        }
52    }
53
54    /**
55     * Highways in traffic network
56     */
57    final private Highways highways;
58
59    /**
60     * Sleep Time (30 seconds). *
61     */
62    private static final int SLEEP_TIME = 30000;
63
64    /**
65     * Exchange Reader
66     */
67    private ExchangeReader exchangeReader;
68
69    @Override
70    public void run()
71    {
72        // Check for packets and update the simulator
73        while (true)
74        {
75            // Flush the input file
76            ExchangeInfo exInfo = exchangeReader.parse(ATMSDriverProperties
77                    .getProperty(PROPERTIES.EXCHANGE_FILE_NAME.name));
78
79            try
80            {
81                highways.writeToFEP();
82            } catch (SimulationException ex)
83            {
84                System.out.println("Skipping writeToFEP...");
85            }
86            // Update if exchangeInfo is recieved
87            if (exInfo != null)
88            {
89                // TODO: handle this condition
90                Logger.getLogger("ATMSDriver").log(Level.INFO, "exInfo is not null");
91            }
92
93            // Wait for FEP Sim to process the data we just sent
94            try
95            {
96                Thread.sleep(SLEEP_TIME);
97            } catch (InterruptedException ie)
98            {
99                ie.printStackTrace();
100            }
101        }
102    }
103
104    public ATMSDriver(String propertiesFile)
105    {
106        // verify properties file
107        if (!verifyProperties(propertiesFile))
108        {
109            System.exit(0);
110        }
111        // create the highways model
112        highways = new Highways(
113                ATMSDriverProperties.getProperty(
114                        PROPERTIES.HIGHWAYS_MAP_FILE_NAME.name),
115                ATMSDriverProperties.getProperty(PROPERTIES.FEP_WRITER_HOST.name),
116                Integer.parseInt(ATMSDriverProperties.getProperty(
117                                PROPERTIES.FEP_WRITER_PORT.name)));
118        System.out.println(highways.toCondensedFormat(false));
119        // create the exchange reader
120        exchangeReader = new ExchangeReader();
121    }
122
123    /**
124     * Verifies that the properties file has all necessary properties.
125     *
126     * @param propertiesFile
127     * @return
128     */
129    private boolean verifyProperties(String propertiesFile)
130    {
131        // Load the properties file.
132        try
133        {
134            ATMSDriverProperties = new Properties();
135            ATMSDriverProperties.load(new FileInputStream(propertiesFile));
136        } catch (Exception e)
137        {
138            ATMSDriverLogger.logp(Level.SEVERE, "ATMSDriver",
139                    "Constructor", "Exception in reading properties file.", e);
140        }
141
142        return true;
143    }
144
145    /**
146     * Runs the ATMS Driver.
147     */
148    public static void main(String[] args)
149    {
150        try
151        {
152            if (System.getProperty("ATMSDRIVER_PROPERTIES") != null)
153            {
154                // Create and run the ATMSDriver thread
155                ATMSDriver atmsDriver = new ATMSDriver(System.getProperty("ATMSDRIVER_PROPERTIES"));
156                Thread ATMSDriverThread = new Thread(atmsDriver);
157                ATMSDriverThread.start();
158
159                // run the console driver, pass it the atmsDriver highways model
160                ConsoleTrafficDriver driver = new ConsoleTrafficDriver(atmsDriver.highways);
161
162            } else
163            {
164                throw new Exception("ATMSDRIVER_PROPERTIES system property not defined.");
165            }
166        } catch (Exception e)
167        {
168            ATMSDriverLogger.logp(Level.SEVERE, "ATMSDriver", "Main",
169                    "Error occured initializing application", e);
170            System.exit(-1);
171        }
172    }
173}
Note: See TracBrowser for help on using the repository browser.