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