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

Revision 87, 4.5 KB checked in by jtorres, 9 years ago (diff)

merged branches/trunk into regular tmcsim/trunk. Changed Network.java to Highways.java. Highways.java: added writeToFEP(), updateSequences(), loadHighways(), and writeHighwaysMeta(). writeToFEP() is a socket client that communicates with FEPSimulator socket server. updateSequences() updates global and schedule sequences per the existing UCI plugin code and may not be necessary. loadHighways() loads the ArrayList? of Highways used in highways class, in sorted order. Stations are now implementing comparable to sort by postmile. writeHighwaysMeta() is an attempt at writing the highways meta data in condensed form, because loading time in NetworkLoader? is super long. We will eventually get rid of NetworkLoader? and do it within the Highways class for good OOP practice. There are new properties in atms_driver_properties.properties in config to support all these changes. Highway.java: new class, represents a highway which is a sorted ArrayList? of stations. This is a good checkpoint, for persistent green dots and all is working within the triangle. There is much commented out code in Highways.java which does not quite work. This commented code is an attempt at writing the condensed form highway meta data to improve loading times as described above.

Line 
1package atmsdriver;
2
3import atmsdriver.model.Highways;
4import java.io.File;
5import java.io.FileInputStream;
6import java.io.FileNotFoundException;
7import java.io.PrintWriter;
8import java.util.Properties;
9import java.util.logging.Level;
10import 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 */
21public 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            System.out.println(highways.toXML());
81            highways.writeToFEP();
82            // Update if exchangeInfo is recieved
83            if (exInfo != null) {
84                // TODO: handle this condition
85                Logger.getLogger("ATMSDriver").log(Level.INFO, "exInfo is not null");
86            }
87
88            // Wait for FEP Sim to process the data we just sent
89            try {
90                Thread.sleep(SLEEP_TIME);
91            } catch (InterruptedException ie) {
92                ie.printStackTrace();
93            }
94        }
95    }
96
97    public ATMSDriver(String propertiesFile) {
98
99        if (!verifyProperties(propertiesFile)) {
100            System.exit(0);
101        }
102
103        highways = new Highways(
104                ATMSDriverProperties.getProperty(
105                                PROPERTIES.LDS_FILE_NAME.name),
106                ATMSDriverProperties.getProperty(
107                                PROPERTIES.LOOPS_FILE_NAME.name),
108                ATMSDriverProperties.getProperty(
109                                PROPERTIES.HIGHWAY_META_FILE.name),
110                ATMSDriverProperties.getProperty(PROPERTIES.FEP_WRITER_HOST.name),
111                Integer.parseInt(ATMSDriverProperties.getProperty(
112                                PROPERTIES.FEP_WRITER_PORT.name)));
113       
114        exchangeReader = new ExchangeReader();
115    }
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                new Thread(new ATMSDriver(System.getProperty(
137                        "ATMSDRIVER_PROPERTIES"))).start();
138            } else {
139                throw new Exception("ATMSDRIVER_PROPERTIES system property not defined.");
140            }
141        } catch (Exception e) {
142            ATMSDriverLogger.logp(Level.SEVERE, "ATMSDriver", "Main",
143                    "Error occured initializing application", e);
144            System.exit(-1);
145        }
146    }
147}
Note: See TracBrowser for help on using the repository browser.