Ignore:
Timestamp:
04/20/2016 02:29:52 PM (10 years ago)
Author:
jdalbey
Message:

Multifile commit. Add version # to Paramics Communicator. Move Load button in Sim Mgr. Add several tests. Add package jars target.

File:
1 edited

Legend:

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

    r2 r6  
    1010import java.util.logging.Level; 
    1111import java.util.logging.Logger; 
    12  
    1312import javax.xml.parsers.DocumentBuilderFactory; 
    14  
    1513import org.w3c.dom.Document; 
    1614import org.w3c.dom.Element; 
    17  
    1815import tmcsim.common.CADProtocol.PARAMICS_ACTIONS; 
    1916import tmcsim.common.CADProtocol.PARAMICS_COMM_TAGS; 
    2017import tmcsim.paramicscommunicator.FileIOUpdate.IO_TYPE; 
    2118 
    22  
    2319/** 
    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. 
    2924 * 
    3025 * @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. 
    3232 */ 
    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 */ 
     33public 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     */ 
    3943    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     */ 
    4247    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     */ 
    4551    private FileReader fileReader = null; 
    46      
    47     /** FileWriter used to clear the input file. */ 
     52    /** 
     53     * FileWriter used to clear the input file. 
     54     */ 
    4855    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     */ 
    5159    private long lastModified = 0; 
    52      
    53     /** Timer used to schedule file reading tasks. */ 
     60    /** 
     61     * Timer used to schedule file reading tasks. 
     62     */ 
    5463    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. 
    5967     */ 
    6068    private long readerInterval; 
    61      
     69 
    6270    /** 
    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     * 
    6575     * @author Matthew Cechini 
    6676     */ 
    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                { 
    7388                    Document readerDoc = DocumentBuilderFactory.newInstance() 
    74                         .newDocumentBuilder().newDocument(); 
    75              
     89                            .newDocumentBuilder().newDocument(); 
     90 
    7691                    Element readerElem = readerDoc.createElement(PARAMICS_COMM_TAGS.READER.tag); 
    7792                    readerElem.setAttribute(PARAMICS_COMM_TAGS.ID.tag, readerID); 
    78                     readerElem.setAttribute(PARAMICS_COMM_TAGS.ACTION.tag,  
     93                    readerElem.setAttribute(PARAMICS_COMM_TAGS.ACTION.tag, 
    7994                            PARAMICS_ACTIONS.READ_FILE.action); 
    80                      
     95 
    8196                    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)); 
    8399                    readerElem.appendChild(messageElem); 
    84                      
    85                     readerDoc.appendChild(readerElem);           
    86                          
     100 
     101                    readerDoc.appendChild(readerElem); 
     102 
    87103                    setChanged(); 
    88104                    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); 
    95111                } 
    96112            } 
     
    98114    } 
    99115 
    100  
    101116    /** 
    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 
    110126     * @param mess ParamicsCommMessage object containing registration data. 
    111127     * @param theComm Reference to the ParamicsCommunicator. 
    112128     */ 
    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; 
    117135            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            { 
    122141                inputFile.createNewFile(); 
    123142            } 
    124              
     143 
    125144            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", 
    131151                    "Constructor()", "Exception in initializing file reading.", ioe); 
    132152        } 
    133153    } 
    134      
    135      
     154 
    136155    /** 
    137      * Method opens the target file and reads all contents.  The file is then  
     156     * Method opens the target file and reads all contents. The file is then 
    138157     * cleared. 
    139158     * 
    140159     * @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. 
    142162     */ 
    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 
    147168        fileReader = new FileReader(inputFile); 
    148         fileReader.read(input);      
     169        fileReader.read(input); 
    149170        fileReader.close(); 
    150          
     171 
    151172        //make sure the new file has a modified time that is different 
    152         try { 
     173        try 
     174        { 
    153175            Thread.sleep(1000); 
     176        } catch (Exception e) 
     177        { 
    154178        } 
    155         catch (Exception e) {} 
    156          
     179 
    157180        //clear file after reading contents 
    158181        fileWriter = new FileWriter(inputFile); 
    159182        fileWriter.write(""); 
    160183        fileWriter.close(); 
    161          
     184 
    162185        lastModified = inputFile.lastModified(); 
    163          
     186 
    164187        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 
    167190        return new String(input); 
    168     }        
     191    } 
    169192} 
Note: See TracChangeset for help on using the changeset viewer.