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

📄 microma.java

📁 基于MJSIP的j2me客户端
💻 JAVA
字号:
package local;



import org.zoolu.sip.provider.SipProvider;
import org.zoolu.sip.provider.SipProviderListener;
import org.zoolu.sip.address.NameAddress;
import org.zoolu.sip.message.Message;
import org.zoolu.sip.message.MessageFactory;
import org.zoolu.sip.message.SipResponses;
import org.zoolu.sip.header.StatusLine;
import org.zoolu.sip.transaction.TransactionClient;
import org.zoolu.sip.transaction.TransactionServer;
import org.zoolu.sip.transaction.TransactionClientListener;

import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;



public class MicroMA extends MIDlet implements /*Runnable,*/ CommandListener, SipProviderListener, TransactionClientListener
{
   String host_addr="127.0.0.77";
   //String host_addr="bingo.zoolu.org";
   int host_port=50777;
   String local_user="u77";  
   NameAddress remote_user=new NameAddress("sip:alice@127.0.0.2");   
   //NameAddress remote_user=new NameAddress("sip:alice@160.78.29.72:50666");   
   
   Display display;
   Form f;
   TextField tf_send;
   StringItem si_dialog;
   
   Command exitCommand = new Command("Exit", Command.EXIT, 1);
   Command sendCommand = new Command("Send", Command.ITEM, 1);
   
   boolean isPaused=true;
   Message message=null;
   SipProvider sip_provider=null;
   NameAddress from_url;


   public MicroMA()
   {  display=Display.getDisplay(this);
      f=new Form("MicroMA");
      tf_send=new TextField("Send:", "",30,TextField.ANY);
      si_dialog=new StringItem("Dialog:"," ");
      f.append(tf_send);
      f.append(si_dialog);
      f.addCommand(exitCommand);
      f.addCommand(sendCommand);
      f.setCommandListener(this);
      display.setCurrent(f);
      
      String[] protocols={ SipProvider.PROTO_UDP };
      sip_provider=new SipProvider(host_addr,host_port,protocols,null);
      sip_provider.addSipProviderListener(SipProvider.ANY,this);
      from_url=sip_provider.completeNameAddress(local_user);
      
      // start the local thread
      //Thread t=new Thread(this);
      //t.start();
   }


   public void startApp()
   {  isPaused=false;
   }
   
   
   public void pauseApp()
   {  isPaused=true;
   }
   
   
   public void destroyApp(boolean unconditional)
   {
   }


   public void commandAction(Command c, Displayable s)
   {
      if (c==exitCommand)
      {  destroyApp(true);
         notifyDestroyed();
      }
      else
      if (c==sendCommand && !isPaused)
      {  String text=tf_send.getString();
         NameAddress to_url=new NameAddress(remote_user);
         send(to_url,from_url,text);
         tf_send.setString("");
         si_dialog.setText(si_dialog.getText()+"\nsent: "+text);
      }
   }


   public void onReceivedMessage(SipProvider provider, Message msg)
   {
      if (msg.isRequest() && msg.isMessage())
      {  remote_user=msg.getFromHeader().getNameAddress();
         si_dialog.setText(si_dialog.getText()+"\nreceived: "+msg.getBody());
         TransactionServer t=new TransactionServer(sip_provider,msg,null);
         t.respondWith(MessageFactory.createResponse(msg,200,SipResponses.reasonOf(200),null));
      }
   }


   public void send(NameAddress to, NameAddress from, String text)
   {  //System.err.println("DEBUG: send message");
      message=MessageFactory.createMessageRequest(sip_provider,to,from,null,"application/text",text);
      TransactionClient t=new TransactionClient(sip_provider,message,this);
      t.request();
   }


   public void onTransProvisionalResponse(TransactionClient tc, Message msg)
   {  onTransResponse(msg.getStatusLine());
      
   }
   public void onTransSuccessResponse(TransactionClient tc, Message msg)
   {  onTransResponse(msg.getStatusLine());
      
   }
   public void onTransFailureResponse(TransactionClient tc, Message msg)
   {  onTransResponse(msg.getStatusLine());
   }
   public void onTransTimeout(TransactionClient tc)
   {  onTransResponse(null);
   }
   
   private void onTransResponse(StatusLine resp)
   {  String result;
      if (resp==null) result="Timout"; else result=resp.getCode()+" "+resp.getReason();
      si_dialog.setText(si_dialog.getText()+"\n"+result);
   }


/*
   public synchronized void send(NameAddress target, NameAddress from, String text)
   {  System.err.println("DEBUG: send message (1)");
      message=MessageFactory.createMessageRequest(sip_provider,target,from,null,"application/text",text);
      notify();
   }


   public synchronized void run()
   {  while (true)
      {
         // If no client to deal, wait until one connects
         if (message==null)
         {   try {  wait();  } catch (InterruptedException e) {}
         }
         System.err.println("DEBUG: send message (2)");
         sip_provider.sendMessage(message);
         message=null;
      }
   }
*/
}

⌨️ 快捷键说明

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