Changes in trunk/src/tmcsim/paramicscommunicator/ParamicsFileWriter.java [25:2] in tmcsimulator
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/tmcsim/paramicscommunicator/ParamicsFileWriter.java
r25 r2 1 1 package tmcsim.paramicscommunicator; 2 2 3 3 4 import java.io.File; … … 10 11 import java.util.logging.Level; 11 12 import java.util.logging.Logger; 13 12 14 import org.apache.xml.serialize.OutputFormat; 13 15 import org.apache.xml.serialize.XMLSerializer; 14 16 import org.w3c.dom.Element; 17 15 18 import tmcsim.paramicscommunicator.FileIOUpdate.IO_TYPE; 16 19 17 20 /** 18 * The ParamicsFileWriter handles writing messages to a target file which is19 * read by Paramics.Messages are received through the writeMessage() method.21 * The ParamicsFileWriter handles writing messages to a target file which 22 * is read by Paramics. Messages are received through the writeMessage() method. 20 23 * This object handles queueing messages and writing as the file becomes 21 * available. New data is written to the target file when it has been modified22 * (cleared) by Paramics. If this does not happen, messages are queued and a23 * timer is used to periodically determine if the file has become available for24 * writing.25 * 24 * available. New data is written to the target file when it has been 25 * modified (cleared) by Paramics. If this does not happen, messages 26 * are queued and a timer is used to periodically determine if the 27 * file has become available for writing. 28 * 26 29 * @author Matthew Cechini 27 30 * @version 28 31 */ 29 public class ParamicsFileWriter extends Observable 30 { 31 32 /** 33 * Duration (in ms) that the TimerTask will be scheduled to retry writing to 34 * the target file. Default = 2000ms 32 public class ParamicsFileWriter extends Observable { 33 34 /** 35 * Duration (in ms) that the TimerTask will be scheduled to 36 * retry writing to the target file. Default = 2000ms 35 37 */ 36 38 private static long TIMER_DURATION = 2000; 37 /** 38 * Error Logger. 39 */ 39 40 /** Error Logger. */ 40 41 private Logger paramLogger = Logger.getLogger("tmcsim.paramicscommunicator"); 41 /** 42 * Linked List of messages that have been received 43 */ 42 43 /** Linked List of messages that have been received */ 44 44 private LinkedList<Element> queuedMessages = null; 45 /** 46 * 47 */ 45 46 /** */ 48 47 private String writerID = null; 49 /** 50 * File name of the file where data is written 51 */ 48 49 /** File name of the file where data is written */ 52 50 private String outputFile = null; 53 /** 54 * FileWriter used to write data to the output file. 55 */ 51 52 /** FileWriter used to write data to the output file. */ 56 53 private FileWriter fileWriter = null; 57 /** 58 * Value (seconds since 1/1/1970) of output file's last modifcation time 59 */ 60 private long lastModified = 0; 61 /** 62 * Timer used to schedule file writing tasks. 63 */ 54 55 /** Value (seconds since 1/1/1970) of output file's last modifcation time */ 56 private long lastModified = 0; 57 58 /** Timer used to schedule file writing tasks. */ 64 59 private Timer writerTimer = null; 65 /** 66 * Synchronizing lock to protect File IO and message queuing. 67 */ 68 private Object lock = null; 69 60 70 61 /** 71 62 * A TimerTask to retry writing messages that have been queued within this 72 * ParamicsWriter. If a message has been queued, see if the target file has 73 * been modified since last write. If so, write the first queued message to 74 * the file and remove the message from the queue. If writing is 75 * unsuccessful, do not remove the message from the queue. If there are no 76 * more messages in the queue, cancel this timer. 77 */ 78 private class WriterTimerTask extends TimerTask 79 { 80 81 public void run() 82 { 83 84 synchronized (lock) 85 { 86 paramLogger.log(Level.INFO, "ParamicsFileWriter, WriterTimerTask " 87 + "run() started, current queue size = " + queuedMessages.size()); 88 63 * ParamicsWriter. If a message has been queued, see if the target file 64 * has been modified since last write. If so, write the first queued message 65 * to the file and remove the message from the queue. If writing is unsuccessful, 66 * do not remove the message from the queue. If there are no more messages in 67 * the queue, cancel this timer. 68 */ 69 private class WriterTimerTask extends TimerTask { 70 public void run() { 71 72 synchronized(lock) { 73 89 74 //if we've queued something, continue. 90 if (queuedMessages.size() > 0) 91 { 92 75 if(queuedMessages.size() > 0) { 76 93 77 //if file has been modified, write to it 94 if (lastModified < new File(outputFile).lastModified()) 95 { 96 try 97 { 78 if(lastModified < new File(outputFile).lastModified()) 79 { 80 try { 98 81 writeToFile(queuedMessages.getFirst()); 99 82 queuedMessages.remove(0); 100 } catch (IOException ioe)101 {102 paramLogger.logp(Level.SEVERE, "ParamicsFileWriter.WriterTimerTask",103 "run()", "Exception in writing to the target file: "104 + outputFile + ". Queue size = " + queuedMessages.size(), ioe);105 83 } 106 84 catch (IOException ioe) { 85 paramLogger.logp(Level.SEVERE, "ParamicsFileWriter.WriterTimerTask", 86 "run()", "Exception in writing to the target file: " + 87 outputFile + ". Queue size = " + queuedMessages.size(), ioe); 88 } 89 107 90 //all queued messages gone, cancel timer 108 if (queuedMessages.size() == 0) 109 { 91 if(queuedMessages.size() == 0) 110 92 this.cancel(); 111 } 112 } 93 } 113 94 } 114 } 95 } 115 96 } 116 97 } 117 118 /** 119 * Constructor. Initialize data objects. If the target file exists, delete 120 * it, and then create a new file. 98 99 /** Synchronizing lock to protect File IO and message queuing. */ 100 private Object lock = null; 101 102 /** 103 * Constructor. Initialize data objects. If the target file exists, delete 104 * it, and then create a new file. 121 105 * 122 106 * @param workingDir Directory path where the output file is to be written 123 107 * @param mess The ParamicsCommMessage containing the outputFile filename. 124 108 */ 125 public ParamicsFileWriter(String id, String workingDir, String targetFile) 126 { 127 128 try 129 { 109 public ParamicsFileWriter(String id, String workingDir, String targetFile) { 110 111 try { 130 112 writerID = id; 131 132 queuedMessages = new LinkedList<Element>(); 133 lock = new Object(); 134 135 outputFile = workingDir + targetFile; 136 137 File tempFile = new File(outputFile); 138 if (tempFile.exists()) 139 { 113 114 queuedMessages = new LinkedList<Element>(); 115 lock = new Object(); 116 117 outputFile = workingDir + targetFile; 118 119 File tempFile = new File(outputFile); 120 if(tempFile.exists()) { 140 121 tempFile.delete(); 141 122 } 142 123 143 tempFile.createNewFile(); 144 145 writerTimer = new Timer(); 146 147 } catch (IOException ioe) 148 { 149 paramLogger.logp(Level.SEVERE, "ParamicsFileWriter", "Constructor", 124 tempFile.createNewFile(); 125 126 writerTimer = new Timer(); 127 128 } catch (IOException ioe) { 129 paramLogger.logp(Level.SEVERE, "ParamicsFileWriter", "Constructor", 150 130 "Unable to create Paramics File Writer.", ioe); 151 131 } 152 132 153 133 } 154 134 135 155 136 /** 156 137 * Method is called when a message has been received from the CAD Simulator. 157 * If the message queue is not empty, add the new message to the queue. If158 * the output file has not been modified (read) since last write, add the159 * message to the queue and set a timer to repeatedly check for modification160 * to the output file. Else, write the new message to the file. If there is161 * an error in writing the data, queue the message start a timer to retry162 * t he writing.138 * If the message queue is not empty, add the new message to the queue. 139 * If the output file has not been modified (read) since last write, 140 * add the message to the queue and set a timer to repeatedly check for 141 * modification to the output file. Else, write the new message to the file. 142 * If there is an error in writing the data, queue the message start a timer 143 * to retry the writing. 163 144 * 164 * @param newMessage The received message which is to be written to the 165 * output file. 166 */ 167 public void writeMessage(Element messageElem) 168 { 169 170 synchronized (lock) 171 { 172 paramLogger.log(Level.INFO, "ParamicsFileWriter, writeMessage " 173 + "current queue size = " + queuedMessages.size()); 174 175 //messages already queued... get in line. 176 if (queuedMessages.size() > 0) 177 { 145 * @param newMessage The received message which is to be written to 146 * the output file. 147 */ 148 public void writeMessage(Element messageElem) { 149 150 synchronized(lock) { 151 152 //messages already queued... get in line. 153 if(queuedMessages.size() > 0) { 178 154 queuedMessages.add(messageElem); 179 155 180 paramLogger.log(Level.INFO, "Queueing message, new queue " 181 + "size = " + queuedMessages.size()); 182 } //No modification since last write. (first queue) 183 else if (lastModified >= new File(outputFile).lastModified()) 184 { 185 paramLogger.log(Level.INFO, "ParamicsFileWriter, writeMessage " 186 + "adding msg to queue."); 187 queuedMessages.add(messageElem); 188 189 writerTimer.scheduleAtFixedRate(new WriterTimerTask(), 156 paramLogger.log(Level.INFO, "Queueing message, new queue " + 157 "size = " + queuedMessages.size()); 158 } 159 //No modification since last write. (first queue) 160 else if (lastModified >= new File(outputFile).lastModified()) { 161 queuedMessages.add(messageElem); 162 163 writerTimer.scheduleAtFixedRate(new WriterTimerTask(), 190 164 0L, TIMER_DURATION); 191 165 192 166 paramLogger.log(Level.INFO, "First message queued"); 193 } //free and clear, write. 194 else 195 { 196 paramLogger.log(Level.INFO, "ParamicsFileWriter, writeMessage " 197 + "try to writeToFile" + outputFile); 198 try 199 { 167 } 168 //free and clear, write. 169 else { 170 try { 200 171 writeToFile(messageElem); 201 } catch (IOException ioe)202 {203 paramLogger.logp(Level.SEVERE, "ParamicsFileWriter", 204 "writeMessage()", "Exception in writing to the " 205 +"target file: " + outputFile, ioe);206 172 } 173 catch(IOException ioe) { 174 paramLogger.logp(Level.SEVERE, "ParamicsFileWriter", 175 "writeMessage()", "Exception in writing to the " + 176 "target file: " + outputFile, ioe); 177 207 178 queuedMessages.add(messageElem); 208 209 writerTimer.scheduleAtFixedRate(new WriterTimerTask(), 179 180 writerTimer.scheduleAtFixedRate(new WriterTimerTask(), 210 181 0L, TIMER_DURATION); 211 182 } 212 paramLogger.log(Level.INFO, "ParamicsFileWriter, writeMessage "213 + "writeToFile succeeded.");214 183 } 215 184 } 216 185 } 217 186 218 187 /** 219 188 * Method writes data to the output file. 220 * 189 * 221 190 * @param output Data to be written to the file. 222 191 */ 223 private void writeToFile(Element output) throws IOException 224 { 225 192 private void writeToFile(Element output) throws IOException { 193 226 194 fileWriter = new FileWriter(outputFile); 227 228 OutputFormat of = new OutputFormat("XML", "ISO-8859-1",true);195 196 OutputFormat of = new OutputFormat("XML","ISO-8859-1",true); 229 197 of.setIndent(1); 230 198 of.setIndenting(true); 231 199 232 200 XMLSerializer serializer = new XMLSerializer(fileWriter, of); 233 201 serializer.asDOMSerializer(); 234 202 serializer.serialize(output); 235 203 236 /** 237 * Added by Nathaniel Lehrer 238 */ 239 try 240 { 204 /** Added by Nathaniel Lehrer */ 205 try { 241 206 java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream(); 242 207 new XMLSerializer(outputStream, of).serialize(output); 243 208 tmcsim.paramicslog.ParamicsLog.getInstance().writeToLog(outputStream.toString()); 244 } catch (Exception e) 245 { 209 } catch(Exception e) { 246 210 System.out.println(e); 247 211 } 248 /** 249 * End Add by Nathaniel Lehrer 250 */ 212 /** End Add by Nathaniel Lehrer */ 213 251 214 fileWriter.flush(); 252 215 fileWriter.close(); 253 254 lastModified = new File(outputFile).lastModified(); 255 216 217 lastModified = new File(outputFile).lastModified(); 218 256 219 setChanged(); 257 220 notifyObservers(new FileIOUpdate(IO_TYPE.WRITE, writerID, new File(outputFile).length())); 258 259 } 221 222 } 260 223 }
Note: See TracChangeset
for help on using the changeset viewer.
