| 1 | package tmcsim.paramicscommunicator; |
|---|
| 2 | |
|---|
| 3 | import java.io.File; |
|---|
| 4 | import java.io.FileReader; |
|---|
| 5 | import java.io.FileWriter; |
|---|
| 6 | import java.io.IOException; |
|---|
| 7 | import java.util.Observable; |
|---|
| 8 | import java.util.Timer; |
|---|
| 9 | import java.util.TimerTask; |
|---|
| 10 | import java.util.logging.Level; |
|---|
| 11 | import java.util.logging.Logger; |
|---|
| 12 | import javax.xml.parsers.DocumentBuilderFactory; |
|---|
| 13 | import org.w3c.dom.Document; |
|---|
| 14 | import org.w3c.dom.Element; |
|---|
| 15 | import tmcsim.common.CADProtocol.PARAMICS_ACTIONS; |
|---|
| 16 | import tmcsim.common.CADProtocol.PARAMICS_COMM_TAGS; |
|---|
| 17 | import tmcsim.paramicscommunicator.FileIOUpdate.IO_TYPE; |
|---|
| 18 | |
|---|
| 19 | /** |
|---|
| 20 | * The ParamicsFileReader handles reading fom a target file which is written to |
|---|
| 21 | * by Paramics. Once initialized, a timer task is started which periodically |
|---|
| 22 | * checks the target file for updates. If the file has been modified, it is read |
|---|
| 23 | * and cleared. The read data is transmitted to the CAD Simulator. |
|---|
| 24 | * |
|---|
| 25 | * @author Matthew Cechini |
|---|
| 26 | * @version 0.1 Notes by jdalbey: Paramics modeler is licensed third-party |
|---|
| 27 | * software and apparently we don't have an SDK for it. So it seems the |
|---|
| 28 | * workaround was to communicate with it via data files that the modeler |
|---|
| 29 | * produces. Wacky, but it works for now. Look at the run() method below: if |
|---|
| 30 | * (lastModified < inputFile.lastModified()) Means it's watching the file to |
|---|
| 31 | * see if it has been updated by the modeler. |
|---|
| 32 | */ |
|---|
| 33 | public class ParamicsFileReader extends Observable |
|---|
| 34 | { |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * Error Logger. |
|---|
| 38 | */ |
|---|
| 39 | private Logger paramLogger = Logger.getLogger("tmcsim.paramicscommunicator"); |
|---|
| 40 | /** |
|---|
| 41 | * FileReader ID used for creation of the ParamicsCommMessage |
|---|
| 42 | */ |
|---|
| 43 | private String readerID = null; |
|---|
| 44 | /** |
|---|
| 45 | * File reference to the file where data is read. |
|---|
| 46 | */ |
|---|
| 47 | private File inputFile = null; |
|---|
| 48 | /** |
|---|
| 49 | * FileReader used to read data from the input file. |
|---|
| 50 | */ |
|---|
| 51 | private FileReader fileReader = null; |
|---|
| 52 | /** |
|---|
| 53 | * FileWriter used to clear the input file. |
|---|
| 54 | */ |
|---|
| 55 | private FileWriter fileWriter = null; |
|---|
| 56 | /** |
|---|
| 57 | * Value (seconds since 1/1/1970) of input file's last modification time. |
|---|
| 58 | */ |
|---|
| 59 | private long lastModified = 0; |
|---|
| 60 | /** |
|---|
| 61 | * Timer used to schedule file reading tasks. |
|---|
| 62 | */ |
|---|
| 63 | private Timer readerTimer = null; |
|---|
| 64 | /** |
|---|
| 65 | * Duration (in seconds) that the TimerTask will be scheduled to read from |
|---|
| 66 | * the target file. |
|---|
| 67 | */ |
|---|
| 68 | private long readerInterval; |
|---|
| 69 | |
|---|
| 70 | /** |
|---|
| 71 | * A TimerTask to read from the target file. If the file has been modified |
|---|
| 72 | * since last read, read the file and transmit the data to the CAD |
|---|
| 73 | * Simulator. |
|---|
| 74 | * |
|---|
| 75 | * @author Matthew Cechini |
|---|
| 76 | */ |
|---|
| 77 | private class ReaderTimerTask extends TimerTask |
|---|
| 78 | { |
|---|
| 79 | |
|---|
| 80 | public void run() |
|---|
| 81 | { |
|---|
| 82 | paramLogger.logp(Level.INFO, |
|---|
| 83 | "ParamicsFileReader.ReaderTimerTask", "run()", |
|---|
| 84 | "Waiting for " + inputFile + " to be modified."); |
|---|
| 85 | if (lastModified < inputFile.lastModified()) |
|---|
| 86 | { |
|---|
| 87 | paramLogger.logp(Level.INFO, |
|---|
| 88 | "ParamicsFileReader.ReaderTimerTask", "run()", |
|---|
| 89 | "Cool, " + inputFile + " has been modified, let's read it."); |
|---|
| 90 | |
|---|
| 91 | try |
|---|
| 92 | { |
|---|
| 93 | Document readerDoc = DocumentBuilderFactory.newInstance() |
|---|
| 94 | .newDocumentBuilder().newDocument(); |
|---|
| 95 | |
|---|
| 96 | Element readerElem = readerDoc.createElement(PARAMICS_COMM_TAGS.READER.tag); |
|---|
| 97 | readerElem.setAttribute(PARAMICS_COMM_TAGS.ID.tag, readerID); |
|---|
| 98 | readerElem.setAttribute(PARAMICS_COMM_TAGS.ACTION.tag, |
|---|
| 99 | PARAMICS_ACTIONS.READ_FILE.action); |
|---|
| 100 | |
|---|
| 101 | Element messageElem = readerDoc.createElement(PARAMICS_COMM_TAGS.MESSAGE.tag); |
|---|
| 102 | String fileContents = readFromFile(); |
|---|
| 103 | paramLogger.logp(Level.INFO, |
|---|
| 104 | "ParamicsFileReader.ReaderTimerTask", "run()", |
|---|
| 105 | "Nice, we read " + fileContents); |
|---|
| 106 | messageElem.appendChild(readerDoc.createTextNode(fileContents)); |
|---|
| 107 | readerElem.appendChild(messageElem); |
|---|
| 108 | |
|---|
| 109 | readerDoc.appendChild(readerElem); |
|---|
| 110 | |
|---|
| 111 | setChanged(); |
|---|
| 112 | notifyObservers(readerDoc); |
|---|
| 113 | } catch (Exception e) |
|---|
| 114 | { |
|---|
| 115 | paramLogger.logp(Level.SEVERE, |
|---|
| 116 | "ParamicsFileReader.ReaderTimerTask", "run()", |
|---|
| 117 | "Exception in reading from file: " |
|---|
| 118 | + inputFile.getName(), e); |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | /** |
|---|
| 125 | * Constructor. Set the reader id and interval from the parsed |
|---|
| 126 | * ParamicsCommMessage. The interval is found within the first three |
|---|
| 127 | * characters of the object's "message" data member. Create a file object |
|---|
| 128 | * for the target file, and create a new file if it does not already exist. |
|---|
| 129 | * Instantiate the timer and ReaderTimerTask to begin periodic reading of |
|---|
| 130 | * the file. |
|---|
| 131 | * |
|---|
| 132 | * @param workingDir Target working directory. // Oops, the following seem |
|---|
| 133 | * obsolete |
|---|
| 134 | * @param mess ParamicsCommMessage object containing registration data. |
|---|
| 135 | * @param theComm Reference to the ParamicsCommunicator. |
|---|
| 136 | */ |
|---|
| 137 | public ParamicsFileReader(String workingDir, String id, Integer interval, String targetFile) |
|---|
| 138 | { |
|---|
| 139 | |
|---|
| 140 | try |
|---|
| 141 | { |
|---|
| 142 | readerID = id; |
|---|
| 143 | readerInterval = interval; |
|---|
| 144 | |
|---|
| 145 | inputFile = new File(workingDir + targetFile); |
|---|
| 146 | |
|---|
| 147 | if (!inputFile.exists()) |
|---|
| 148 | { |
|---|
| 149 | inputFile.createNewFile(); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | readerTimer = new Timer(); |
|---|
| 153 | readerTimer.scheduleAtFixedRate(new ReaderTimerTask(), |
|---|
| 154 | 0L, readerInterval * 1000); |
|---|
| 155 | |
|---|
| 156 | } catch (IOException ioe) |
|---|
| 157 | { |
|---|
| 158 | paramLogger.logp(Level.SEVERE, "ParamicsFileReader", |
|---|
| 159 | "Constructor()", "Exception in initializing file reading.", ioe); |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | /** |
|---|
| 164 | * Method opens the target file and reads all contents. The file is then |
|---|
| 165 | * cleared. |
|---|
| 166 | * |
|---|
| 167 | * @returns |
|---|
| 168 | * @throws IOException if there is an error in reading or writing to the |
|---|
| 169 | * file. |
|---|
| 170 | */ |
|---|
| 171 | private String readFromFile() throws IOException |
|---|
| 172 | { |
|---|
| 173 | |
|---|
| 174 | char[] input = new char[(int) inputFile.length()]; |
|---|
| 175 | |
|---|
| 176 | fileReader = new FileReader(inputFile); |
|---|
| 177 | fileReader.read(input); |
|---|
| 178 | fileReader.close(); |
|---|
| 179 | |
|---|
| 180 | //make sure the new file has a modified time that is different |
|---|
| 181 | try |
|---|
| 182 | { |
|---|
| 183 | Thread.sleep(1000); |
|---|
| 184 | } catch (Exception e) |
|---|
| 185 | { |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | //clear file after reading contents |
|---|
| 189 | fileWriter = new FileWriter(inputFile); |
|---|
| 190 | fileWriter.write(""); |
|---|
| 191 | fileWriter.close(); |
|---|
| 192 | |
|---|
| 193 | lastModified = inputFile.lastModified(); |
|---|
| 194 | |
|---|
| 195 | setChanged(); |
|---|
| 196 | notifyObservers(new FileIOUpdate(IO_TYPE.READ, readerID, (long) input.length)); |
|---|
| 197 | |
|---|
| 198 | return new String(input); |
|---|
| 199 | } |
|---|
| 200 | } |
|---|