📄 mysearch.java
字号:
/************************************************************************
*
* Copyright (c) 2002 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Sun Microsystems, Inc. for Project JXTA."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
* must not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact Project JXTA at http://www.jxta.org.
*
* 5. Products derived from this software may not be called "JXTA",
* nor may "JXTA" appear in their name, without prior written
* permission of Sun.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of Project JXTA. For more
* information on Project JXTA, please see
* <http://www.jxta.org/>.
*
* This license is based on the BSD license adopted by the Apache
* Foundation.
**********************************************************************/
/**
* Sample code illustrating PeerNetwork.search()
*/
package tutorial;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.IOException;
import net.jxta.j2me.PeerNetwork;
import net.jxta.j2me.Message;
import net.jxta.j2me.Element;
/**
* This example illustrates how to use the JXTA for J2ME API to search
* for Pipes on the JXTA network.
*
* It creates an instance of
* {@link PeerNetwork PeerNetwork}, connects to the JXTA
* network, creates a pipe and issues a search request looking for
* this pipe, and then polls for responses to this search request.<p>
*
* The usage for running the application is:
* <pre>
* mySearch [-relay <i>relay</i>]
* </pre>
*
* where <tt>relay</tt> is the URL of the JXTA for J2ME relay hosts
* (for example, <tt>http://209.25.154.233:9700</tt>). If no relay is
* specified on the command line, the application attempts to use the
* localhost as the relay.
*
* Sample output when this application is run:
* <pre>
* relay url: http://209.25.154.233:9700
* listen query id: 0
* pipe search query id: 1
* start polling...
* </pre>
* <a name="response-0"><i>response-0:</i></a>
* <pre>
* 0 "proxy:response" "application/octet-stream" dlen=4 "info"
* 1 "proxy:requestId" "application/octet-stream" dlen=1 "0"
* 2 "proxy:type" "application/octet-stream" dlen=4 "PIPE"
* 3 "proxy:name" "application/octet-stream" dlen=12 "mySearchPipe"
* 4 "proxy:id" "application/octet-stream" dlen=80
* "urn:jxta:uuid-59616261646162614E50472050325033DE91EDF073014BB19BBDA4A5D344215B04"
* 5 "proxy:arg" "application/octet-stream" dlen=11 "JxtaUnicast"
* 6 "jxta:EndpointDestinationAddress" "application/octet-stream" dlen=93
* http://JxtaHttpClientuuid-59616261646162614A787461503250330F
* 3E7ED95E1A48C0AE86B13D867C658E03/"
* 7 "jxta:EndpointSourceAddress" "application/octet-stream" dlen=27 *
* "http://209.25.154.233:9700/"
* </pre>
* <a name="response-1"><i>response-1:</i></a>
* <pre>
* 0 "proxy:response" "application/octet-stream" dlen=7 "success"
* 1 "proxy:requestId" "application/octet-stream" dlen=1 "0"
* 2 "jxta:EndpointDestinationAddress" "application/octet-stream" dlen=93
* "http://JxtaHttpClientuuid-59616261646162614A787461503250330F3E7ED95E
* 1A48C0AE86B13D867C658E0 3/"
* 3 "jxta:EndpointSourceAddress" "application/octet-stream" dlen=27
* "http://209.25.154.233:9700/"
* </pre>
* <a name="response-2"><i>response-2:</i></a>
* <pre>
* 0 "proxy:response" "application/octet-stream" dlen=6 "result"
* 1 "proxy:requestId" "application/octet-stream" dlen=1 "1"
* 2 "proxy:type" "application/octet-stream" dlen=4 "PIPE"
* 3 "proxy:name" "application/octet-stream" dlen=12 "mySearchPipe"
* 4 "proxy:id" "application/octet-stream" dlen=80
* "urn:jxta:uuid-59616261646162614E50472050325033DE91EDF073014BB19BBDA4A5D344215B04"
* 5 "proxy:arg" "application/octet-stream" dlen=11 "JxtaUnicast"
* 6 "jxta:EndpointDestinationAddress" "application/octet-stream" dlen=93
* "http://JxtaHttpClientuuid-59616261646162614A787461503250330F3E7ED95E1A48C0AE86B13D867C658E03/"
* 7 "jxta:EndpointSourceAddress" "application/octet-stream" dlen=27
* "http://209.25.154.233:9700/"
* </pre>
*
* In this example, we received three messages:
*
* <ol>
*
* <li><a href="#response-0">response-0</a> is in response to
* <code>requestID==0</code>, the <code>listen()</code> request. The
* first five elements in this message use the proxy name space and
* return information about the pipe that was created (name, id, type,
* etc.). The last two elements were appended by JXTA and are used for
* routing; these elements use the JXTA name space.</li>
*
* <li><a href="#response-1">response-1</a> is also in response to
* <code>requestID==0</code>, the <code>listen()</code> request. The
* first element returns
* {@link net.jxta.j2me.Message#RESPONSE_SUCCESS success},
* indicating that the pipe was successfully created.</li>
*
* <li><a href="#response-2">response-2</a> is in response to
* <code>requestID==1</code>, the <code>search()</code> request. The
* first element returns
* {@link net.jxta.j2me.Message#RESPONSE_RESULT result},
* indicating that this message
* contains the results of the search request. Elements 2-5 contain
* information about the pipe that was found (name, id, type, etc.)
* </li>
*
* </ol>
*/
public final class mySearch {
private static final String USAGE =
"\nUsage: mySearch [-relay relay]" +
"\n" +
"options:\n" +
" -relay http relay URL\n";
private static final String PEER_NAME = "mySearch";
private static final String PIPE_NAME = "mySearchPipe";
private static final String PIPE_TYPE = PeerNetwork.UNICAST_PIPE;
private PeerNetwork peerNetwork = null;
private String relayUrl = null;
private byte[] persistentState = null;
private int listenQueryId = 0;
private int pipeSearchQueryId = 0;
/**
* Constructor for the mySearch object.
*
* First, create a new Peer with the specified name. Although names do
* not need to be unique, it is better for MIDP clients if they are.
*
* <pre>
* peerNetwork = PeerNetwork.createInstance(PEER_NAME)
* </pre>
*
* Then, connect to the JXTA relay using:
* <pre>
* persistentState = peerNetwork.connect(relayUrl, persistentState);
* </pre>
*
* A connection to a relay needs to be established before any other
* operations can be invoked. In this example, we do not use
* persistentState again. However, in a real application, this value
* should be persisted and passed back on subsequent calls to
* <code>connect()</code> so that the peer can be identified and it
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -