| 1 | package tmcsim.cadsimulator.paramicscontrol; |
|---|
| 2 | |
|---|
| 3 | import java.io.ByteArrayInputStream; |
|---|
| 4 | import java.util.logging.Level; |
|---|
| 5 | |
|---|
| 6 | import javax.xml.parsers.SAXParserFactory; |
|---|
| 7 | |
|---|
| 8 | import org.w3c.dom.Node; |
|---|
| 9 | import org.xml.sax.SAXParseException; |
|---|
| 10 | import org.xml.sax.helpers.DefaultHandler; |
|---|
| 11 | |
|---|
| 12 | import tmcsim.cadsimulator.managers.ParamicsSimulationManager; |
|---|
| 13 | import tmcsim.common.CADEnums.PARAMICS_STATUS; |
|---|
| 14 | |
|---|
| 15 | /** |
|---|
| 16 | * ParamicsStatusReader extends from the Abstrct ParamicsReader to provide |
|---|
| 17 | * the methods necessary to read the Paramics Status file. The receive() |
|---|
| 18 | * method is overloaded to parse the XML node for status information. |
|---|
| 19 | * The ParamicsSimulationManager is notified after each successful parse |
|---|
| 20 | * of a Paramics Status Node. |
|---|
| 21 | * |
|---|
| 22 | * @author Matthew Cechini |
|---|
| 23 | * @version |
|---|
| 24 | */ |
|---|
| 25 | public class ParamicsStatusReader extends ParamicsReader { |
|---|
| 26 | |
|---|
| 27 | /** Current Paramics Status. */ |
|---|
| 28 | protected PARAMICS_STATUS currentStatus; |
|---|
| 29 | |
|---|
| 30 | /** Current Network ID that has been loaded. */ |
|---|
| 31 | protected int currentNetwork; |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Reference to the ParamicsSimulationManager used to send notifications |
|---|
| 35 | * of Paramics status changes. |
|---|
| 36 | */ |
|---|
| 37 | protected ParamicsSimulationManager paramicsSimMgr = null; |
|---|
| 38 | |
|---|
| 39 | /** SAX Handler that is used to parse the recieved Paramics Status Node. */ |
|---|
| 40 | protected ParamicsStatusHandler psh = null; |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * Constructor. Initialize Paramics Status to UNKNOWN and network loaded to -1. |
|---|
| 45 | * |
|---|
| 46 | * @param theParamSimMgr The ParamicsSimulationManager object. |
|---|
| 47 | */ |
|---|
| 48 | public ParamicsStatusReader(ParamicsSimulationManager theParamSimMgr) { |
|---|
| 49 | paramicsSimMgr = theParamSimMgr; |
|---|
| 50 | |
|---|
| 51 | psh = new ParamicsStatusHandler(); |
|---|
| 52 | |
|---|
| 53 | currentStatus = PARAMICS_STATUS.UNKNOWN; |
|---|
| 54 | currentNetwork = -1; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | /** Get the current paramics status */ |
|---|
| 58 | public PARAMICS_STATUS getStatus() { return currentStatus; } |
|---|
| 59 | |
|---|
| 60 | /** Get the ID of the current paramics network. (Default: -1) */ |
|---|
| 61 | public int getNetworkID() { return currentNetwork; } |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * Reset the Paramics Status info. The currentStatus will be set to UNKNOWN |
|---|
| 65 | * and the currentNetwork set to -1. |
|---|
| 66 | */ |
|---|
| 67 | public void resetStatusInfo() { |
|---|
| 68 | currentStatus = PARAMICS_STATUS.UNKNOWN; |
|---|
| 69 | currentNetwork = -1; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | * This method parses the received XML Node with the local |
|---|
| 74 | * ParamicsStatusHandler. The parsed Paramics Status is sent to the |
|---|
| 75 | * ParamicsSimulationManager. |
|---|
| 76 | */ |
|---|
| 77 | public void receive(Node rxMessage) { |
|---|
| 78 | |
|---|
| 79 | try { |
|---|
| 80 | if(rxMessage.getTextContent().length() > 0) |
|---|
| 81 | SAXParserFactory.newInstance().newSAXParser().parse( |
|---|
| 82 | new ByteArrayInputStream(rxMessage.getTextContent().getBytes()), psh); |
|---|
| 83 | } |
|---|
| 84 | catch (Exception e) { |
|---|
| 85 | paramLogger.logp(Level.SEVERE, "ParamicsStatusReader", "receive", |
|---|
| 86 | "Exception in parsing received ParamicsCommMessage.", e); |
|---|
| 87 | paramLogger.logp(Level.INFO, "ParamicsStatusReader", "receive", |
|---|
| 88 | "Invalid received bytes", rxMessage.getTextContent().getBytes()); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | paramicsSimMgr.updateParamicsStatus(currentStatus); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * Internal SAX Handler used to parse the Camera Status Document read by |
|---|
| 96 | * the remote Status Reader. The schema for this document is: <br/> |
|---|
| 97 | * |
|---|
| 98 | * <Paramics> |
|---|
| 99 | * <Network_Status/> ("LOADING", "WARMING", or "LOADED") |
|---|
| 100 | * <Network_ID/> (integer network ID) |
|---|
| 101 | * </Paramics> |
|---|
| 102 | */ |
|---|
| 103 | protected class ParamicsStatusHandler extends DefaultHandler { |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | /** String XML creation. Tag contains information for the paramics status. */ |
|---|
| 107 | private final String NETWORK_STATUS = "Network_Status"; |
|---|
| 108 | |
|---|
| 109 | /** String XML creation. Tag contains information for the loaded paramics network id. */ |
|---|
| 110 | private final String NETWORK_ID = "Network_ID"; |
|---|
| 111 | |
|---|
| 112 | private StringBuffer parsedValue = new StringBuffer(); |
|---|
| 113 | |
|---|
| 114 | public void startDocument() { |
|---|
| 115 | |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | public void characters(char[] ch, int start, int length) { |
|---|
| 119 | parsedValue.append(new String(ch, start, length).trim()); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | public void endElement(String uri, String localName, String qName) { |
|---|
| 123 | |
|---|
| 124 | if(qName.equals(NETWORK_STATUS)) { |
|---|
| 125 | |
|---|
| 126 | currentStatus = PARAMICS_STATUS.UNKNOWN; |
|---|
| 127 | for(PARAMICS_STATUS val : PARAMICS_STATUS.values()) { |
|---|
| 128 | if(val.toString().equals(parsedValue.toString())) |
|---|
| 129 | currentStatus = val; |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | else if(qName.equals(NETWORK_ID)) { |
|---|
| 133 | currentNetwork = Integer.parseInt(parsedValue.toString()); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | parsedValue.setLength(0); |
|---|
| 137 | |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | public void error(SAXParseException e) { |
|---|
| 141 | paramLogger.logp(Level.SEVERE, "ParamicsStatusReader:ParamicsStatusHandler", |
|---|
| 142 | "error", "Error in parsing received ParamicsCommMessage.", e); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | public void fatalError(SAXParseException e) { |
|---|
| 146 | paramLogger.logp(Level.SEVERE, "ParamicsStatusReader:ParamicsStatusHandler", |
|---|
| 147 | "error", "Fatal error in parsing received ParamicsCommMessage.", e); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | public void warning(SAXParseException e) { |
|---|
| 151 | paramLogger.logp(Level.WARNING, "ParamicsStatusReader:ParamicsStatusHandler", |
|---|
| 152 | "error", "Warning in parsing received ParamicsCommMessage.", e); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | } |
|---|