⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcptemperaturesensor.java

📁 这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package net.sf.dz.device.sensor.impl;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.Socket;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Set;import java.util.StringTokenizer;import javax.net.ssl.SSLException;import org.freehold.jukebox.conf.Configuration;import org.freehold.jukebox.logger.LogAware;import org.freehold.jukebox.logger.LogChannel;import org.freehold.jukebox.service.ActiveService;import org.freehold.jukebox.service.ServiceUnavailableException;import net.sf.dz.device.sensor.TemperatureSensor;import net.sf.dz.util.SSLContextFactory;/** * TCP temperature sensor. * * <p> * * Connects to DAC over TCP and reads temperature values, then distributes * them to listeners. * * @author Copyright &copy; <a href="mailto:vt@freehold.crocodile.org">Vadim Tkachenko</a> 2001-2004 * @version $Id: TcpTemperatureSensor.java,v 1.8 2004/06/28 20:35:49 vtt Exp $ */public class TcpTemperatureSensor extends AbstractTemperatureSensor {    /**     * Log channel to use.     *     * Properly initialized in {@link configure configure()}.     */    private LogChannel CH_TCPTS = new LogChannel("TempSensor/TCP");        /**     * Remote host to connect to.     *     * Default is localhost.     */    private String remoteHost;        /**     * Port on the remote host to connect to.     *     * Default is 5000.     */    private int remotePort;        /**     * Is connection requested to be secure.     */    private boolean secure;        /**     * Password for the key store.     *     * VT: Don't tell me it's insecure, just fix the implementation to be     * more secure, all right?     */    private String password;        /**     * Last known temperature.     *     * <p>     *     * The reason for it being an Object, not <code>java.lang.Double</code>,     * is the {@link net.sf.dz.event.TemperatureSensorListener     * TemperatureSensorListener} signature which requires an Object to be     * able to accept error condition notifications.     *     * <p>     *     * Implementation will have two possible value classes for this object:     * if everything is normal, then it is <code>java.lang.Double</code>,     * otherwise <code>java.lang.String</code> containing the error     * description.     */    private Object lastKnownTemperature = (Object)"Not Available";        /**     * The TCP listener.     *     * <p>     *     * VT: FIXME: "Worse is better" works here again.     *     * Ideally, there should be a way to independently connect each TCP     * sensor to a different host/port, but in reality, all your data will     * be coming from the same location, at least at the beginning. So,     * instead of providing separate TCP listener for each host/port, we     * just create ONE at the beginning, and the configuration values from     * the subsequent TCP sensors will be ignored. Caveat emptor.     */    private static TcpListener tcpListener = null;        protected void configure() throws Throwable {            super.configure();                String cfroot = getConfigurationRoot();        Configuration cf = getConfiguration();                        remoteHost = cf.getString(cfroot + ".tcp.host", "localhost");        remotePort = cf.getInteger(cfroot + ".tcp.port", 5000);        secure = cf.getBoolean(cfroot + ".tcp.secure");                if ( secure ) {                    password = cf.getString(cfroot + ".tcp.password");        }        CH_TCPTS = new LogChannel("TempSensor/TCP/" + remoteHost + ":" + remotePort + ":" + getAddress());                complain(LOG_INFO, CH_TCPTS, "configured from " + cfroot);    }        protected void startup() throws Throwable {            synchronized ( getClass() ) {                if ( tcpListener == null ) {                            tcpListener = new TcpListener();                tcpListener.setLogger(getLogger());                                if ( !tcpListener.start().waitFor() ) {                                    throw new ServiceUnavailableException("Couldn't start the TCP listener, messages should have been provided");                }            }        }                tcpListener.addListener(this);    }    public synchronized double getSensorTemperature() throws IOException {            checkStatus();                if ( !isActive() ) {                    complain(LOG_ALERT, CH_TCPTS, "getSensorTemperature() before started");            return 0;        }            if ( lastKnownTemperature == null ) {                    throw new IllegalStateException("lastKnownTemperature is null, this can't be happening");        }                if ( lastKnownTemperature instanceof String ) {                    throw new IllegalStateException((String)lastKnownTemperature);        }                if ( lastKnownTemperature instanceof Double ) {                    return ((Double)lastKnownTemperature).doubleValue();        }                throw new IllegalStateException("lastKnownTemperature is not null, not a String, not a Double - WTF?");    }        public void currentTemperatureChanged(TemperatureSensor source, Object currentTemperature) {            lastKnownTemperature = currentTemperature;    }        protected void shutdown(Throwable cause) throws Throwable {            // VT: FIXME: Get rid of the TcpListener when the last one closes?    }    private class TcpListener extends ActiveService {            public final LogChannel CH_LISTENER = new LogChannel(CH_ATS, "TCP listener");                private Socket socket;        private BufferedReader br;        private Map listenerMap = new HashMap();        private Set warningList = new HashSet();                protected void startup() throws Throwable {                    if ( secure ) {                            complain(LOG_NOTICE, CH_LISTENER, "Secure connection requested");                                try {                                    socket = SSLContextFactory.createContext(password).getSocketFactory().createSocket(remoteHost, remotePort);                                    } catch ( SSLException sslex ) {                                    complain(LOG_WARNING, CH_LISTENER, "Can't establish a secure connection to " + remoteHost + ":" + remotePort, sslex);                    complain(LOG_WARNING, CH_LISTENER, "Reverting to insecure connection");                }                        }                        if ( socket == null ) {                            socket = new Socket(remoteHost, remotePort);            }                        br = new BufferedReader(new InputStreamReader(socket.getInputStream()));                        if ( !secure ) {                            // In case we're talking to a secure socket implementation,                // we'll get stuck reading the input stream - they won't                // tell us anything, nor would they break away (well, maybe                // after a really long timeout). Therefore, we'll have to                // give them a kick so they kick us out and we'll get a                // visible indication of a failure.                                PrintWriter pw = new PrintWriter(socket.getOutputStream());                                pw.println("");                pw.println("");                pw.println("");                pw.println("");                pw.println("");                pw.println("");                pw.flush();                                // If they're secure, and we're not, we'll get kicked out                // right here...            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -