Changeset 62 in tmcsimulator for trunk/src/tmcsim/client/CADClockDisplay.java
- Timestamp:
- 03/15/2017 02:53:35 PM (9 years ago)
- File:
-
- 1 edited
-
trunk/src/tmcsim/client/CADClockDisplay.java (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/tmcsim/client/CADClockDisplay.java
r61 r62 1 1 package tmcsim.client; 2 2 3 import java.awt.event.ActionEvent; 4 import java.awt.event.ActionListener; 3 5 import java.io.FileInputStream; 4 6 import java.io.FileOutputStream; … … 7 9 import java.rmi.RemoteException; 8 10 import java.rmi.server.UnicastRemoteObject; 9 import java.text.SimpleDateFormat;10 import java.util.Date;11 11 import java.util.Properties; 12 12 import java.util.Vector; … … 14 14 import java.util.logging.Level; 15 15 import java.util.logging.Logger; 16 17 16 import javax.swing.JOptionPane; 18 17 import javax.swing.JWindow; 18 import javax.swing.Timer; 19 19 import javax.swing.UIManager; 20 21 20 import tmcsim.client.cadclientgui.CADClientGUI; 22 21 import tmcsim.common.SimulationException; … … 25 24 26 25 /** 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. 55 29 */ 56 30 public class CADClockDisplay extends UnicastRemoteObject implements 57 31 CADClientInterface 58 32 { 59 60 33 /** 61 34 * Error logger. … … 72 45 private static enum PROPERTIES 73 46 { 74 75 47 CAD_SIM_HOST("CADSimulatorHost"), CAD_SIM_PORT("CADSimulatorSocketPort"), CAD_RMI_PORT( 76 48 "CADRmiPort"), CLIENT_CAD_POS("CADPosition"), CLIENT_USER_ID( … … 114 86 private CADClientInterface client = this; 115 87 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 125 90 /** 126 91 * 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. 136 93 * 137 94 * @param propertiesFile File path (absolute or relative) to the properties … … 149 106 cadClientProp.getProperty(PROPERTIES.CAD_RMI_PORT.name).trim()); 150 107 151 // Instantiate the CADScreenView and set up the model-view observer152 // relationship.153 108 theClientScreenView = new CADClockView(); 154 109 theClientScreenView.setVisible(true); 155 110 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(); 174 127 175 128 ensureProperShutdown(); … … 259 212 cadClientProp.store(new FileOutputStream(propertiesFile), 260 213 ""); 261 } else 214 } 215 else 262 216 { 263 217 throw new SimulationException( … … 277 231 cadClientProp.store(new FileOutputStream(propertiesFile), 278 232 ""); 279 } else 233 } 234 else 280 235 { 281 236 throw new SimulationException( … … 457 412 }); 458 413 } 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 } 459 428 }
Note: See TracChangeset
for help on using the changeset viewer.
