Changeset 47 in tmcsimulator for trunk/src/tmcsim/paramicslog


Ignore:
Timestamp:
06/28/2016 02:25:42 PM (10 years ago)
Author:
jdalbey
Message:

Merge 305 modifications into trunk.

Location:
trunk/src/tmcsim/paramicslog
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tmcsim/paramicslog/ParamicsLog.java

    r33 r47  
    1010import java.util.logging.Level; 
    1111import java.util.logging.Logger; 
    12  
    1312import javax.swing.JOptionPane; 
    14  
    15 import org.w3c.dom.Element; 
    16  
    1713import tmcsim.common.SimulationException; 
    1814import tmcsim.interfaces.CoordinatorInterface; 
     
    2117/** 
    2218 * Logs communication from ParamicsCommunicator to ParamicsSimulator. 
    23  *  
    24  * The system property "PARAMICS_LOG_CONFIG" should be set to the path 
    25  * where the properties file for this class is located. <br><br> 
     19 * 
     20 * The system property "PARAMICS_LOG_CONFIG" should be set to the path where the 
     21 * properties file for this class is located. <br><br> 
    2622 * The data for the properties file follows. <br> 
    2723 * <code> 
    2824 * -----------------------------------------------------------------<br> 
    29  * Log File                The file to write the communication to. 
    30  * CAD Simulator Host      The host that runs the CAD Simulator. 
    31  * CAD Simulator RMI Port  The port on the host that runs the CAD  
    32  *                         Simulator where the RMI Coordinator 
    33  *                         object is registered to.  
     25 * Log File The file to write the communication to. CAD Simulator Host The host 
     26 * that runs the CAD Simulator. CAD Simulator RMI Port The port on the host that 
     27 * runs the CAD Simulator where the RMI Coordinator object is registered to. 
    3428 * -----------------------------------------------------------------<br> 
    3529 * Example File: <br> 
    36  * LogFile=c:\\log.txt 
    37  * CADSimulatorHost=localhost 
    38  * CADSimulatorRMIPort=4445 
     30 * LogFile=c:\\log.txt CADSimulatorHost=localhost CADSimulatorRMIPort=4445 
    3931 * -----------------------------------------------------------------<br> 
    4032 * </code> 
    41  *  
     33 * 
    4234 * @author Nathaniel Lehrer 
    43  * @version  
     35 * @version 
    4436 */ 
    45 public class ParamicsLog extends Observable  
    46 {    
     37public class ParamicsLog extends Observable 
     38{ 
     39 
    4740    /** 
    4841     * Enmeration containing property names. 
     42     * 
    4943     * @author Nathaniel Lehrer 
    5044     */ 
    51     private enum PROPERTIES  
    52     { 
    53         LOG_FILE      ("LogFile"), 
    54         CAD_SIM_HOST  ("CADSimulatorHost"), 
    55         CAD_SIM_PORT  ("CADSimulatorRMIPort"); 
    56          
     45    private enum PROPERTIES 
     46    { 
     47 
     48        LOG_FILE("LogFile"), 
     49        CAD_SIM_HOST("CADSimulatorHost"), 
     50        CAD_SIM_PORT("CADSimulatorRMIPort"); 
    5751        public String name; 
    58          
     52 
    5953        private PROPERTIES(String n) 
    6054        { 
     
    6256        } 
    6357    } 
    64  
    65         private static final String CONFIG_FILE_NAME = "paramics_communicator_logging.properties"; 
    66      
    67     /** Error logger. */ 
     58    private static final String CONFIG_FILE_NAME = "paramics_communicator_logging.properties"; 
     59    /** 
     60     * Error logger. 
     61    */ 
    6862    private static Logger paramLogger = Logger.getLogger("tmcsim.paramicslog"); 
    69      
    70     /** Static instance. */ 
     63    /** 
     64     * Static instance. 
     65     */ 
    7166    private static ParamicsLog instance; 
    72      
    73     /** Properties object. */ 
     67    /** 
     68     * Properties object. 
     69     */ 
    7470    private Properties paramicsLogProp; 
    75      
    76     /** File log entries are written to */ 
     71    /** 
     72     * File log entries are written to 
     73     */ 
    7774    private File logFile; 
    78      
    79     /** Stores the log entries. This contains the same information logFile. */ 
     75    /** 
     76     * Stores the log entries. This contains the same information logFile. 
     77     */ 
    8078    private StringBuilder log; 
    81  
    82     /** Remote reference to the simulation */ 
     79    /** 
     80     * Remote reference to the simulation 
     81     */ 
    8382    private CoordinatorInterface theCoorInt; 
    84      
    85     /** Object for synchronizing IO */ 
     83    /** 
     84     * Object for synchronizing IO 
     85     */ 
    8686    Object lock; 
    87      
     87 
    8888    /** 
    8989     * Creates the singleton instance of this class. 
    9090     */ 
    91     static { 
    92         try { 
    93                 if(System.getProperty("CONFIG_DIR") == null){ 
    94                 System.setProperty("CONFIG_DIR", "config"); 
    95             } 
    96                  
    97                 instance = new ParamicsLog(System.getProperty("CONFIG_DIR") + System.getProperty("file.separator") + CONFIG_FILE_NAME); 
    98         }  
    99         catch (Exception e)  
     91    static 
     92    { 
     93        try 
     94        { 
     95            if (System.getProperty("CONFIG_DIR") == null) 
     96            { 
     97                System.setProperty("CONFIG_DIR", "config"); 
     98            } 
     99 
     100            instance = new ParamicsLog(System.getProperty("CONFIG_DIR") + System.getProperty("file.separator") + CONFIG_FILE_NAME); 
     101        } catch (Exception e) 
    100102        { 
    101103            instance = new ParamicsLog(); 
    102              
    103             paramLogger.logp(Level.WARNING, "ParamicsLog", "static initializer",  
     104 
     105            paramLogger.logp(Level.WARNING, "ParamicsLog", "static initializer", 
    104106                    "Error occured initializing application", e); 
    105107 
    106             JOptionPane.showMessageDialog(null, e.getMessage(),  
    107                     "Error - ParamicsLog will not save log to file.",  
    108                     JOptionPane.ERROR_MESSAGE);  
    109         } 
    110          
     108            JOptionPane.showMessageDialog(null, e.getMessage(), 
     109                    "Error - ParamicsLog will not save log to file.", 
     110                    JOptionPane.ERROR_MESSAGE); 
     111        } 
     112 
    111113        instance.addObserver(ParamicsLogGUI.getInstance()); 
    112114    } 
    113      
    114     /** 
    115      * Creates an instance of ParamicsLog that does not write to a file when  
     115 
     116    /** 
     117     * Creates an instance of ParamicsLog that does not write to a file when 
    116118     * writeToLog is called. 
    117119     */ 
    118     private ParamicsLog() { 
    119          
     120    private ParamicsLog() 
     121    { 
     122 
    120123        lock = new Object(); 
    121124        log = new StringBuilder(""); 
    122125    } 
    123      
    124     /** 
    125      * Creates an instance of ParamicsLog that writes to a file when writeToLog is called. 
     126 
     127    /** 
     128     * Creates an instance of ParamicsLog that writes to a file when writeToLog 
     129     * is called. 
     130     * 
    126131     * @param propertiesFile 
    127132     */ 
    128     private ParamicsLog(String propertiesFile) { 
     133    private ParamicsLog(String propertiesFile) 
     134    { 
    129135        this(); 
    130          
     136 
    131137        String logFile = null; 
    132138        String CADSIMHost = null; 
    133139        String CADSIMPort = null; 
    134          
    135         try { 
     140 
     141        try 
     142        { 
    136143            paramicsLogProp = new Properties(); 
    137144            paramicsLogProp.load(new FileInputStream(propertiesFile)); 
    138              
     145 
    139146            if ((logFile = paramicsLogProp.getProperty(PROPERTIES.LOG_FILE.name)) == null) 
    140147            { 
     
    142149            } 
    143150            else if ((CADSIMHost = paramicsLogProp.getProperty(PROPERTIES.CAD_SIM_HOST.name)) == null) 
    144             {    
     151            { 
    145152                throw new Exception("Properties file missing CAD Simulator host."); 
    146153            } 
     
    149156                throw new Exception("Properties file missing CAD Simulator RMI port."); 
    150157            } 
    151              
     158 
    152159            try 
    153160            { 
    154161                connect(CADSIMHost, CADSIMPort); 
    155             } 
    156             catch (Exception e) 
    157             { 
    158                 JOptionPane.showMessageDialog(null,  
    159                         "ParamicsLog: Could not connect to remote Coordinator object.",  
    160                         "Network Error", JOptionPane.ERROR_MESSAGE);                 
    161             } 
    162              
    163             try  
     162            } catch (Exception e) 
     163            { 
     164                JOptionPane.showMessageDialog(null, 
     165                        "ParamicsLog: Could not connect to remote Coordinator object.", 
     166                        "Network Error", JOptionPane.ERROR_MESSAGE); 
     167            } 
     168 
     169            try 
    164170            { 
    165171                createLogFile(logFile); 
    166             } 
    167             catch (Exception e) 
    168             { 
    169                 JOptionPane.showMessageDialog(null,  
    170                         "ParamicsLog: Could not create new log file.",  
     172            } catch (Exception e) 
     173            { 
     174                JOptionPane.showMessageDialog(null, 
     175                        "ParamicsLog: Could not create new log file.", 
    171176                        "File Error", JOptionPane.ERROR_MESSAGE); 
    172177            } 
    173              
    174         } catch (Exception e) { 
    175              
    176             paramLogger.logp(Level.WARNING, "ParamicsLog", "ParamicsLog constructor",  
     178 
     179        } catch (Exception e) 
     180        { 
     181 
     182            paramLogger.logp(Level.WARNING, "ParamicsLog", "ParamicsLog constructor", 
    177183                    "Properties file incorrect or missing.", e); 
    178              
    179             JOptionPane.showMessageDialog(null,  
    180                     "ParamicsLog: Properties file invalid.",  
     184 
     185            JOptionPane.showMessageDialog(null, 
     186                    "ParamicsLog: Properties file invalid.", 
    181187                    "Invalid Configuration", JOptionPane.ERROR_MESSAGE); 
    182188        } 
    183189    } 
    184      
     190 
    185191    /** 
    186192     * Creates the log file. 
     193     * 
    187194     * @param filePath The path to the file including the file name. 
    188195     * @throws IOException If the log file could not be created. 
     
    190197    private void createLogFile(String filePath) throws IOException 
    191198    { 
    192         try { 
     199        try 
     200        { 
    193201            logFile = new File(filePath); 
    194              
    195             if (logFile.exists()) { 
     202 
     203            if (logFile.exists()) 
     204            { 
    196205                logFile.delete(); 
    197206            } 
    198              
     207 
    199208            logFile.createNewFile(); 
    200              
    201         } catch (Exception e) { 
    202              
     209 
     210        } catch (Exception e) 
     211        { 
     212 
    203213            logFile = null; 
    204              
    205             paramLogger.logp(Level.WARNING, "ParamicsLog", "ParamicsLog constructor",  
     214 
     215            paramLogger.logp(Level.WARNING, "ParamicsLog", "ParamicsLog constructor", 
    206216                    "Could not create new log file.", e); 
    207              
     217 
    208218            throw new IOException("Could not create log file."); 
    209         }                
    210     } 
    211      
     219        } 
     220    } 
     221 
    212222    /** 
    213223     * Connect to the Coordinator's RMI object. 
    214      * @param hostname Host name of the CAD Simulator.     
    215      * @param portNumber Port number of the CAD Simulator RMI communication.  
    216      * @throws SimulationException if there is an error creating the RMI connection. 
    217      */  
    218     private void connect(String hostname, String portNumber)  
    219         throws SimulationException { 
    220          
     224     * 
     225     * @param hostname Host name of the CAD Simulator. 
     226     * @param portNumber Port number of the CAD Simulator RMI communication. 
     227     * @throws SimulationException if there is an error creating the RMI 
     228     * connection. 
     229     */ 
     230    private void connect(String hostname, String portNumber) 
     231            throws SimulationException 
     232    { 
     233 
    221234        String coorIntURL = ""; 
    222          
    223         try {   
    224             coorIntURL = "rmi://" + hostname + ":" + portNumber + "/coordinator";  
    225              
    226             theCoorInt = (CoordinatorInterface)Naming.lookup(coorIntURL);            
    227         } 
    228         catch (Exception e)  
    229         { 
    230             paramLogger.logp(Level.WARNING, "ParamicsLog",  
    231                     "establishRMIConnection", "Unable to establish RMI " + 
    232                     "communication with the CAD Simulator.  URL <" + coorIntURL + ">", e); 
    233         }    
    234     } 
    235      
     235 
     236        try 
     237        { 
     238            coorIntURL = "rmi://" + hostname + ":" + portNumber + "/coordinator"; 
     239 
     240            theCoorInt = (CoordinatorInterface) Naming.lookup(coorIntURL); 
     241        } catch (Exception e) 
     242        { 
     243            paramLogger.logp(Level.WARNING, "ParamicsLog", 
     244                    "establishRMIConnection", "Unable to establish RMI " 
     245                    + "communication with the CAD Simulator.  URL <" + coorIntURL + ">", e); 
     246        } 
     247    } 
     248 
    236249    /** 
    237250     * Accessor to the entries in the log. No file IO is used. 
     251     * 
    238252     * @return The entries in the log. 
    239253     */ 
    240     public String getLog() { 
    241          
     254    public String getLog() 
     255    { 
     256 
    242257        return log.toString(); 
    243258    } 
    244      
    245     /** 
    246      * Writes an entry to the log.  
    247      * The simulator time when the message was sent is prepended to the entry. 
    248      * Entries are padded by a blank line before and after them. 
    249      * TODO: Ensure output is written in order of request. 
     259 
     260    /** 
     261     * Writes an entry to the log. The simulator time when the message was sent 
     262     * is prepended to the entry. Entries are padded by a blank line before and 
     263     * after them. TODO: Ensure output is written in order of request. 
     264     * 
    250265     * @param entry 
    251266     */ 
    252     public void writeToLog(String entry) { 
    253          
     267    public void writeToLog(String entry) 
     268    { 
     269 
    254270        String time = "?"; 
    255271        String formattedEntry; 
    256          
     272 
    257273        if (theCoorInt != null) 
    258274        { 
     
    260276            { 
    261277                time = formatTime(theCoorInt.getCurrentSimulationTime()); 
    262             } 
    263             catch (Exception e) 
    264             { 
    265                 paramLogger.logp(Level.WARNING, "ParamicsLog",  
     278            } catch (Exception e) 
     279            { 
     280                paramLogger.logp(Level.WARNING, "ParamicsLog", 
    266281                        "RMICommunication", "Unable to communicate with RMI object", e); 
    267282            } 
    268283        } 
    269          
     284 
    270285        formattedEntry = "\n" + "<!-- Time written to file: " + time + " -->\n" + entry + "\n"; 
    271286        log.append(formattedEntry); 
    272          
     287 
    273288        if (logFile != null) 
    274289        { 
    275             try  
    276             { 
    277                 synchronized(lock) 
     290            try 
     291            { 
     292                synchronized (lock) 
    278293                { 
    279294                    FileWriter writer = new FileWriter(logFile, true); 
     
    282297                    writer.close(); 
    283298                } 
    284             }  
    285             catch (IOException e)  
    286             { 
    287                 paramLogger.logp(Level.WARNING, "ParamicsLog", "writeToLog",  
     299            } catch (IOException e) 
     300            { 
     301                paramLogger.logp(Level.WARNING, "ParamicsLog", "writeToLog", 
    288302                        "Could not write to log file.", e); 
    289303            } 
    290304        } 
    291          
     305 
    292306        setChanged(); 
    293307        notifyObservers(entry); 
    294308    } 
    295      
     309 
    296310    /** 
    297311     * Formats the time given in seconds to hh:mm:ss format. 
     312     * 
    298313     * @param time The time in seconds. 
    299314     */ 
    300     private String formatTime(long time) 
     315    public String formatTime(long time) 
    301316    { 
    302317        long seconds = time % 60; 
     
    306321        return padr(hours) + ":" + padr(minutes) + ":" + padr(seconds); 
    307322    } 
    308      
     323 
    309324    private String padr(long n) 
    310325    { 
     
    317332        } 
    318333    } 
    319      
     334 
    320335    /** 
    321336     * Accessor for static instance of this class. 
     337     * 
    322338     * @return The instance of this class. 
    323339     */ 
    324     public static ParamicsLog getInstance() { 
    325          
     340    public static ParamicsLog getInstance() 
     341    { 
     342 
    326343        return instance; 
    327344    } 
  • trunk/src/tmcsim/paramicslog/gui/ParamicsLogGUI.java

    r2 r47  
    11package tmcsim.paramicslog.gui; 
    22 
    3 import javax.swing.*; 
    43import java.awt.event.WindowEvent; 
    54import java.util.*; 
     5import javax.swing.*; 
    66 
    77/** 
    8  * The UI for ParamicsLog.  
     8 * The UI for ParamicsLog. 
     9 * 
    910 * @author Nathaniel Lehrer 
    1011 * @version 
    1112 */ 
    12 public class ParamicsLogGUI extends JFrame implements Observer { 
     13public class ParamicsLogGUI extends JFrame implements Observer 
     14{ 
    1315 
    14     /** The static instance */ 
     16    /** 
     17     * The static instance 
     18     */ 
    1519    private static ParamicsLogGUI instance = new ParamicsLogGUI(); 
     20    /** 
     21     * The text area to display the log in 
     22     */ 
     23    private JTextArea textArea; 
    1624 
    17     /** The text area to display the log in */ 
    18     private JTextArea textArea; 
    19      
    20     /** Creates an instance of this class */ 
    21     public ParamicsLogGUI() { 
    22          
     25    /** 
     26     * Creates an instance of this class 
     27     */ 
     28    public ParamicsLogGUI() 
     29    { 
     30 
    2331        setupGUI(); 
    2432    } 
    25      
    26     /** Creates the UI */ 
     33 
     34    /** 
     35     * Creates the UI 
     36     */ 
    2737    private void setupGUI() 
    2838    { 
    29         try { 
     39        try 
     40        { 
    3041            UIManager.setLookAndFeel( 
    31                 UIManager.getSystemLookAndFeelClassName()); 
    32         } catch (Exception e) { 
     42                    UIManager.getSystemLookAndFeelClassName()); 
     43        } catch (Exception ex) 
     44        { 
     45            System.out.println(ex.getMessage()); 
    3346            System.err.println("Couldn't use system look and feel."); 
    3447        } 
    35          
    36         this.addWindowListener(new java.awt.event.WindowAdapter() { 
    37              
     48 
     49        this.addWindowListener(new java.awt.event.WindowAdapter() 
     50        { 
    3851            public void windowClosing(WindowEvent e) 
    3952            { 
     
    4154            } 
    4255        }); 
    43          
    44          
     56 
     57 
    4558        setTitle("Paramics Log"); 
    46          
     59 
    4760        textArea = new JTextArea(); 
    4861        textArea.setColumns(60); 
    4962        textArea.setRows(30); 
    5063        JScrollPane scrollPane = new JScrollPane(textArea); 
    51          
     64 
    5265        getContentPane().add(scrollPane); 
    53          
     66 
    5467    } 
    55      
    56     /** Shows the UI window */ 
     68 
     69    /** 
     70     * Shows the UI window 
     71     */ 
    5772    public void display() 
    5873    { 
     
    6277 
    6378    /** 
    64      * Updates the text area. If the observable class given is of type ParamicsLog then 
    65      * the log entries are displayed. 
     79     * Updates the text area. If the observable class given is of type 
     80     * ParamicsLog then the log entries are displayed. 
     81     * 
    6682     * @param o The model for this viewer. 
    6783     * @param arg An argument that is not used. 
     
    7288        { 
    7389            textArea.setText(((tmcsim.paramicslog.ParamicsLog) o).getLog()); 
    74         }  
     90        } 
    7591 
    7692        repaint(); 
    7793    } 
    78      
     94 
    7995    /** 
    8096     * Accessor for the instance of ParamicsLogGUI. 
     97     * 
    8198     * @return The instance of ParamicsLogGUI. 
    8299     */ 
    83     public static ParamicsLogGUI getInstance() { 
     100    public static ParamicsLogGUI getInstance() 
     101    { 
    84102        return instance; 
    85103    } 
    86      
    87104} 
Note: See TracChangeset for help on using the changeset viewer.