| 1 | package tmcsim.cadsimulator.viewer.model; |
|---|
| 2 | |
|---|
| 3 | import java.util.logging.Handler; |
|---|
| 4 | import java.util.logging.Level; |
|---|
| 5 | import java.util.logging.LogRecord; |
|---|
| 6 | import java.util.logging.Logger; |
|---|
| 7 | import tmcsim.common.CADEnums; |
|---|
| 8 | import tmcsim.common.CADEnums.PARAMICS_STATUS; |
|---|
| 9 | |
|---|
| 10 | /** |
|---|
| 11 | * SimulatorStatus represents the status of the current simulation. This |
|---|
| 12 | * information includes: |
|---|
| 13 | * |
|---|
| 14 | * <ul> |
|---|
| 15 | * <li>Current simulation time.</li> |
|---|
| 16 | * <li>Current simulation status.</li> |
|---|
| 17 | * <li>Number of remote CAD Clients connected.</li> |
|---|
| 18 | * <li>Status of Simulation Manager connection.</li> |
|---|
| 19 | * <li>Status of Paramics connection.</li> |
|---|
| 20 | * <li>Paramics Network Loaded</li> |
|---|
| 21 | * <li>Information log messages.</li> |
|---|
| 22 | * <li>Error log messages</li> |
|---|
| 23 | * </ul> |
|---|
| 24 | * |
|---|
| 25 | * @author jdalbey |
|---|
| 26 | * @version 0.1 |
|---|
| 27 | */ |
|---|
| 28 | @SuppressWarnings("serial") |
|---|
| 29 | public class CADSimulatorStatus extends java.util.Observable |
|---|
| 30 | { |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * Count of how many CAD clients have connected. |
|---|
| 34 | */ |
|---|
| 35 | private int numClientsConnected; |
|---|
| 36 | /** |
|---|
| 37 | * Is the Simulation Manager connected? |
|---|
| 38 | */ |
|---|
| 39 | private boolean simMgrStatus; |
|---|
| 40 | /** |
|---|
| 41 | * The current state of the script this simulation has loaded. |
|---|
| 42 | */ |
|---|
| 43 | private CADEnums.SCRIPT_STATUS scriptState; |
|---|
| 44 | /** |
|---|
| 45 | * The current simulation elapsed time. |
|---|
| 46 | */ |
|---|
| 47 | private long currentTime; |
|---|
| 48 | /** |
|---|
| 49 | * The current state of the connectio to paramics |
|---|
| 50 | */ |
|---|
| 51 | private PARAMICS_STATUS paramicsConnectionStatus; |
|---|
| 52 | /** |
|---|
| 53 | * ID of currently loaded paramics network |
|---|
| 54 | */ |
|---|
| 55 | private String networkID; |
|---|
| 56 | /** |
|---|
| 57 | * Logging ErrorHandler. |
|---|
| 58 | */ |
|---|
| 59 | private SimulatorErrorHandler errorHandler; |
|---|
| 60 | private StringBuffer infoMessages; |
|---|
| 61 | private StringBuffer errorMessages; |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * Constructor. Initialize GUI Objects. Register the logging handler to |
|---|
| 65 | * listen for log records from all loggers that exist in the |
|---|
| 66 | * "tmcsim.cadsimulator" package structure. |
|---|
| 67 | */ |
|---|
| 68 | public CADSimulatorStatus() |
|---|
| 69 | { |
|---|
| 70 | errorHandler = new SimulatorErrorHandler(); |
|---|
| 71 | Logger.getLogger("tmcsim.cadsimulator").addHandler(errorHandler); |
|---|
| 72 | scriptState = CADEnums.SCRIPT_STATUS.NO_SCRIPT; |
|---|
| 73 | paramicsConnectionStatus = PARAMICS_STATUS.DISCONNECTED; |
|---|
| 74 | networkID = ""; |
|---|
| 75 | numClientsConnected = 0; |
|---|
| 76 | infoMessages = new StringBuffer(); |
|---|
| 77 | errorMessages = new StringBuffer(); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | public String getInfoMessages() |
|---|
| 81 | { |
|---|
| 82 | return infoMessages.toString(); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | public String getErrorMessages() |
|---|
| 86 | { |
|---|
| 87 | return errorMessages.toString(); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /** |
|---|
| 91 | * Method is called when a CAD Client connects to the Server. |
|---|
| 92 | * The displayed number of connected clients is incremented by one. |
|---|
| 93 | */ |
|---|
| 94 | public void connectClient() |
|---|
| 95 | { |
|---|
| 96 | numClientsConnected += 1; |
|---|
| 97 | setChanged(); |
|---|
| 98 | notifyObservers(); |
|---|
| 99 | |
|---|
| 100 | //termConnectedTF.setText(String.valueOf(numClientsConnected)); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | /** |
|---|
| 104 | * Method is called when a CAD Client disconnects from the CAD Simulator. |
|---|
| 105 | * The displayed number of connected clients is decremented by one. |
|---|
| 106 | */ |
|---|
| 107 | public void disconnectClient() |
|---|
| 108 | { |
|---|
| 109 | if (numClientsConnected > 0) |
|---|
| 110 | { |
|---|
| 111 | numClientsConnected--; |
|---|
| 112 | } |
|---|
| 113 | setChanged(); |
|---|
| 114 | notifyObservers(); |
|---|
| 115 | |
|---|
| 116 | //termConnectedTF.setText(String.valueOf(numClientsConnected)); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | /** |
|---|
| 120 | * Accessor to current number of clients. |
|---|
| 121 | * |
|---|
| 122 | * @return number of connected clients |
|---|
| 123 | */ |
|---|
| 124 | public int getNumClients() |
|---|
| 125 | { |
|---|
| 126 | return numClientsConnected; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | /** |
|---|
| 130 | * Method is called when Simulation Manager connects or disconnects. |
|---|
| 131 | * |
|---|
| 132 | * @param connection True if simulation manager is connected, false if not. |
|---|
| 133 | */ |
|---|
| 134 | public void setSimManagerStatus(boolean connection) |
|---|
| 135 | { |
|---|
| 136 | simMgrStatus = connection; |
|---|
| 137 | setChanged(); |
|---|
| 138 | notifyObservers(); |
|---|
| 139 | |
|---|
| 140 | //managerConnectedTF.setText("Yes"); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | /** |
|---|
| 144 | * Accessor to Sim Mgr connection status |
|---|
| 145 | * |
|---|
| 146 | * @return number of connected clients |
|---|
| 147 | */ |
|---|
| 148 | public boolean isSimManagerConnected() |
|---|
| 149 | { |
|---|
| 150 | return simMgrStatus; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | /** |
|---|
| 154 | * Method called to convert current simulation time (parameter long value) |
|---|
| 155 | * to a string of format H:MM:SS. Time is then updated on GUI. |
|---|
| 156 | * |
|---|
| 157 | * @param seconds Long value of current time |
|---|
| 158 | */ |
|---|
| 159 | public void setTime(long seconds) |
|---|
| 160 | { |
|---|
| 161 | currentTime = seconds; |
|---|
| 162 | setChanged(); |
|---|
| 163 | notifyObservers(); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | public String getCurrentTime() |
|---|
| 167 | { |
|---|
| 168 | String time = new String(); |
|---|
| 169 | long timeSegment; |
|---|
| 170 | long seconds = currentTime; |
|---|
| 171 | timeSegment = seconds / 3600; |
|---|
| 172 | time += String.valueOf(timeSegment) + ":"; |
|---|
| 173 | |
|---|
| 174 | seconds = seconds % 3600; |
|---|
| 175 | |
|---|
| 176 | timeSegment = seconds / 60; |
|---|
| 177 | if (timeSegment < 10) |
|---|
| 178 | { |
|---|
| 179 | time += "0"; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | time += String.valueOf(timeSegment) + ":"; |
|---|
| 183 | seconds = seconds % 60; |
|---|
| 184 | |
|---|
| 185 | timeSegment = seconds; |
|---|
| 186 | if (timeSegment < 10) |
|---|
| 187 | { |
|---|
| 188 | time += "0"; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | time += String.valueOf(timeSegment); |
|---|
| 192 | return time; |
|---|
| 193 | //simulationClockLabel.setText(time); |
|---|
| 194 | |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | /** |
|---|
| 198 | * Method is called to display the current status of the simulation. |
|---|
| 199 | * |
|---|
| 200 | * @param newStatus Current status of simulation. The following table |
|---|
| 201 | * describes each possible status and what is displayed. Each status code is |
|---|
| 202 | * found as a public static int in the Coordinator Class. |
|---|
| 203 | * |
|---|
| 204 | * <table cellpadding="2" cellspacing="2" border="1" style="text-align: |
|---|
| 205 | * left; width: 250px;"> |
|---|
| 206 | * <tbody> |
|---|
| 207 | * <tr> |
|---|
| 208 | * <th>Status<br></th> |
|---|
| 209 | * <th>Actions Taken<br></th> |
|---|
| 210 | * </tr> |
|---|
| 211 | * <tr> |
|---|
| 212 | * <td>NO_SCRIPT<br></td> |
|---|
| 213 | * <td>Set the simulation status text to a black "No Script". <br></td> |
|---|
| 214 | * </tr> |
|---|
| 215 | * <tr> |
|---|
| 216 | * <td>SCRIPT_STOPPED_NOT_STARTED<br></td> |
|---|
| 217 | * <td>Set the simulation status text to a red "Ready". <br></td> |
|---|
| 218 | * </tr> |
|---|
| 219 | * <tr> |
|---|
| 220 | * <td>SCRIPT_PAUSED_STARTED<br></td> |
|---|
| 221 | * <td>Set the simulation status text to a red "Paused". <br></td> |
|---|
| 222 | * </tr> |
|---|
| 223 | * <tr> |
|---|
| 224 | * <td>SCRIPT_RUNNING<br></td> |
|---|
| 225 | * <td>Set the simulation status text to a green "Running". <br></td> |
|---|
| 226 | * </tr> |
|---|
| 227 | * <tr> |
|---|
| 228 | * <td>ATMS_SYNCHRONIZATION<br></td> |
|---|
| 229 | * <td>Set the simulation status text to an orange "Synchronizing". |
|---|
| 230 | * <br></td> |
|---|
| 231 | * </tr> |
|---|
| 232 | * </tbody> |
|---|
| 233 | * </table> |
|---|
| 234 | */ |
|---|
| 235 | public void setScriptStatus(CADEnums.SCRIPT_STATUS newStatus) |
|---|
| 236 | { |
|---|
| 237 | scriptState = newStatus; |
|---|
| 238 | setChanged(); |
|---|
| 239 | notifyObservers(); |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | public CADEnums.SCRIPT_STATUS getScriptStatus() |
|---|
| 243 | { |
|---|
| 244 | return scriptState; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | /** |
|---|
| 248 | * Method is called when a connection to paramics is made or dropped. |
|---|
| 249 | * |
|---|
| 250 | * @param newStatus The status denoting whether a connection has been made |
|---|
| 251 | * or dropped. |
|---|
| 252 | */ |
|---|
| 253 | public void setParamicsStatus(CADEnums.PARAMICS_STATUS newStatus) |
|---|
| 254 | { |
|---|
| 255 | paramicsConnectionStatus = newStatus; |
|---|
| 256 | setChanged(); |
|---|
| 257 | notifyObservers(); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | public CADEnums.PARAMICS_STATUS getParamicsStatus() |
|---|
| 261 | { |
|---|
| 262 | return paramicsConnectionStatus; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | /** |
|---|
| 266 | * Method is called when a paramics network is loaded. |
|---|
| 267 | * |
|---|
| 268 | * @param networkID Unique ID for Paramics network that has been loaded. |
|---|
| 269 | */ |
|---|
| 270 | public void setParamicsNetworkLoaded(String networkID) |
|---|
| 271 | { |
|---|
| 272 | this.networkID = networkID; |
|---|
| 273 | setChanged(); |
|---|
| 274 | notifyObservers(); |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | public String getParmicsNetworkID() |
|---|
| 278 | { |
|---|
| 279 | return networkID; |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | /** |
|---|
| 283 | * Logging Handler to listen for Information and Error messages logged for |
|---|
| 284 | * the CAD Simulator. Received LogRecords are displayed in the info or error |
|---|
| 285 | * message Text Area. |
|---|
| 286 | * |
|---|
| 287 | * @author Matthew Cechini |
|---|
| 288 | */ |
|---|
| 289 | private class SimulatorErrorHandler extends Handler |
|---|
| 290 | { |
|---|
| 291 | |
|---|
| 292 | public void close() throws SecurityException |
|---|
| 293 | { |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | public void flush() |
|---|
| 297 | { |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | public void publish(LogRecord rec) |
|---|
| 301 | { |
|---|
| 302 | StringBuffer msgBuffer = new StringBuffer(); |
|---|
| 303 | |
|---|
| 304 | msgBuffer.append(rec.getSourceClassName() + "." |
|---|
| 305 | + rec.getSourceMethodName() + " = " |
|---|
| 306 | + rec.getMessage()); |
|---|
| 307 | // which panel to display in |
|---|
| 308 | if (rec.getLevel() == Level.INFO) |
|---|
| 309 | { |
|---|
| 310 | infoMessages.append(msgBuffer); |
|---|
| 311 | } |
|---|
| 312 | else |
|---|
| 313 | { |
|---|
| 314 | errorMessages.append(msgBuffer); |
|---|
| 315 | } |
|---|
| 316 | setChanged(); |
|---|
| 317 | notifyObservers(); |
|---|
| 318 | } |
|---|
| 319 | } |
|---|
| 320 | } |
|---|