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

📄 netmapreader.java

📁 openmap java写的开源数字地图程序. 用applet实现,可以像google map 那样放大缩小地图.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// **********************************************************************// // <copyright>// //  BBN Technologies//  10 Moulton Street//  Cambridge, MA 02138//  (617) 873-8000// //  Copyright (C) BBNT Solutions LLC. All rights reserved.// // </copyright>// **********************************************************************// // $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/graphicLoader/netmap/NetMapReader.java,v $// $RCSfile: NetMapReader.java,v $// $Revision: 1.2.2.3 $// $Date: 2005/08/09 17:59:29 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap.graphicLoader.netmap;import java.net.*;import java.util.*;import java.io.*;import com.bbn.openmap.util.Debug;/** * The NetMapReader is the class that makes actual contact with the * NetMap server and maintains the connection to it. It is controlled * by the NetMapConnector, and is created by it as well. The * NetMapReader understands that the NetMap server maintains a notion * of views, and that to get information from the server, it has to * request information about a particular view. Luckily, a list of * views can be requested from the server as well. * <P> *  * The NetMap server will provide a text output stream of lines, with * each line representing an event. The NetMapReader has an implicit * understanding of the format of different line types, and creates a * java Properties object for each line, categorizing each field, and * putting it in the Properties object as (Field Header key, Field * value). This Properties object is sent to the NetMapConnector, * where it is distributed to the NetMapEventListeners. Each * NetMapEventListener is free to peruse the Properties object in the * event, and get information it needs from each one by asking for * specific field values. Some fields may not be present in all * Properties objects, but the fields should be consistant for the * same event type. At some point, we'll include a description of the * events and the fields that can be expected. *  * In general, there are two things you can do. First, you can create * a NetMapReader without a view, and then call getViewList() on it to * get a list of possible views. The NetMapReader will disconnect * itself after that request. Second, you can create a NetMapReader * with a specific view (or set the view later), and then call start() * on it to begin receivng events about that view. Call disconnect() * when you want it to stop. * <P> */public class NetMapReader extends Thread implements NetMapConstants {    private static Object EOF = new Object();    private static Object LP = new Object();    private static Object RP = new Object();    StreamTokenizer st = null;    boolean shutdown = false;    String viewName = null;    Socket s = null;    /**     * This is the component that is organizing the dispersal of the     * events as a result of reading stuff off the NetMap connection.     */    NetMapConnector netmapConn;    boolean DEBUG = false;    boolean DEBUG_VERBOSE = false;    /**     * Create a NetMapReader to listen to a host on a port, with a     * NetMapConnector to get back in touch with. This method just     * connects, finds out the views, and closes the connection to the     * server.     */    public NetMapReader(String host, String port, NetMapConnector connector)            throws IOException {        this(host, port, connector, null);    }    /**     * Create a NetMapReader to listen to a NetMapServer running on a     * port, and parse the stream relating information about the given     * view. If the view is null, then the NetMapReader will get a     * view list and disconnect.     */    public NetMapReader(String host, String port, NetMapConnector connector,            String view) throws IOException {        netmapConn = connector;        if (view != null) {            setView(view);        }        DEBUG = Debug.debugging("netmap");        DEBUG_VERBOSE = Debug.debugging("netmap_verbose");        try {            s = connect(host, port);        } catch (IOException e) {            throw e;        }    }    /**     * Set the view that will be requested when the reader is started,     * via reader.start().     */    public void setView(String view) {        this.viewName = view;    }    /**     * A general connection method that returns a socket for a host     * and port.     */    private Socket connect(String host, String portString) throws IOException {        int port = 0;        Socket sock = null;        boolean DEBUG = Debug.debugging("netmap");        try {            port = Integer.parseInt(portString, 10);        } catch (NumberFormatException e) {            if (DEBUG)                Debug.output("Illegal name " + host + ":" + portString);            throw new IOException("Illegal port: " + portString);        }        if (DEBUG)            Debug.output("Connecting to server " + host + ":" + port);        try {            sock = new Socket(host, port);        } catch (IOException e) {            if (sock != null)                sock.close();            if (DEBUG) {                Debug.output("Can't connect to " + host + ":" + port + "\n   "                        + e);            }            throw e;        }        return sock;    }    /**     * For an established NetMapReader, get the list of views that the     * NetMapReader knows about. The NetMapReader will disconnect     * after this query.     */    public ChoiceList getViewList(String host, String port) {        BufferedReader in = null;        PrintWriter out = null;        ChoiceList viewList = null;        Socket sock = null;        boolean DEBUG = Debug.debugging("netmap");        try {            sock = connect(host, port);            in = new BufferedReader(new InputStreamReader(sock.getInputStream()));            out = new PrintWriter(sock.getOutputStream(), true);            // Now load the views from the server            out.println(JMAP_VIEW_CMD);        } catch (IOException e) {            Debug.error("NetMapReader: " + e);            return null;        }        viewList = new ChoiceList();        try {            String line = null;            while ((line = in.readLine()) != null) {                line = line.trim();                int labelStart = line.indexOf("\'");                int labelEnd = line.lastIndexOf("\'");                String viewLabel = null;                String viewLine = null;                if (DEBUG)                    Debug.output("View definition: " + line);                if (labelStart < 0) {                    viewLine = line;                    viewLabel = line;                } else {                    viewLine = line.substring(0, labelStart) + " "                            + line.substring(labelEnd + 1);                    viewLabel = line.substring((labelStart + 1), labelEnd);                }                viewList.add(viewLabel.trim(), viewLine.trim());            }        } catch (IOException e) {            Debug.error("NetMapReader: Input error: " + e);            viewList.removeAllElements();            viewList = null;        }        try {            sock.close();            netmapConn.connectionDown();        } catch (IOException e) {        }        return viewList;    }    /**     * A queue command that lets the NetMapReader know to disconnect     * when it has the opportunity to.     */    public void shutdown() {        this.shutdown = true;        return;    }    /**     * Called when NetMapReader.start() is called. This makes the     * NetMapReader call the NetMap server to get information about     * the view that is set. This assumes that the view has been set.     * The NetMapConnector will be called back with Properties objects     * describing events received.     */    public void run() {        BufferedReader tdin = null;        PrintWriter tdout = null;        if (viewName == null) {            Debug.error("NetMapReader not given a view name to request from the NETMAP server.");            return;        }        while (!this.shutdown) {            if (DEBUG)                Debug.output("NetMapReader attemping connection");            try {                tdin = new BufferedReader(new InputStreamReader(s.getInputStream()));                tdout = new PrintWriter(s.getOutputStream(), true);                if (DEBUG)                    Debug.output("Loading view: " + viewName);                tdout.println("jmap '" + viewName + "' 768");                s.setSoTimeout(500);            } catch (InterruptedIOException eConnectInterrupted) {                continue;            } catch (IOException eConnect) {                Debug.error("NetMapReader: " + eConnect.getMessage()                        + "; NetMapReader sleeping");                try {                    Thread.sleep(40000);                } catch (Exception eSleep) {                }                continue;            }            if (netmapConn == null) {                continue;            }            netmapConn.connectionUp();            while (!this.shutdown) {                try {                    String line = null;                    if ((line = tdin.readLine()) == null)                        break;                    if (DEBUG_VERBOSE)                        Debug.output("  read: " + line);                    Properties eventProps = procline(line);                    if (DEBUG_VERBOSE)                        Debug.output("  processed...");                    if (eventProps.size() > 0) {                        netmapConn.distributeEvent(eventProps);                        if (DEBUG_VERBOSE)                            Debug.output("  distributed...");                    } else {                        if (DEBUG_VERBOSE)                            Debug.output("  ignored...");                    }                } catch (InterruptedIOException eReadInterrupted) {                    continue;                } catch (Exception e) {                    Debug.error("NetMapReader exception: " + e.getMessage()                            + "; in NetMapReader run. ");                    // Which is better?                    //                  break;                    continue;                }            }            try {                s.close();                netmapConn.connectionDown();            } catch (Exception eShutdown) {            }        }    }    /**     * Used to represent Double values read off the stream, converted     * from the tokenized vector.     */    int[] nargs = new int[60];    /** Process a line from NetMap input stream. */    protected Properties procline(String cmdline) {        Vector v = tokenize(cmdline);        // Right now, nargs[] gets set in tokenize() with values        // gleamed off Doubles in v.        if (DEBUG_VERBOSE)            Debug.output("  parsed: " + v.toString());        Properties eventProps = new Properties();        String cmd = v.firstElement().toString();        int shape = nargs[4];        if (cmd.equals(NODE_OBJECT)) {            eventProps.put(COMMAND_FIELD, NODE_OBJECT);            eventProps.put(INDEX_FIELD, Integer.toString(nargs[1]));            eventProps.put(SHAPE_FIELD, Integer.toString(nargs[4]));            if (v.elementAt(4) instanceof String) {                String icon = (String) v.elementAt(4);

⌨️ 快捷键说明

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