📄 tcptemperaturesensor.java
字号:
} final int RETRY_COUNT = 20; protected void execute() throws Throwable { int retryCount = RETRY_COUNT; while ( retryCount > 0 ) { while ( isEnabled() ) { if ( br == null ) { // This means that the connection reestablishment // attempt has just failed break; } try { String line = br.readLine(); if ( line == null ) { complain(LOG_ERR, CH_LISTENER, "Socket broken, exiting loop"); processError("Connection Lost"); break; } StringTokenizer st = new StringTokenizer(line, " "); // There is a few reserved words: // // D Data // E Error // + Arrival // - Departure // // If the first word (as in 'whitespace delimited // sequence') is reserved, then the parsing logic // changes. Otherwise, we process it just as before. String header = st.nextToken(); if ( "E".equals(header) ) { processError(line.substring(2)); } else if ( "+".equals(header) ) { processArrival(st.nextToken()); } else if ( "-".equals(header) ) { processDeparture(st.nextToken()); } else if ( "D".equals(header) ) { processData(line.substring(2)); } else { processData(line); } } catch ( SSLException sslex ) { if ( sslex.getMessage() != null ) { if ( sslex.getMessage().equals("Unrecognized SSL message, plaintext connection?") ) { // Let's retry as insecure complain(LOG_WARNING, CH_LISTENER, "Can't establish secure connection to " + remoteHost + ":" + remotePort + ", other end seems to be plaintext"); complain(LOG_WARNING, CH_LISTENER, "Reverting to insecure connection"); socket = null; br = null; secure = false; break; } } else { // I don't know what it is throw sslex; } } } if ( !isEnabled() ) { return; } // If we're here, it means that the socket has gone bad and // retryCount is positive. Let's try to reestablish the // connection. Thread.sleep(10000); try { socket = new Socket(remoteHost, remotePort); br = new BufferedReader(new InputStreamReader(socket.getInputStream())); retryCount = RETRY_COUNT; complain(LOG_NOTICE, CH_LISTENER, "Connection reestablished"); } catch ( IOException ioex ) { complain(LOG_NOTICE, CH_LISTENER, "Can't reestablish the connection, " + retryCount + " retries left, cause:", ioex); socket = null; br = null; } retryCount--; } complain(LOG_CRIT, CH_LISTENER, "Retry count exceeded, sensor server unavailable, terminating"); } private void processData(String line) { StringTokenizer st = new StringTokenizer(line, " "); String address = st.nextToken(); String temp = st.nextToken(); TcpTemperatureSensor s = (TcpTemperatureSensor)listenerMap.get(address); if ( s == null ) { if ( !warningList.contains(address) ) { complain(LOG_WARNING, CH_LISTENER, "No listener for " + address); warningList.add(address); } } else { s.currentTemperatureChanged(null, new Double(Double.parseDouble(temp))); } } private void processError(String line) { // VT: FIXME: This may not be associated with a sensor address // (such as 1-Wire short circuit). Therefore, it may be // necessary to broadcast such a condition to all the sensors. // VT: NOTE: However, it may so happen that the error is indeed // related to an individual sensor, so let's try to process it // as usual. //complain(LOG_WARNING, CH_LISTENER, "Error reported: " + line); StringTokenizer st = new StringTokenizer(line, " "); String address = st.nextToken(); String error = line.substring(address.length() + 1); TcpTemperatureSensor s = (TcpTemperatureSensor)listenerMap.get(address); if ( s == null ) { error = line; complain(LOG_WARNING, CH_LISTENER, "General error: " + error); for ( Iterator i = listenerMap.keySet().iterator(); i.hasNext(); ) { address = i.next().toString(); s = (TcpTemperatureSensor)listenerMap.get(address); s.currentTemperatureChanged(null, error); } } else { s.currentTemperatureChanged(null, error); } } private void processArrival(String address) { // VT: NOTE: There's nothing to process, really. To the actual // consumers, the arrival itself doesn't mean anything at all // unless the sensor starts producing data - and this case is // already handled. complain(LOG_NOTICE, CH_LISTENER, "Sensor arrived: " + address); } private void processDeparture(String address) { complain(LOG_WARNING, CH_LISTENER, "Sensor departed: " + address); TcpTemperatureSensor s = (TcpTemperatureSensor)listenerMap.get(address); if ( s == null ) { if ( !warningList.contains(address) ) { complain(LOG_WARNING, CH_LISTENER, "No listener for " + address); warningList.add(address); } } else { // VT: NOTE: Let the consumer worry about handling the // momentary lapses - the control logic doesn't belong at // this level s.currentTemperatureChanged(null, "Sensor Departed"); } } protected void shutdown(Throwable cause) throws Throwable { } public void addListener(TcpTemperatureSensor l) { listenerMap.put(l.getAddress(), l); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -