📄 peernetwork.java
字号:
/************************************************************************
*
* $Id: PeerNetwork.java,v 1.22 2002/07/10 18:48:25 akhil Exp $
*
* Copyright (c) 2001 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.
**********************************************************************/
package net.jxta.j2me;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;
/**
* This class is an abstraction for the JXTA Network. It specifies the
* operations that an application can invoke on the JXTA network.
*/
public final class PeerNetwork {
/**
* The group, when not specified, defaults to the
* <code>NetPeerGroup</code>.
*/
public static final String DEFAULT_GROUP = "urn:jxta:jxta-NetGroup";
/**
* The JXTA Unicast pipe. Use for one-to-one communications with a
* peer.
*/
public static final String UNICAST_PIPE = "JxtaUnicast";
/**
* The JXTA Propagte pipe. Messages sent to this pipe are
* propagated to the entire group.
*/
public static final String PROPAGATE_PIPE = "JxtaPropagate";
/**
* Create or Search for a Peer.
*/
public static final String PEER = "PEER";
/**
* Create or Search for a Group.
*/
public static final String GROUP = "GROUP";
/**
* Create or Search for a Pipe.
*/
public static final String PIPE = "PIPE";
private static final String PROXY_SERVICE_NAME =
"urn:jxta:uuid-DEADBEEFDEAFBABAFEEDBABE0000000E05";
private Vector sendMessageQueue = new Vector();
/**
* The default constructor is private. Use the createInstance
* factory method instead.
*/
private PeerNetwork() {
}
/**
* Connect to a relay. A connection to a relay needs to be
* established before any of the other operations can be invoked.
*
* @param url relay URL
*
* @param state a byte array that represents the persistent state
* of a connection to the PeerNetwork. Initially, this would be
* null. A successful {@link #connect} returns this state
* information which the application is expected to persist and
* pass it back to connect, if available.
*
* @return see the description for the state parameter
*
* @throws IOException if the relay is not accesible
*/
public byte[] connect(String url, byte[] state)
throws IOException {
relayUrl = url;
String persistedPeerId = null;
if (state != null && state.length > 0) {
persistedPeerId = new String(state);
}
peerId = messenger.connect(url, persistedPeerId);
if (peerId == null) {
throw new IOException("Connect did not return a peerId");
}
return peerId.getBytes();
}
/**
* Search for Peers, Groups or Pipes.
*
* @param type one of
* {@link #PEER}, {@link #GROUP} or {@link #PIPE}
*
* @param query an expression specifying the items being searched
* for and also limiting the scope of items to be returned. This
* is usually a simple regular expression such as, for example,
* <code>TicTacToe*</code> to search for all entities with names
* that begin with TicTacToe.
*
* @return query id that can be used to match responses
*
* @throws IOException if a communication error occurs with the
* relay or with the JXTA network
*/
public int search(String type, String query)
throws IOException {
String attr = "Name";
int requestId = getNextRequestId();
Element[] elm = new Element[5];
elm[0] = new Element(Message.REQUEST_TAG,
Message.REQUEST_SEARCH.getBytes(),
Message.PROXY_NAME_SPACE, null);
elm[1] = new Element(Message.TYPE_TAG,
type.getBytes(),
Message.PROXY_NAME_SPACE, null);
elm[2] = new Element(Message.ATTRIBUTE_TAG,
attr.getBytes(),
Message.PROXY_NAME_SPACE, null);
elm[3] = new Element(Message.VALUE_TAG,
query.getBytes(),
Message.PROXY_NAME_SPACE, null);
elm[4] = new Element(Message.REQUESTID_TAG,
Integer.toString(requestId).getBytes(),
Message.PROXY_NAME_SPACE, null);
sendMessage(elm);
return requestId;
}
/**
* Create a
* {@link #PEER}, {@link #GROUP} or {@link #PIPE}
*
* @param type one of
* {@link #PEER}, {@link #GROUP} or {@link #PIPE}
*
* @param name the name of the entity being created.
*
* @param arg an optional arg depending upon the type of entity
* being created. For example, for {@link #PIPE}, this would be
* the type of {@link #PIPE} that is to be created. For example,
* <code>JxtaUniCast</code> and <code>JxtaPropagate</code> are
* commonly-used values. This parameter can be <code>null</code>.
*
* @return query id that can be used to match responses.
*
* @throws IOException if a communication error occurs with the
* relay or with the JXTA network
*/
public int create(String type, String name, String arg)
throws IOException {
int requestId = getNextRequestId();
Element[] elm = null;
if (arg != null) {
elm = new Element[5];
} else {
elm = new Element[4];
}
elm[0] = new Element(Message.REQUEST_TAG,
Message.REQUEST_CREATE.getBytes(),
Message.PROXY_NAME_SPACE, null);
elm[1] = new Element(Message.NAME_TAG,
name.getBytes(),
Message.PROXY_NAME_SPACE, null);
elm[2] = new Element(Message.REQUESTID_TAG,
Integer.toString(requestId).getBytes(),
Message.PROXY_NAME_SPACE, null);
elm[3] = new Element(Message.TYPE_TAG,
type.getBytes(),
Message.PROXY_NAME_SPACE, null);
if (arg != null) {
elm[4] = new Element(Message.ARG_TAG,
arg.getBytes(),
Message.PROXY_NAME_SPACE, null);
}
sendMessage(elm);
return requestId;
}
/**
* Open a Pipe for input.
*
* @param name the name of the Pipe
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -