Changeset 25 in tmcsimulator


Ignore:
Timestamp:
04/26/2016 07:47:31 AM (10 years ago)
Author:
jdalbey
Message:

ParamicsCommunicator?: added some diagnostic logging messages.

Location:
trunk/src/tmcsim/paramicscommunicator
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tmcsim/paramicscommunicator/ParamicsCommunicator.java

    r6 r25  
    359359 
    360360    /** 
    361      * Perform the action represented in the received XML document message. 
    362      * First determine if the action is from a READER, WRITER, and RESET. If the 
    363      * paramics action is REGISTER, add a new ParamicsFileReader/Writer to the 
    364      * local list of readers/writers and update the GUI with a FileRegUpdate 
    365      * object. If the paramics action is UNREGISTER, remove the 
    366      * ParamicsFileReader/Writer from the local list of readers/writers and 
    367      * update the GUI with a FileRegUpdate object. If RESET is received, clear 
    368      * all readers and writers. 
     361     * Perform the action represented in XML document message received from the 
     362     * CAD Simulator. First determine if the action is from a READER, WRITER, 
     363     * and RESET. If the paramics action is REGISTER, add a new 
     364     * ParamicsFileReader/Writer to the local list of readers/writers and update 
     365     * the GUI with a FileRegUpdate object. If the paramics action is 
     366     * UNREGISTER, remove the ParamicsFileReader/Writer from the local list of 
     367     * readers/writers and update the GUI with a FileRegUpdate object. If RESET 
     368     * is received, clear all readers and writers. 
    369369     * 
    370370     * @param mess Received XML document message. 
     
    482482            } catch (SocketTimeoutException ste) 
    483483            { 
    484                 System.out.println("...waiting..."); 
     484                System.out.println("...waiting for socket connection from the CAD Simulator..."); 
    485485            } catch (IOException ioe) 
    486486            { 
  • trunk/src/tmcsim/paramicscommunicator/ParamicsFileReader.java

    r6 r25  
    8080        public void run() 
    8181        { 
    82  
     82            paramLogger.logp(Level.INFO, 
     83                    "ParamicsFileReader.ReaderTimerTask", "run()", 
     84                    "Waiting for " + inputFile + " to be modified."); 
    8385            if (lastModified < inputFile.lastModified()) 
    8486            { 
     87                paramLogger.logp(Level.INFO, 
     88                        "ParamicsFileReader.ReaderTimerTask", "run()", 
     89                        "Cool, " + inputFile + " has been modified, let's read it."); 
    8590 
    8691                try 
     
    96101                    Element messageElem = readerDoc.createElement(PARAMICS_COMM_TAGS.MESSAGE.tag); 
    97102                    String fileContents = readFromFile(); 
     103                    paramLogger.logp(Level.INFO, 
     104                            "ParamicsFileReader.ReaderTimerTask", "run()", 
     105                            "Nice, we read " + fileContents); 
    98106                    messageElem.appendChild(readerDoc.createTextNode(fileContents)); 
    99107                    readerElem.appendChild(messageElem); 
  • trunk/src/tmcsim/paramicscommunicator/ParamicsFileWriter.java

    r2 r25  
    11package tmcsim.paramicscommunicator; 
    2  
    32 
    43import java.io.File; 
     
    1110import java.util.logging.Level; 
    1211import java.util.logging.Logger; 
    13  
    1412import org.apache.xml.serialize.OutputFormat; 
    1513import org.apache.xml.serialize.XMLSerializer; 
    1614import org.w3c.dom.Element; 
    17  
    1815import tmcsim.paramicscommunicator.FileIOUpdate.IO_TYPE; 
    1916 
    2017/** 
    21  * The ParamicsFileWriter handles writing messages to a target file which 
    22  * is read by Paramics. Messages are received through the writeMessage() method. 
     18 * The ParamicsFileWriter handles writing messages to a target file which is 
     19 * read by Paramics. Messages are received through the writeMessage() method. 
    2320 * This object handles queueing messages and writing as the file becomes 
    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  *  
     21 * available. New data is written to the target file when it has been modified 
     22 * (cleared) by Paramics. If this does not happen, messages are queued and a 
     23 * timer is used to periodically determine if the file has become available for 
     24 * writing. 
     25 * 
    2926 * @author Matthew Cechini 
    3027 * @version 
    3128 */ 
    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 
     29public 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 
    3735     */ 
    3836    private static long TIMER_DURATION = 2000; 
    39      
    40     /** Error Logger. */ 
     37    /** 
     38     * Error Logger. 
     39     */ 
    4140    private Logger paramLogger = Logger.getLogger("tmcsim.paramicscommunicator"); 
    42      
    43     /** Linked List of messages that have been received */ 
     41    /** 
     42     * Linked List of messages that have been received 
     43     */ 
    4444    private LinkedList<Element> queuedMessages = null; 
    45      
    46     /**  */ 
     45    /** 
     46     * 
     47     */ 
    4748    private String writerID = null; 
    48  
    49     /** File name of the file where data is written */ 
     49    /** 
     50     * File name of the file where data is written 
     51     */ 
    5052    private String outputFile = null; 
    51      
    52     /** FileWriter used to write data to the output file. */ 
     53    /** 
     54     * FileWriter used to write data to the output file. 
     55     */ 
    5356    private FileWriter fileWriter = null; 
    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. */ 
     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     */ 
    5964    private Timer writerTimer = null; 
    60      
     65    /** 
     66     * Synchronizing lock to protect File IO and message queuing. 
     67     */ 
     68    private Object lock = null; 
     69 
    6170    /** 
    6271     * A TimerTask to retry writing messages that have been queued within this 
    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              
     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 
    7489                //if we've queued something, continue. 
    75                 if(queuedMessages.size() > 0) {          
    76                      
     90                if (queuedMessages.size() > 0) 
     91                { 
     92 
    7793                    //if file has been modified, write to it 
    78                     if(lastModified < new File(outputFile).lastModified()) 
    79                     {        
    80                         try { 
     94                    if (lastModified < new File(outputFile).lastModified()) 
     95                    { 
     96                        try 
     97                        { 
    8198                            writeToFile(queuedMessages.getFirst()); 
    8299                            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); 
    83105                        } 
    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); 
     106 
     107                        //all queued messages gone, cancel timer 
     108                        if (queuedMessages.size() == 0) 
     109                        { 
     110                            this.cancel(); 
    88111                        } 
    89                          
    90                         //all queued messages gone, cancel timer 
    91                         if(queuedMessages.size() == 0) 
    92                             this.cancel(); 
    93                     }    
     112                    } 
    94113                } 
    95             }                
    96         } 
    97     } 
    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.   
     114            } 
     115        } 
     116    } 
     117 
     118    /** 
     119     * Constructor. Initialize data objects. If the target file exists, delete 
     120     * it, and then create a new file. 
    105121     * 
    106122     * @param workingDir Directory path where the output file is to be written 
    107123     * @param mess The ParamicsCommMessage containing the outputFile filename. 
    108124     */ 
    109     public ParamicsFileWriter(String id, String workingDir, String targetFile) { 
    110  
    111         try { 
     125    public ParamicsFileWriter(String id, String workingDir, String targetFile) 
     126    { 
     127 
     128        try 
     129        { 
    112130            writerID = id; 
    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()) { 
     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            { 
    121140                tempFile.delete(); 
    122141            } 
    123142 
    124             tempFile.createNewFile();            
    125                                                  
    126             writerTimer     = new Timer(); 
    127              
    128         } catch (IOException ioe) { 
    129             paramLogger.logp(Level.SEVERE, "ParamicsFileWriter", "Constructor",  
     143            tempFile.createNewFile(); 
     144 
     145            writerTimer = new Timer(); 
     146 
     147        } catch (IOException ioe) 
     148        { 
     149            paramLogger.logp(Level.SEVERE, "ParamicsFileWriter", "Constructor", 
    130150                    "Unable to create Paramics File Writer.", ioe); 
    131151        } 
    132          
    133     } 
    134      
    135      
     152 
     153    } 
     154 
    136155    /** 
    137156     * Method is called when a message has been received from the CAD Simulator. 
    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. 
    144      * 
    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) { 
     157     * If the message queue is not empty, add the new message to the queue. If 
     158     * the output file has not been modified (read) since last write, add the 
     159     * message to the queue and set a timer to repeatedly check for modification 
     160     * to the output file. Else, write the new message to the file. If there is 
     161     * an error in writing the data, queue the message start a timer to retry 
     162     * the writing. 
     163     * 
     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            { 
    154178                queuedMessages.add(messageElem); 
    155179 
    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(),  
     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(), 
    164190                        0L, TIMER_DURATION); 
    165191 
    166192                paramLogger.log(Level.INFO, "First message queued"); 
    167             }            
    168             //free and clear, write. 
    169             else { 
    170                 try { 
     193            } //free and clear, write. 
     194            else 
     195            { 
     196                paramLogger.log(Level.INFO, "ParamicsFileWriter, writeMessage " 
     197                        + "try to writeToFile" + outputFile); 
     198                try 
     199                { 
    171200                    writeToFile(messageElem); 
    172                 } 
    173                 catch(IOException ioe) { 
    174                     paramLogger.logp(Level.SEVERE, "ParamicsFileWriter",  
    175                             "writeMessage()", "Exception in writing to the " + 
    176                             "target file: " + outputFile, ioe); 
    177                      
     201                } catch (IOException ioe) 
     202                { 
     203                    paramLogger.logp(Level.SEVERE, "ParamicsFileWriter", 
     204                            "writeMessage()", "Exception in writing to the " 
     205                            + "target file: " + outputFile, ioe); 
     206 
    178207                    queuedMessages.add(messageElem); 
    179                      
    180                     writerTimer.scheduleAtFixedRate(new WriterTimerTask(),  
     208 
     209                    writerTimer.scheduleAtFixedRate(new WriterTimerTask(), 
    181210                            0L, TIMER_DURATION); 
    182211                } 
     212                paramLogger.log(Level.INFO, "ParamicsFileWriter, writeMessage " 
     213                        + "writeToFile succeeded."); 
    183214            } 
    184215        } 
    185216    } 
    186          
     217 
    187218    /** 
    188219     * Method writes data to the output file. 
    189      *  
     220     * 
    190221     * @param output Data to be written to the file. 
    191222     */ 
    192     private void writeToFile(Element output) throws IOException {        
    193          
     223    private void writeToFile(Element output) throws IOException 
     224    { 
     225 
    194226        fileWriter = new FileWriter(outputFile); 
    195          
    196         OutputFormat of = new OutputFormat("XML","ISO-8859-1",true); 
     227 
     228        OutputFormat of = new OutputFormat("XML", "ISO-8859-1", true); 
    197229        of.setIndent(1); 
    198230        of.setIndenting(true); 
    199          
     231 
    200232        XMLSerializer serializer = new XMLSerializer(fileWriter, of); 
    201233        serializer.asDOMSerializer(); 
    202234        serializer.serialize(output); 
    203235 
    204         /** Added by Nathaniel Lehrer */ 
    205         try { 
     236        /** 
     237         * Added by Nathaniel Lehrer 
     238         */ 
     239        try 
     240        { 
    206241            java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream(); 
    207242            new XMLSerializer(outputStream, of).serialize(output); 
    208243            tmcsim.paramicslog.ParamicsLog.getInstance().writeToLog(outputStream.toString()); 
    209         } catch(Exception e) { 
     244        } catch (Exception e) 
     245        { 
    210246            System.out.println(e); 
    211247        } 
    212         /** End Add by Nathaniel Lehrer */ 
    213          
     248        /** 
     249         * End Add by Nathaniel Lehrer 
     250         */ 
    214251        fileWriter.flush(); 
    215252        fileWriter.close(); 
    216          
    217         lastModified = new File(outputFile).lastModified();      
    218          
     253 
     254        lastModified = new File(outputFile).lastModified(); 
     255 
    219256        setChanged(); 
    220257        notifyObservers(new FileIOUpdate(IO_TYPE.WRITE, writerID, new File(outputFile).length())); 
    221                              
    222     }                
     258 
     259    } 
    223260} 
Note: See TracChangeset for help on using the changeset viewer.