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

📄 jxta4jmsmessagingclient.java

📁 p2p 是现在java社区最火的一个话题, 看看jxta能给P2p带来什么吧
💻 JAVA
字号:
import net.jxta.j2me.PeerNetwork;
import net.jxta.j2me.Message;
import net.jxta.j2me.Element;

import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;

import java.io.IOException;


public class JXTA4JMSMessagingClient implements Runnable
{
    private PeerNetwork peerNetwork = null;
    private DataStore dataStore = null;
    private String outputPipeId = null;
    private StringBuffer stringBuffer = null;
    private int timePeriod;
    private TextField textField;
    private long expiryTime = 7200000l;
    
    public JXTA4JMSMessagingClient( 
                          String relayURL, 
                          String peerName, 
                          int timePeriod, 
                          TextField textField )
    {
        this.timePeriod = timePeriod;
        this.textField= textField;
        String pipeName = new String(peerName + "J2ME");

        try
        {
            byte[] persistentState  = null;            
            peerNetwork = PeerNetwork.createInstance ("J2MEClient");
            persistentState = peerNetwork.connect (relayURL, persistentState);
            
            DataStore dataStore = new DataStore(pipeName);

            if (dataStore.getPipeId() == null) {
                String pipeId = createPipe(pipeName);
                listenToPipe(pipeId);
                dataStore.setRecord( System.currentTimeMillis(), pipeId);
                
                System.out.println ("JXTA4JMSMessageClient>>>...new pipeId stored in dataStore..");
                
            }//if(dataStore.getPipeId() == null )
            else
            {
                long difference = (System.currentTimeMillis() - dataStore.getAdvPublishTime());
                                
                if (difference > expiryTime) {
                    dataStore.deleteStore(pipeName);
                    dataStore = new DataStore(pipeName);
                       
                    String pipeId = createPipe(pipeName); 
                    listenToPipe(pipeId);
                    dataStore.setRecord ( System.currentTimeMillis(), pipeId);
                    System.out.println ("JXTA4JMSMessageClient>>>...dataStore updated with new pipe ID");
                }//if (difference > expiryTime)
                
                else
                { 
                    listenToPipe(dataStore.getPipeId());
                    System.out.println ("JXTA4JMSMessageClient>>>...pipeId got from dataStore.."+dataStore.getPipeId());
                }
            }//else

            outputPipeId = searchPipe (peerName + "JMS");
            System.out.println ("JXTA4JMSMessageClient>>> outputPipe = "+outputPipeId);

        }//try
        catch ( Exception e )
        {
            e.printStackTrace();
        }//catch
        
    }//JXTA4JMSMessagingClient

    
    public void sendMessage (JXTA4JMSMessage message) {
        try
        {
            int messageID = peerNetwork.send (outputPipeId, message.getMessage());
            Message  msg = peerNetwork.poll(5000);

            if (msg != null)
                processMessage(msg, messageID, "success");

        }//try
        catch (IOException ie){
            ie.printStackTrace();
        }//catch

    }//sendMessage()

 
    private String createPipe(String pipeName) {
        String pipeId = null;    
        try 
        {
            int messageID = peerNetwork.create (PeerNetwork.PIPE, pipeName, null, PeerNetwork.UNICAST_PIPE);
            Message  msg = peerNetwork.poll(5000);

            if (msg == null)
                return null;
            
            pipeId = processMessage(msg, messageID, "success");

        }//try 
        catch (IOException ie) {
            ie.printStackTrace ();
        }//catch

        return pipeId;
        
    }//createPipe()


    private String listenToPipe (String pipeId) {
        String listenOk = null;
        try 
        {
            int messageID = peerNetwork.listen (pipeId);
            Message msg = peerNetwork.poll(5000);

            if (msg == null)
                return null;

            listenOk = processMessage (msg, messageID, "success");
        }//try 
        catch (IOException ie) {
            ie.printStackTrace ();
        }//catch
        
        return listenOk;
    }//listenToPipe()


    private String searchPipe(String pipeName) {
        String pipeId = null;    
        try
        {
            int messageID = peerNetwork.search (PeerNetwork.PIPE, "Name", pipeName, -1);
            Message msg = null;
            
            do
            {
                msg = peerNetwork.poll(5000);
                if (msg != null) {
                    pipeId = processMessage(msg, messageID, "result");
                    System.out.println("MessagClient>>>.. searched pipe id : "+pipeId );
                    return pipeId;
                }//if (msg != null)

            } while (msg!=null);

        }//try            
        catch (IOException ie) { 
            ie.printStackTrace();
        }//catch

        return null;
    }//searchPipe()

    
    private String processMessage ( Message msg, 
                                    int reqId, 
                                    String successCriterion) 
    {
        int requestIdentifierInResponse = 0;
        String searchedResourceIdentifier = null;
        String responseString = null;

        try
        {
            for (int j=0; j < msg.getElementCount(); j++) 
            {
                Element e = msg.getElement(j);
                if ((e.getNameSpace()).equals("proxy")) 
                {
                    if ((e.getName()).equals("requestId"))
                        requestIdentifierInResponse = Integer.parseInt(new String(e.getData()));
                    else if ((e.getName()).equals("response"))
                        responseString = new String (e.getData());
                    else if ((e.getName()).equals("id"))
                        searchedResourceIdentifier = new String (e.getData());
                }//if ((e.getNameSpace()).equals("proxy"))
        
            }//for (int j=0; j < msg.getElementCount(); j++)


            if (requestIdentifierInResponse == reqId) {
                if (responseString.equals (successCriterion)) {
                    if (searchedResourceIdentifier != null)
                        return searchedResourceIdentifier;    
                    else
                        return responseString;
                }
            }//if (requestIdentifierInResponse == reqId)

        }//try
        catch(NumberFormatException ne){
            ne.printStackTrace();
        }//catch
        
        return null;
    }//processMessage()


    public void run() {
        Message msg = null;
        String message = null;
        JXTA4JMSMessage jxmeMsg = null; 
        stringBuffer = new StringBuffer();

        while( true ) {
            try 
            {
                Thread.sleep(timePeriod);
                msg = peerNetwork.poll (2000);
                
                if (msg != null) {
                    jxmeMsg = new JXTA4JMSMessage (msg);
                    if ((jxmeMsg.getMessage()!=null) && (jxmeMsg.getSender()!=null))
                    {
                        textField.setLabel("Message From: "+jxmeMsg.getSender());
                        textField.setString(jxmeMsg.getMessageText());
                    }
                }//if(msg != null)
                
            }//try
            catch (Exception e)
            {
                System.out.println(e);
            }//catch
        
        }//while(true)
    
    }//run()

}//JXTA4JMSMessagingClient

⌨️ 快捷键说明

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