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

📄 mysearch.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * can receive any messages that have been queued in its absence.
     *
     * Next, we create and open a Unicast pipe named after the peer:
     *
     * <pre>
     *     listenQueryId = peerNeetwork.listen(PIPE_NAME, null, PIPE_TYPE);
     * </pre> 
     *
     * The call to <code>listen()</code> takes three arguments:
     * <ul>
     * <li> String name - Name of the pipe
     * <li> String id - Pipe ID
     * <li> String type - Type of pipe (
     * {@link net.jxta.j2me.PeerNetwork#UNICAST_PIPE PeerNetwork.UNICAST_PIPE} or
     * {@link net.jxta.j2me.PeerNetwork#PROPAGATE_PIPE PeerNetwork.PROPAGATE_PIPE})
     * </ul>
     *
     * <p></p>
     * Because we pass a null Pipe ID, a new pipe will be created for
     * us. The Pipe ID for this new pipe will be returned asynchronously
     * in a response message. We will also receive an asynchronous
     * response indicating the status of this call to
     * <code>listen()</code> -- whether it completed successfully or an
     * error occurred.  This response message will contain an element with
     * name "requestID" which matches listenQueryId. A separate Element in
     * this Message with name "response" will contain one of the
     * following:
     * {@link net.jxta.j2me.Message#RESPONSE_SUCCESS success}, 
     * {@link net.jxta.j2me.Message#RESPONSE_ERROR error},
     * {@link net.jxta.j2me.Message#RESPONSE_INFO info},
     * {@link net.jxta.j2me.Message#RESPONSE_RESULT result}, or 
     * {@link net.jxta.j2me.Message#RESPONSE_MESSAGE message}. For
     * example, if the call to <code>listen()</code> successfully creates
     * the Pipe, we would receive a message with elements similar to the
     * following:
     *
     * <p></p>
     * <table border="0" bgcolor="#e0e0ff">
     * <tr>
     *   <th>Element Index</th>
     *   <th>Name Space</th>
     *   <th>Name</th>
     *   <th>Data</th>
     * </tr>
     * <tr>
     *   <td>0</td>
     *   <td>proxy</td>
     *   <td>response</td>
     *   <td>success</td>
     * </tr>
     * <tr>
     *   <td>1</td>
     *   <td>proxy</td>
     *   <td>requestID</td>
     *   <td>5</td>
     * </tr>
     * <tr>
     *   <td colspan=4><i>(additional elements deleted for brevity)</i></td>
     * </tr>
     * </table>
     * <p></p>
     *
     * We then send a search request to the JXTA Proxy looking for the
     * pipe we just created:
     *
     * <pre>
     *    pipeSearchQueryId = peerNetwork.search(PeerNetwork.PIPE, PIPE_NAME); 
     * </pre>
     *
     * We pass two arguments to <code>search()</code>:
     *
     * <ul>
     *
     * <li>String type - The type of item to search for; this can be
     * {@link net.jxta.j2me.PeerNetwork#PEER PeerNetwork.PEER},
     * {@link net.jxta.j2me.PeerNetwork#GROUP PeerNetwork.GROUP}, or
     * {@link net.jxta.j2me.PeerNetwork#PIPE PeerNetwork.PIPE}.
     *
     * <li>String query - An expression specifying the items to search for
     *
     * </ul>
     *
     * In this example, we are searching for the newly created pipe. We
     * will receive asynchronous responses from the JXTA Proxy Service
     * when (and if) it locates the entities for which we're searching.
     *
     * Finally, we loop forever and poll for responses that were sent to
     * this peer:
     *
     * <pre>
     *    Message m = peerNetwork.poll(1);
     * </pre>
     *
     * This method takes one argument, which is the timeout in
     * milliseconds. (A timeout value of 0 would indicate that the poll
     * operation should wait forever.) <p> For each message that is
     * received, we loop through all elements in this message and print
     * their contents. We should expect to see status and informational
     * Messages in response to our call to <code>listen()</code>, as well
     * as one or more messages containing the results of our search query.
     * 
     * @param  relay     URL of the relay host
     * @throws IOException
     */

    public mySearch(String relay) throws IOException {
        this.relayUrl = relay;

        System.out.println("relay url: " + relayUrl);
        
        peerNetwork = PeerNetwork.createInstance(PEER_NAME);
        // Create a peer and have it connect to the relay.
        // A connection to relay is required before any other
        // operations can be invoked.
        persistentState = peerNetwork.connect(relayUrl, persistentState);
            
        // Create and open a Unicast pipe named after the peer; PipeID will
        // be returned asynchronously in a response message
        listenQueryId = peerNetwork.listen(PIPE_NAME, null, PIPE_TYPE);
        System.out.println("listen query id: " + listenQueryId);
            
        // Send a search request looking for the pipe
        pipeSearchQueryId = peerNetwork.search(PeerNetwork.PIPE, PIPE_NAME);
        System.out.println("pipe search query id: " + pipeSearchQueryId);

        System.out.println("start polling...");

        // Now poll for all messages addressed to us.  We should
        // receive messages containing the status and responses to
        // peerNetwork's listen() and search() requests
        while (true) {

            // do not use a timeout of 0, 0 means block forever
            Message m = peerNetwork.poll(1);
            if (m != null) {

                // display details of each element
                for (int i = 0; i < m.getElementCount(); i++ ) {
                    Element e = m.getElement(i);
                    System.out.println(i + " " + 
                                       e.toString() + " " + 
                                       "\"" + new String(e.getData()) + "\"");
                }
                System.out.println();
            }
        }
    }

    /** 
     * Gets the relay URL and calls mySearch().
     * 
     * First parses the command line to see if the <code>-relay</code>
     * option was specified. If the relay was not specified, tries to
     * use the IP address of the localhost. <p>
     * 
     * If we get a relay URL, either from the command line or by using
     * the localhost IP address, we pass this URL to the call to
     * create a new mySearch object. This constructor performs all the
     * interesting work in this example: creating the JXTA components,
     * connecting to the JXTA network, and performing the search
     * operation.
     */
    public static void main(String args[]) {
        String relay = null;
        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("-relay") && i+1 < args.length) {
                relay = args[++i];
            } else if (args[i].equals("-help")) {
                System.err.println(USAGE);
            } else {
                System.err.println("Error parsing arguments" + USAGE);
                return;
            }
        }

        // Relay URL was not specified on the command-line, perhaps it
        // is running locally?
        if (relay == null) {
            // get the ip address of localhost
            try {
                InetAddress localhost = InetAddress.getLocalHost();
                if (localhost != null) {
                    relay = "http://" + localhost.getHostAddress() + ":9700";
                }
            } catch (UnknownHostException ex) {
                System.err.println(ex);
                System.err.println("Cannot determine IP address of localhost: " + 
                                   "Must use the -relay option" + USAGE);
                return;
            }
        }

        try {
            mySearch test = new mySearch(relay);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

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