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

📄 mysend.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * {@link net.jxta.j2me.Message#PROXY_NAME_SPACE}:
     * <pre>
     *     if (Message.PROXY_NAME_SPACE.equals(e.getNameSpace()))
     * </pre>
     *
     * If it is, we get the name of this element:
     * <pre>
     *     String elementName = e.getName();
     * </pre>
     * And we check if it matches any of our expected tags. For example, if the
     * name matches {@link net.jxta.j2me.Message#ID_TAG}, we save the data in the
     * variable id:
     * <pre>
     *     else if (Message.ID_TAG.equals(elementName)) {
     *          id = new String(e.getData());
     * </pre>
     * When processing the element with the name
     * {@link net.jxta.j2me.Message#REQUESTID_TAG}, we first save the element data as
     * a String, and then use Integer.parseInt() to generate the integer value.
     * <p></p>
     * When we finish processing all elements in this message, we check if it's a
     * match. If it is, we return the pipe ID; otherwise, we continue polling for
     * messages until we find one that matches.
     * <p></p>
     * @throws IOException
     */
    public String findMyPipe() 
        throws IOException {

        System.out.println("start polling for pipeId...");
        
        // Now poll for all messages addressed to us. Stop when we
        // find the response to our listen command
        int rid = -1;
        String id = null;
        String type = null;
        String name = null;
        String arg = null;
        String response = null;

        Message msg = null;
        while (true) {
            
            // do not use a timeout of 0, 0 means block forever
            msg = peer.poll(1);
            if (msg == null) {
                continue;
            }
                
            // look for a response to our search query
            for (int i = 0; i < msg.getElementCount(); i++ ) {
                Element e = msg.getElement(i);
                if (Message.PROXY_NAME_SPACE.equals(e.getNameSpace())) {

                    String elementName = e.getName();
                    if (Message.REQUESTID_TAG.equals(elementName)) {
                        String rids = new String(e.getData());
                        try {
                            rid = Integer.parseInt(rids);
                        } catch (NumberFormatException nfx) {
                            System.err.println("Recvd invalid " +
                                               Message.REQUESTID_TAG +
                                               ": " + rids);
                            continue;
                        }
                    } else if (Message.TYPE_TAG.equals(elementName)) {
                        type = new String(e.getData());
                    } else if (Message.NAME_TAG.equals(elementName)) {
                        name = new String(e.getData());
                    } else if (Message.ARG_TAG.equals(elementName)) {
                        arg = new String(e.getData());
                    } else if (Message.ID_TAG.equals(elementName)) {
                        id = new String(e.getData());
                    } else if (Message.RESPONSE_TAG.equals(elementName)) {
                        response = new String(e.getData());
                    }
                }
            }

            if (rid == listenQueryId &&
                response.equals("success") &&
                type.equals("PIPE") &&
                name.equals(PIPE_NAME) &&
                arg.equals(PIPE_TYPE)) {
                System.out.println("got pipeId");
                return id;
            }
        }
    }
    
    /**
     * Sends a "Hello there" message to the specified Pipe. <p></p>
     *
     * This method first creates an array of two elements. The first is named
     * "mySend:Name" and contains the name of the sending peer. The second
     * is named "mySend:Message" and contains the text message we're sending:
     * <pre>
     *     Element[] elm = new Element[2];
     *     elm[0] = new Element("mySend:Name", PEER_NAME.getBytes(), null, null);
     *     elm[1] = new Element("mySend:Message", "Hello there".getBytes(),
     *                          null, null);
     * </pre>
     *
     * Each call to the constructor
     * {@link net.jxta.j2me.Element#Element Element} takes four arguments:
     * <ul>
     * <li><code>String name</code> -- the name of the element
     * <li><code>byte[] data</code> -- the data that this element carries
     * <li><code>String nameSpace</code> -- the name space used by this element
     * <li><code>String mimeType</code> -- the mimeType of the data (if null, the default
     * MIME type of "application/octet-stream" is assumed.
     * </ul>
     * <p>
     * Next, a message is constructed using these elements, and is sent using
     * {@link net.jxta.j2me.PeerNetwork#send PeerNetwork.send()}:
     * <pre>
     *     Message msg = new Message(elm);
     *     sendRequestId = peer.send(PIPE_NAME, pipeID, PIPE_TYPE, msg);
     * </pre>
     * </p>
     * The call to {@link net.jxta.j2me.PeerNetwork#send PeerNetwork.send()}
     * takes four arguments:
     * <ul>
     * <li><code>String name</code> -- the name of the pipe to which the message
     * is sent
     * <li><code>String id</code> --  the pipe ID to which the message is sent
     * <li><code>String type</code> -- the type of pipe (
     * {@link net.jxta.j2me.PeerNetwork#UNICAST_PIPE} or 
     * {@link net.jxta.j2me.PeerNetwork#PROPAGATE_PIPE}.
     * <li><code>Message data</code> -- The message containing an array of elements
     * </ul>
     * <p> </p>
     * @param pipeID The JXTA pipe ID to which we send the message
     * @throws IOException
     */
    public void sendMyMessage(String pipeID) 
        throws IOException {

        Element[] elm = new Element[2];
        
        // Our Message will contain two elements. Receiver should look for
        // elements with the same names ("mySend:Name" and "mySend:Message")
        elm[0] = new Element("mySend:Name", PEER_NAME.getBytes(), 
                             null, null);
        elm[1] = new Element("mySend:Message", "Hello there".getBytes(), 
                             null, null);
        
        Message msg = new Message(elm);        
        sendRequestId = peer.send(PIPE_NAME, pipeID, PIPE_TYPE, msg);
        System.out.println("send request id: " + sendRequestId);
    }
    
    /**
     * Loops forevers, polling for messages and printing out any that
     * contain the expected elements.
     * <p>
     * In this example, we're only interested in the message we just sent.
     * For every message that is received, we loop through and check its elements.
     * We are expecting two elements: one named "mySend:Name" which is the name of
     * the sending peer, and another named "mySend:Message" which is the text
     * message that was sent to us. If the element's name space and name match
     * these values, we display the information. 
     * </p>
     * Any elements that do not exactly match these names are ignored.
     * 
     */
    public void recvMessage() throws IOException {

        System.out.println("start polling for incoming messages...");
        
        Message msg = null;        
        while (true) {
            msg = peer.poll(1);
            if (msg == null) {
                continue;
            }

            for (int i = 0; i < msg.getElementCount(); i++) {
                Element e = msg.getElement(i);
                if ("mySend".equals(e.getNameSpace()) && 
                    "Name".equals(e.getName()))
                    System.out.println("Message from: " + 
                                       new String(e.getData()));
                if ("mySend".equals(e.getNameSpace()) && 
                    "Message".equals(e.getName()))
                    System.out.println("Message: " +
                                       new String(e.getData()));
            }
        }
    }

 /** 
     * Gets the relay URL and calls {@link #mySend mySend()}.
     * 
     * 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 mySend object. This constructor performs all the
     * interesting work in this example: creating the JXTA components,
     * connecting to the JXTA network, searching for the pipe that was
     * created, and sending and receiving messages.
     * </p>
     * @param args the command line arguments
     */
    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 {
            mySend test = new mySend(relay);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
}

⌨️ 快捷键说明

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