📄 mysend.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.send()
*/
package tutorial;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import net.jxta.j2me.PeerNetwork;
import net.jxta.j2me.Message;
import net.jxta.j2me.Element;
/**
* This example illustrates using the JXTA for JXME APIs to send and
* receive messages.
*
* This example creates an instance of
* {@link PeerNetwork PeerNetwork} and connects to the JXTA network, creates
* a pipe, and issues a search request looking for this pipe. It next polls
* for messages until it receives a response that the pipe has been found,
* and then sends a message to this pipe (essentially talking to itself).
* Finally, it polls for messages again and displays the messages it receives.
*
* The usage for running the application is:
* <pre>
* mySend [-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
* start polling for pipeID...
* got pipeID
* send request id: 1
* start polling for incoming messages...
* Message from: mySendPeer
* Message: Hello there
* </pre>
*/
public class mySend {
private static final String USAGE =
"\nUsage: mySend [-relay relay]" +
"\n" +
"options:\n" +
" -relay http relay URL\n";
private static final String PEER_NAME = "mySendPeer";
private static final String PIPE_NAME = "myPipe";
private static final String PIPE_TYPE = PeerNetwork.UNICAST_PIPE;
private PeerNetwork peer = null;
private String relayUrl = null;
private byte[] persistentState = null;
private int listenQueryId = -1;
private int sendRequestId = -1;
/**
* Constructor for the mySend object.
*
* First, create a new instance of
* {@link PeerNetwork} with the specified name and connect
* to the JXTA relay. A connection to the relay needs to be established
* before any other operations can be invoked.
* <pre>
* peerNetwork = PeerNetwork.createInstance(PEER_NAME);
* persistentState = peerNetwork.connect(relayUrl, persistentState);
* </pre>
*
* 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
* {@link net.jxta.j2me.PeerNetwork#connect connect()}
* so that the peer can be identified and it
* 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>
*
* 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. In this example, we are ignoring these responses.
* <p></p>
* Next, we have our peer search for this pipe and send it a message.
* {@link #findMyPipe} waits until it finds the pipe and then returns
* the pipe ID. The method {@link #sendMyMessage sendMymessage()}
* uses this pipeID to send the message:
* <pre>
* String pipeId = findMyPipe();
* sendMyMessage(pipeId);
* </pre>
*
* Finally, we call {@link #recvMessage} to poll for and display the
* message that was sent to this peer. Because {@link #recvMessage} loops forever
* polling for messages, this application never exits.
* <pre>
* recvMessage();
* </pre>
*/
public mySend(String relay) throws IOException {
relayUrl = relay;
System.out.println("relay url: " + relayUrl);
// Create a peer and have it connect to the relay.
// A connection to relay is required before any other
// operations can be invoked.
peer = PeerNetwork.createInstance(PEER_NAME);
persistentState = peer.connect(relayUrl, persistentState);
// Have the peer create and open a Unicast pipe; PipeID will
// be returned asynchronously in a response message
listenQueryId = peer.listen(PIPE_NAME, null, PIPE_TYPE);
System.out.println("listen query id: " + listenQueryId);
// Have peer search for this Pipe and then send it a Message
String pipeID = findMyPipe();
sendMyMessage(pipeID);
// Finally, have the peer poll for Messages sent to it
recvMessage();
}
/**
* Search for a pipe with the given name, waiting
* (possibly forever) until the pipe is found.<p></p>
*
* This method polls for messages addressed to this peer. We should
* receive messages in response to our call to listen() -- messages
* providing information about the pipe and indicating if the pipe was
* successfully created. These messages will contain an Element named
* {@link net.jxta.j2me.Message#REQUESTID_TAG} whose data matches our
* searchQueryId. <p></p>
*
* In this example, we ignore all messages except for those that match
* our searchQueryId. When we receive a matching message, we return the
* pipe id. <p></p>
*
* First, we poll for messages that were sent to this peer. This method
* takes one argument, which is the timout in milliseconds. (A timeout
* value of 0 would indicate that the poll operation should wait forever):
* <pre>
* msg = peer.poll(1);
* </pre>
*
* For each message that is received, we loop through all elements:
* <pre>
* for (int i = 0; i < msg.getElementCount(); i++) {
* Element e = msg.getElement(i);
* </pre>
*
* We are expecting a message in response to our search request. This message
* will contain elements in the {@link net.jxta.j2me.Message#PROXY_NAME_SPACE}
* with the following names:
* <ul>
* <li>{@link net.jxta.j2me.Message#REQUESTID_TAG} -- The request ID; we're
* looking for one that matches our searchRequestId.
* <li>{@link net.jxta.j2me.Message#TYPE_TAG} -- Type of JXTA component; can be
* {@link net.jxta.j2me.PeerNetwork#PIPE}, {@link net.jxta.j2me.PeerNetwork#PEER},
* or {@link net.jxta.j2me.PeerNetwork#GROUP}. We're expecting
* {@link net.jxta.j2me.PeerNetwork#PIPE}.
* <li>{@link net.jxta.j2me.Message#NAME_TAG} -- The name of the JXTA component;
* we're expecting our PIPE_NAME.
* <li>{@link net.jxta.j2me.Message#ARG_TAG} -- Argument; in this example, we're
* expecting the type of our pipe, PIPE_TYPE.
* <li>{@link net.jxta.j2me.Message#ID_TAG} -- The JXTA ID; in this example,
* we're expecting the JXTA pipe ID.
* </ul>
* <p></p>
* We first check if this element is using the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -