Changeset 62 in tmcsimulator for trunk/src/tmcsim/client


Ignore:
Timestamp:
03/15/2017 02:53:35 PM (9 years ago)
Author:
jdalbey
Message:

Clock Client version 1 done.

Location:
trunk/src/tmcsim/client
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tmcsim/client/CADClockDisplay.java

    r61 r62  
    11package tmcsim.client; 
    22 
     3import java.awt.event.ActionEvent; 
     4import java.awt.event.ActionListener; 
    35import java.io.FileInputStream; 
    46import java.io.FileOutputStream; 
     
    79import java.rmi.RemoteException; 
    810import java.rmi.server.UnicastRemoteObject; 
    9 import java.text.SimpleDateFormat; 
    10 import java.util.Date; 
    1111import java.util.Properties; 
    1212import java.util.Vector; 
     
    1414import java.util.logging.Level; 
    1515import java.util.logging.Logger; 
    16  
    1716import javax.swing.JOptionPane; 
    1817import javax.swing.JWindow; 
     18import javax.swing.Timer; 
    1919import javax.swing.UIManager; 
    20  
    2120import tmcsim.client.cadclientgui.CADClientGUI; 
    2221import tmcsim.common.SimulationException; 
     
    2524 
    2625/** 
    27  * CADClient is the main class for the CAD Client application. The main method 
    28  * instantiates an instance of the CADClient object with the default properties 
    29  * file "..\config\CADClient.properties" or the first argument fom the command 
    30  * line invocation. Properties data values are used to bind socket communication 
    31  * between the CAD Client and the CAD Simulator. The CADClientModel object is 
    32  * instantiated and the CAD Client registers itself with the CAD Simulator. 
    33  * Finally, the CADClockView is initialized, the model-view and observer 
    34  * relationships are established, and the view is shown.<br> 
    35  * <br> 
    36  * The properties file contains the following data: <br> 
    37  * <code> 
    38  * -----------------------------------------------------------------------------<br> 
    39  * Host Name The host name where the CAD Simulator is located.<br> 
    40  * Port Number The port number that the CAD Simulator is bound on.<br> 
    41  * CAD Position The integer (>= 0) position for this CAD Client.<br> 
    42  * CAD User ID The unique user id for this CAD Client.<br> 
    43  * Error File Filename of error logging file.<br> 
    44  * -----------------------------------------------------------------------------<br> 
    45  * Example File: <br> 
    46  * CADSimulatorHost = localhost<br> 
    47  * CADSimulatorSocketPort = 4444<br> 
    48  * CADPosition = 1 <br> 
    49  * CADUserID = A12345<br> 
    50  * ErrorFile = cad_client_err.txt<br> 
    51  * </code> 
    52  * 
    53  * @author Matthew Cechini (mcechini@calpoly.edu) 
    54  * @version $Date: 2009/04/17 16:27:47 $ $Revision: 1.8 $ 
     26 * CADClockDisplay shows the simulation clock time. It operates as a client of 
     27 * the CAD server, using RMI to poll the server every second for the current 
     28 * simulation clock time. 
    5529 */ 
    5630public class CADClockDisplay extends UnicastRemoteObject implements 
    5731        CADClientInterface 
    5832{ 
    59  
    6033    /** 
    6134     * Error logger. 
     
    7245    private static enum PROPERTIES 
    7346    { 
    74  
    7547        CAD_SIM_HOST("CADSimulatorHost"), CAD_SIM_PORT("CADSimulatorSocketPort"), CAD_RMI_PORT( 
    7648        "CADRmiPort"), CLIENT_CAD_POS("CADPosition"), CLIENT_USER_ID( 
     
    11486    private CADClientInterface client = this; 
    11587    private static final String CONFIG_FILE_NAME = "cad_client_config.properties"; 
    116  
    117     private String formatInterval(final long l) 
    118     { 
    119         final long hr = TimeUnit.SECONDS.toHours(l); 
    120         final long min = TimeUnit.SECONDS.toMinutes(l - TimeUnit.HOURS.toSeconds(hr)); 
    121         final long sec = TimeUnit.SECONDS.toSeconds(l - TimeUnit.HOURS.toSeconds(hr) - TimeUnit.MINUTES.toSeconds(min)); 
    122         return String.format("%02d:%02d:%02d", hr, min, sec); 
    123     } 
    124      
     88    private final static int ONE_SECOND = 1000; 
     89 
    12590    /** 
    12691     * Constructor. Initialize data from parsed properties file. Create a socket 
    127      * connection to the CADSimulator. The ClientScreenModel is initialized with 
    128      * the input and output I/O streams for socket communication. The 
    129      * ClientScreenModel registers with the CAD Simulator, using CAD position 
    130      * and userID read in from the properties file. The ClientScreenView is then 
    131      * created and initialized and set as an observer of the model. 
    132      * 
    133      * A thread is created with the runnable ClientScreenModel and is started. 
    134      * When this thread is no longer alive, or the ClientScrenView and 
    135      * CADClientSocket are closed. The program then exits. 
     92     * connection to the CADSimulator. 
    13693     * 
    13794     * @param propertiesFile File path (absolute or relative) to the properties 
     
    149106                cadClientProp.getProperty(PROPERTIES.CAD_RMI_PORT.name).trim()); 
    150107 
    151         // Instantiate the CADScreenView and set up the model-view observer 
    152         // relationship. 
    153108        theClientScreenView = new CADClockView(); 
    154109        theClientScreenView.setVisible(true); 
    155110 
    156         try 
    157         { 
    158             while (true) 
    159             { 
    160                 long simtime = theCoorInt.getCurrentSimulationTime(); 
    161                 //System.out.println("" + formatInterval(simtime)); 
    162                 theClientScreenView.updateTime("" + formatInterval(simtime)); 
    163                 Thread.sleep(1000); 
    164             } 
    165         } catch (InterruptedException ex) 
    166         { 
    167             Logger.getLogger(CADClockDisplay.class.getName()).log(Level.SEVERE, null, ex); 
    168         } 
    169  
    170         // Create the CAD Client thread to run the CADClientModel Object. 
    171 //        Thread clientThread = new Thread(theClientScreenModel); 
    172 //        clientThread.start(); 
    173  
     111        // Create a timer that fetches the simulation time every second. 
     112        Timer timer = new Timer(ONE_SECOND, new ActionListener() 
     113        { 
     114            public void actionPerformed(ActionEvent e) 
     115            { 
     116                try 
     117                { 
     118                    long simtime = theCoorInt.getCurrentSimulationTime(); 
     119                    theClientScreenView.updateTime("" + formatInterval(simtime)); 
     120                } catch (RemoteException ex) 
     121                { 
     122                    Logger.getLogger(CADClockDisplay.class.getName()).log(Level.SEVERE, null, ex); 
     123                } 
     124            } 
     125        }); 
     126        timer.start(); 
    174127 
    175128        ensureProperShutdown(); 
     
    259212                    cadClientProp.store(new FileOutputStream(propertiesFile), 
    260213                            ""); 
    261                 } else 
     214                } 
     215                else 
    262216                { 
    263217                    throw new SimulationException( 
     
    277231                    cadClientProp.store(new FileOutputStream(propertiesFile), 
    278232                            ""); 
    279                 } else 
     233                } 
     234                else 
    280235                { 
    281236                    throw new SimulationException( 
     
    457412        }); 
    458413    } 
     414 
     415    /** 
     416     * Format a time in seconds as HH:MM:SS 
     417     * 
     418     * @param l 
     419     * @return 
     420     */ 
     421    private String formatInterval(final long l) 
     422    { 
     423        final long hr = TimeUnit.SECONDS.toHours(l); 
     424        final long min = TimeUnit.SECONDS.toMinutes(l - TimeUnit.HOURS.toSeconds(hr)); 
     425        final long sec = TimeUnit.SECONDS.toSeconds(l - TimeUnit.HOURS.toSeconds(hr) - TimeUnit.MINUTES.toSeconds(min)); 
     426        return String.format("%02d:%02d:%02d", hr, min, sec); 
     427    } 
    459428} 
  • trunk/src/tmcsim/client/CADClockView.java

    r61 r62  
    44import java.awt.Dimension; 
    55import java.awt.Font; 
    6 import java.awt.event.KeyEvent; 
    7 import java.awt.event.KeyListener; 
    8 import java.util.Observable; 
    9 import java.util.Observer; 
    106import java.util.logging.Logger; 
    11  
    127import javax.swing.BorderFactory; 
    138import javax.swing.Box; 
     
    1510import javax.swing.JFrame; 
    1611import javax.swing.JLabel; 
    17 import javax.swing.JOptionPane; 
    1812import javax.swing.JPanel; 
    1913import javax.swing.WindowConstants; 
    20 import tmcsim.common.ObserverMessage; 
    21 import tmcsim.common.CADEnums.CADScreenNum; 
    22 import tmcsim.cadsimulator.viewer.model.CADSimulatorStatus; 
    2314 
    2415/** 
    2516 * The CADClientView class is the view component to the CAD Client application. 
    2617 * 
    27  * This view class observers the CADClientModel, listening for updates from the 
    28  * CAD Simulator. Updates includes the current CAD time 
    2918 */ 
    3019@SuppressWarnings("serial") 
    3120public class CADClockView extends JFrame 
    3221{ 
    33  
    3422    /** 
    3523     * Error Logger. 
     
    4735    public CADClockView() 
    4836    { 
    49         super("CAD Client"); 
     37        super("Simulation Clock"); 
    5038        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    51         currentTime = new JLabel("0:00:00"); 
    52         CADSimulatorStatus status = new CADSimulatorStatus(); 
    53         String simtime = status.getCurrentTime(); 
    54         currentTime.setText(simtime); 
     39        currentTime = new JLabel("00:00:00"); 
    5540        currentTime.setAlignmentX(Box.CENTER_ALIGNMENT); 
    56         currentTime.setFont(new Font("Geneva", Font.BOLD, 70)); 
     41        currentTime.setFont(new Font("Geneva", Font.BOLD, 200)); 
    5742        mainPane = new JPanel(); 
    58         setSize(new Dimension(730, 455)); 
    59         setMaximumSize(new Dimension(730, 455)); 
    60         setMinimumSize(new Dimension(730, 455)); 
     43        setSize(new Dimension(1100, 255)); 
     44        setMaximumSize(new Dimension(1100, 255)); 
     45        setMinimumSize(new Dimension(1100, 255)); 
    6146        mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS)); 
    6247        mainPane.setBorder(BorderFactory.createLineBorder(Color.black)); 
    63         mainPane.setBackground(Color.LIGHT_GRAY); 
     48        mainPane.setBackground(new Color(230, 230, 230));  // #E6E6E6 
    6449        mainPane.add(currentTime); 
    6550        add(mainPane); 
Note: See TracChangeset for help on using the changeset viewer.