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

📄 remoteprinthandler.java

📁 无线传感器网络节点Sun SPOT管理工具
💻 JAVA
字号:
/* * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.  *  * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product that is * described in this document. In particular, and without limitation, these intellectual property rights may * include one or more of the U.S. patents listed at http://www.sun.com/patents and one or more additional patents * or pending patent applications in the U.S. and in other countries. *  * U.S. Government Rights - Commercial software. Government users are subject to the Sun Microsystems, Inc. * standard license agreement and applicable provisions of the FAR and its supplements. *  * Use is subject to license terms.  *  * This distribution may include materials developed by third parties. Sun, Sun Microsystems, the Sun logo and * Java are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.  *  * Copyright (c) 2006 Sun Microsystems, Inc. Tous droits r?serv?s. *  * Sun Microsystems, Inc. d?tient les droits de propri?t? intellectuels relatifs ? la technologie incorpor?e dans * le produit qui est d?crit dans ce document. En particulier, et ce sans limitation, ces droits de propri?t? * intellectuelle peuvent inclure un ou plus des brevets am?ricains list?s ? l'adresse http://www.sun.com/patents * et un ou les brevets suppl?mentaires ou les applications de brevet en attente aux Etats - Unis et dans les * autres pays. *  * L'utilisation est soumise aux termes du contrat de licence. *  * Cette distribution peut comprendre des composants d?velopp?s par des tierces parties. * Sun, Sun Microsystems, le logo Sun et Java sont des marques de fabrique ou des marques d?pos?es de Sun * Microsystems, Inc. aux Etats-Unis et dans d'autres pays. */package org.sunspotworld.remoteprinting;import java.io.*;import javax.microedition.io.*;import java.util.Calendar;import com.sun.spot.util.IEEEAddress;import com.sun.spot.util.Utils;/** * An internal auxiliary class used by RemotePrintServer. Each remote printing client * has an associate RemotePrintHandler thread that listens for remote printing from * the client and when a full line is received it passes it to the local PrintStream * for display by the server. Each line is prefixed with a timestamp and the radio * address of the client. * <p> * You should NOT normally instantiate or call this class directly. * * @author Ron Goldman * @see org.sunspotworld.remoteprinting.RemotePrintServer */public class RemotePrintHandler extends Thread {        private final static int BUFFER_SIZE = 256;        private InputStream in = null;    private boolean running = true;    private String macAddress;    private int portNo;    private String key;    private PrintStream out;    private boolean showingTimeStamps = false;        /**     * Creates a new instance of RemotePrintHandler.     *     * @param address the radio address of the remote print client     * @param port the port to use for remote printing (0-127)     * @param ps the PrintStream to use to display remote printing     */    public RemotePrintHandler(String address, int port, PrintStream ps) {        macAddress = address;        portNo = port;        out = ps;        key = macAddress + ":" + Integer.toString(getPort());        try {            in = Connector.openInputStream("radiostream://" + key);        } catch (IOException ex) {            System.out.println("Error opening remote printing connection: " + key + " : " + ex.toString());        }    }        /**     * Return the status of the RemotePrintHandler thread.     *     * @return true if the remote printing connection is still open and active     */    public boolean isRunning () {        return running & isAlive();    }         /**     * Cause the RemotePrintHandler thread to stop.     *     */    public void quit () {        running = false;        if (in != null) {            try {                in.close();                in = null;            } catch (IOException ex) {                // just ignore            }        }    }        /**     * Simple way to specify a fixed width display for 2-3 digit numbers.     * Leading zeros are printed if necessary.     *     * @param out the PrintStream to write to     * @param num the number to write out     * @param digits how many characters to write     */    private static void printNumAux (PrintStream out, int num, int digits) {        if (digits >= 3 & num < 100) out.print('0');        if (digits >= 2 & num < 10)  out.print('0');        out.print(num);    }    /**     * Auxiliary function to print a time stamp and identifying SPOT address.     *     * @param out the PrintStream to use     * @param prefix the SPOT IEEE radio address     */    public static void printTimeStamp (PrintStream out, String prefix) {        Calendar c = Calendar.getInstance();        printNumAux(out, c.get(Calendar.HOUR_OF_DAY), 2);        out.print(':');        printNumAux(out, c.get(Calendar.MINUTE), 2);        out.print(':');        printNumAux(out, c.get(Calendar.SECOND), 2);        out.print('.');        printNumAux(out, c.get(Calendar.MILLISECOND), 3);        out.print(' ');        out.print(prefix);        out.print(' ');    }    /**     * Internal loop to receive lines from the remote print client and display them.     * Not called directly but via Thread.start().     */    public void run () {        try {            byte buffer[] = new byte[BUFFER_SIZE];            IEEEAddress addr = new IEEEAddress (macAddress);            String prefix = "[" + addr.asDottedHex() + "]";            int len = 0;            while (running) {                int ch = in.read();                if (ch == -1) {                    if(showingTimeStamps) printTimeStamp(out, prefix);                    out.println("Remote printing channel closed " + key);                    break;                } else if (ch == '\n') {                    if(showingTimeStamps) printTimeStamp(out, prefix);                    out.write(buffer, 0, len);                    out.println();                    len = 0;                } else {                    buffer[len++] = (byte)ch;                    if (len >= BUFFER_SIZE) {                        if(showingTimeStamps) printTimeStamp(out, prefix);                        out.write(buffer, 0, len);                        out.println("  [cont...]");                        len = 0;                                        }                }            }        } catch (IOException ex) {            if (running) {                out.println("Error reading remote printing: " + key + " : " + ex.toString());            }        } finally {            Utils.sleep(1000);            if (in != null) {                try {                    in.close();                    in = null;                } catch (IOException ex) { /* ignore */ }            }        }    }    public int getPort() {        return portNo;    }    public boolean isShowingTimeStamps() {        return showingTimeStamps;    }    public void setShowingTimeStamps(boolean showingTimeStamps) {        this.showingTimeStamps = showingTimeStamps;    }}

⌨️ 快捷键说明

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