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

📄 peernetwork.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @param id the id of the Pipe. As a convenience feature, the id
     * can be <code>null</code>. If <code>null</code>, the JXTA Proxy
     * will create an id for the Pipe and return it asynchronously in
     * a response message.
     *
     * <p>
     *
     * An application is expected to use the returned pipe id in
     * future sessions, otherwise the application may end-up listening
     * multiple times to the same pipe, resulting in duplicate
     * messages.
     *
     * @param type the type of the Pipe. <code>JxtaUnicast</code> and
     * <code>JxtaPropagate</code> are two commonly-used values, for
     * example.
     *
     * @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 listen(String name, String id, String type) 
        throws IOException {

        return pipeOperation(Message.REQUEST_LISTEN, name, id, type, null);
    }

    /**
     * Close an input Pipe.
     *
     * @param name the name of the Pipe
     *
     * @param id the id of the Pipe. Can be <code>null</code>.
     *
     * @param type the type of the Pipe. <code>JxtaUnicast</code> and
     * <code>JxtaPropagate</code> are two commonly-used values, for
     * example.
     *
     * @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 close(String name, String id, String type) 
        throws IOException {

        return pipeOperation(Message.REQUEST_CLOSE, name, id, type, null);
    }

    /**
     * Send data to the specified Pipe.
     *
     * @param name the name of the Pipe to which the specified 
     * {@link Message} is to be sent.
     *
     * @param id the peer or pipe id to which data is to be sent. 
     *
     * @param type the type of the Pipe. <code>JxtaUnicast</code> and
     * <code>JxtaPropagate</code> are two commonly-used values, for
     * example.
     *
     * @param data a {@link Message} containing an array of 
     * {@link Element}s which contain application data that is to 
     * be sent
     *
     * @return query id that can be used to match responses, if any
     *
     * @throws IOException if there is a problem in sending
     */
    public int send(String name, String id, String type, Message data) 
        throws IOException {

        return pipeOperation(Message.REQUEST_SEND, name, id, type, data);
    }

    private int pipeOperation(String op, String name, 
                              String id, String type, Message data) 
        throws IOException {

        int requestId = getNextRequestId();

        int numberElements = 2;
        if (name != null) {
            numberElements++;
        }
        if (id != null) {
            numberElements++;
        }
        if (type != null) {
            numberElements++;
        }
        if (data != null) {
            numberElements += data.getElementCount();
        }

        Element[] elm = new Element[numberElements];
        elm[0] = new Element(Message.REQUEST_TAG, 
                             op.getBytes(), 
                             Message.PROXY_NAME_SPACE, null);
        elm[1] = new Element(Message.REQUESTID_TAG, 
                             Integer.toString(requestId).getBytes(), 
                             Message.PROXY_NAME_SPACE, null);

        int index = 2;
        if (name != null) {
            elm[index++] = new Element(Message.NAME_TAG, 
                                       name.getBytes(), 
                                       Message.PROXY_NAME_SPACE, null);
        }
        if (id != null) {
            elm[index++] = new Element(Message.ID_TAG, 
                                       id.getBytes(), 
                                       Message.PROXY_NAME_SPACE, null);
        }
        if (type != null) {
            elm[index++] = new Element(Message.ARG_TAG, 
                                       type.getBytes(), 
                                       Message.PROXY_NAME_SPACE, null);
        }

        if (data != null) {
            for (int i=0; i < data.getElementCount(); i++) {
                elm[index++] = data.getElement(i);
            }
        }

        sendMessage(elm);
        return requestId;
    }

    /**
     * Poll the relay for messages addressed to this Peer.
     *
     * <p>For optimum performance, it is <em>highly</em> recommended
     * that this method be called repeatedly until it returns
     * <code>null</code>, draining all queued messages before sending
     * out any new messages.</p>
     *
     * @param int timeout time in milliseconds to wait for the
     * response. A timeout of <code>0</code> means wait forever.
     *
     * @return a {@link Message} containing an array of 
     * {@link Element}s containing incoming data. Will return a
     * <code>null</code> if there are no incoming {@link Message}s.
     *
     * @throws IOException if there is a problem in communicating with
     * the relay
     */
    public Message poll(int timeout) 
        throws IOException {

        /* Send an empty message when there are no messages to
           send. This helps maintain persistent connections to the
           relay */
        Message outgoing = Message.EMPTY;

        // if there is a queued message, send it first
        if (!sendMessageQueue.isEmpty()) {
            outgoing = (Message) sendMessageQueue.elementAt(0);
            sendMessageQueue.removeElementAt(0);
        }

	// this is a good time to GC... we will be stuck for a while
	// on IO and we may need room if we have a large incoming
	// message
	System.gc();

        return messenger.poll(timeout, outgoing);
    }

    /**
     * Factory method, used to create an instance of a PeerNetwork. 
     * 
     * @param peername a name that the user would like to give to
     * this Peer. It need not be unique, but it is better for MIDP
     * clients if it is so.
     *
     * @return an instance of PeerNetwork. 
     */
    public static PeerNetwork createInstance(String peername) {

        return new PeerNetwork(peername, DEFAULT_GROUP);
    }

    /*
     * Implementation starts here
     */

    private static int nextRequestId = -1;

    private String peername = null;
    private String groupname = null;
    private HttpMessenger messenger = null;
    private String relayUrl = null;
    private String peerId = null;

    private PeerNetwork(String peername, String groupname) {
        if (peername == null) {
            throw new IllegalArgumentException("Peer name must be specified");
        }

        if (groupname == null) {
            throw new IllegalArgumentException("Group name must be specified");
        }

        this.peername = peername;
        this.groupname = groupname;
        messenger = new HttpMessenger();
    }

    private void sendMessage(Element[] elm)
        throws IOException {

        if (peerId == null) {
            throw new IOException("Must connect before sending a message");
        }

        String proxyAddr = 
            relayUrl + "/" + PROXY_SERVICE_NAME + "/" + groupname;
        Element destAddrElem = 
            new Element("EndpointDestinationAddress", 
                        proxyAddr.getBytes(), "jxta", null);

        String sourceAddr = "http://JxtaHttpClient" + peerId + "/";
        Element srcAddrElem = 
            new Element("EndpointSourceAddress", 
                        sourceAddr.getBytes(), "jxta", null);

        // we will add two elements to the end of the message
        Element[] elm2 = new Element[elm.length + 2];
        for (int i=0; i < elm.length; i++) {
            elm2[i] = elm[i];
        }
        
        // add the src and dest address elements
        elm2[elm.length] = destAddrElem;
        elm2[elm.length + 1] = srcAddrElem;

        Message msg = new Message(elm2);
        sendMessageQueue.addElement(msg);
    }

    private static synchronized int getNextRequestId() {
        if (++nextRequestId < 0) {
            nextRequestId = 0;
        }

        return nextRequestId;
    }
}

⌨️ 快捷键说明

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