1

我已经实现了一个可以使用 MJSIP 接收简单消息的 android 程序,问题是我也希望能够发送消息,我的课程是:

package org.sipdroid.sipua.ui;
import java.util.Iterator;
import java.util.Locale;
import java.util.Vector;
import org.sipdroid.sipua.SipdroidEngine;
import org.zoolu.sip.address.NameAddress;
import org.zoolu.sip.address.SipURL;
import org.zoolu.sip.call.CallListenerAdapter;
import org.zoolu.sip.dialog.Dialog;
import org.zoolu.sip.dialog.InviteDialog;
import org.zoolu.sip.header.FromHeader;
import org.zoolu.sip.header.Header;
import org.zoolu.sip.header.RecordRouteHeader;
import org.zoolu.sip.header.ToHeader;
import org.zoolu.sip.message.Message;
import org.zoolu.sip.message.MessageFactory;
import org.zoolu.sip.message.SipMethods;
import org.zoolu.sip.message.SipResponses;
import org.zoolu.sip.provider.ConnectionIdentifier;
import org.zoolu.sip.provider.DialogIdentifier;
import org.zoolu.sip.provider.SipProvider;
import org.zoolu.sip.provider.SipProviderListener;
import org.zoolu.sip.provider.TransactionIdentifier;
import org.zoolu.sip.transaction.AckTransactionServer;
import org.zoolu.sip.transaction.AckTransactionServerListener;
import org.zoolu.sip.transaction.InviteTransactionServer;
import org.zoolu.sip.transaction.InviteTransactionServerListener;
import org.zoolu.sip.transaction.TransactionClient;
import org.zoolu.sip.transaction.TransactionClientListener;
import org.zoolu.sip.transaction.TransactionServer;
import org.zoolu.sip.transaction.TransactionServerListener;
import org.zoolu.tools.LogLevel;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.Reader;
import java.io.StringReader;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.util.Log;
public class IMHandler extends TransactionServer implements TransactionClientListener,SipProviderListener,TransactionServerListener,
                            InviteTransactionServerListener, AckTransactionServerListener,
                                                        TextToSpeech.OnInitListener{

private SipProvider sip_provider;
private Message message;
private TransactionServer ts;
private AckTransactionServer ack_ts;
private String callee;
private TextToSpeech mTts;
private String contact_url;

private boolean firstTime = true;






/** constructor */
public IMHandler(SipProvider sip_provider) {
    super(sip_provider);
    this.sip_provider = sip_provider;
    listen();
    Log.e("IMHandler"," <Constructor> IMHandler");

        mTts = new TextToSpeech(Receiver.mContext, this);
}


public void setMessage( Message message){
    this.message = message;
}


/** Starts a new InviteTransactionServer. */
public void listen() {
    ts = new TransactionServer(sip_provider,"MESSAGE", this);
    ts.listen();
    Log.e("IMHandler","<listen> LISTENING FOR INCOMING IM MSGS ....");
}



public void sendMessage(String callee, String caller, String contact,String session_descriptor, String icsi){
    this.callee = callee;
    this.contact_url = contact;
    Log.e("IMHandler","********** callee::"+callee+" caller::"+caller+" contact::"+contact+" *************");
    NameAddress to_url = new NameAddress(callee);
    NameAddress from_url = new NameAddress(caller);
    SipURL request_uri = to_url.getAddress();

    NameAddress contact_url = null;
    if (contact != null) {
        if (contact.indexOf("sip:") >= 0)
            contact_url = new NameAddress(contact);
        else
            contact_url = new NameAddress(new SipURL(contact,sip_provider. getViaAddress(),sip_provider.getPort()));
    } else
        contact_url = from_url;

    Message messageIM = MessageFactory.createMessageRequest(sip_provider, to_url,  from_url, "HELLO", "text/plain", "HELLO WORLD");
    message = messageIM;
    sendIM(messageIM);
}



/**
 * Starts a new InviteTransactionClient and initializes the dialog state
 * information
 * 
 * @param invite
 *            the INVITE message
 */
public void sendIM(Message messageIM) {
    TransactionClient invite_tc = new TransactionClient(sip_provider, message, this);
    invite_tc.request();
}



@Override
public void onTransProvisionalResponse(
        org.zoolu.sip.transaction.TransactionClient tc, Message resp) {
    Log.e("IMHandler"," <onTransProvisionalResponse>  ##################");
    // TODO Auto-generated method stub

}



@Override
public void onTransSuccessResponse(
        org.zoolu.sip.transaction.TransactionClient tc, Message resp) {
    Log.e("IMHandler"," <onTransSuccessResponse>  ##################");
    // TODO Auto-generated method stub

}



@Override
public void onTransFailureResponse(
        org.zoolu.sip.transaction.TransactionClient tc, Message resp) {
    Log.e("IMHandler"," <onTransFailureResponse>  ##################");
    // TODO Auto-generated method stub

}



@Override
public void onTransTimeout(org.zoolu.sip.transaction.TransactionClient tc) {
    Log.e("IMHandler"," <onTransTimeout>  ##################");
    // TODO Auto-generated method stub

}


public Message sendResponce(String callee, String caller, String contact,Message req,NameAddress contact_address){
    this.callee = callee;

    Log.e("IMHandler","********** callee::"+callee+" caller::"+caller+" contact::"+contact+" *************");
    NameAddress to_url = new NameAddress(callee);
    NameAddress from_url = new NameAddress(caller);
    SipURL request_uri = to_url.getAddress();

    NameAddress contact_url = null;
    if (contact != null) {
        if (contact.indexOf("sip:") >= 0)
            contact_url = new NameAddress(contact);
        else
            contact_url = new NameAddress(new SipURL(contact,sip_provider. getViaAddress(),sip_provider.getPort()));
    } else
        contact_url = from_url;
    Message resp = MessageFactory.createResponse(req, 200,SipResponses.reasonOf(200), contact_address);
}


@Override
public void onTransRequest(TransactionServer ts, Message req)  {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true); // never forget this! 
    DocumentBuilder builder;
    try {
            builder = domFactory.newDocumentBuilder();
            Log.e("IMHandler","<onTransRequest> parsing body:: "+ req.getBody());
            InputStream is = new ByteArrayInputStream(req.getBody().getBytes());
            final Document doc = builder.parse(is );

            NamespaceContext NMC = new NamespaceContext() {
                public String getNamespaceURI(String s) {
                    if (s.equals(XMLConstants.DEFAULT_NS_PREFIX))
                        return doc.lookupNamespaceURI(null);
                    else
                        return doc.lookupNamespaceURI(s);
                }

                public String getPrefix(String s) {
                    return doc.lookupPrefix(s);
                }

                public Iterator getPrefixes(String s) {
                    return null;
                }
            };

            NodeList nodes =doc.getChildNodes();// (NodeList) result;
            Log.e("IMHandler"," <PARSING>  *************** result ***********"+nodes.getLength());
            for (int i = 0; i < nodes.getLength(); i++) {
                Log.e("IMHandler"," <PARSING>  *************** PARSING ***********");
                Log.e("IMHandler"," <PARSING>  NodeValue"+nodes.item(i).getNodeName());
                Log.e("IMHandler"," <PARSING>  *************** /PARSING ***********");
            }

    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String msg = req.getBody().replaceAll("<(.|\n)*?>","");
    if(firstTime ){
        Receiver.showIM();
        firstTime = false;
    }else{
        Receiver.getMessageListener().setFrom(req.getFromHeader().getValue());
        Receiver.getMessageListener().messageReceived(msg);
    }
    mTts.speak(msg,TextToSpeech.QUEUE_FLUSH,  null);

    NameAddress contact_address = new NameAddress("win@192.168.1.10");
    ConnectionIdentifier conn_id = ts.getConnectionId();

    Message resp = sendResponce(contact_address.toString(),"maxsap@192.168.1.9","maxsap@192.168.1.9",req,contact_address);
    ts.respondWith(resp);

    listen();
}








/**
 * Responds with <i>resp</i>. This method can be called when the
 * InviteDialog is in D_INVITED or D_BYED states.
 * <p>
 * If the CSeq method is INVITE and the response is 2xx, it moves to state
 * D_ACCEPTED, adds a new listener to the SipProviderListener, and creates
 * new AckTransactionServer
 * <p>
 * If the CSeq method is INVITE and the response is not 2xx, it moves to
 * state D_REFUSED, and sends the response.
 */
public void respond(Message resp)
{
    Log.d("IMHandler","<respond> inside respond(resp)");
    String method = resp.getCSeqHeader().getMethod();
    int code = resp.getStatusLine().getCode();

        if (code >= 200 && code < 300) {
            ts.terminate();
            ConnectionIdentifier conn_id = ts.getConnectionId();
            ack_ts = new AckTransactionServer(sip_provider, conn_id, resp,this);
            ack_ts.respond();
            return;
        } 
}




/**
 * Responds with <i>code</i> and <i>reason</i>. This method can be called
 * when the InviteDialog is in D_INVITED, D_ReINVITED states
 */
public void respond(int code, String reason, String contact,  Message req) {
    Log.d("InviteDialog","<respond> inside respond(" + code + "," + reason + ")");

        NameAddress contact_address = null;
        if (contact != null)
            contact_address = new NameAddress(contact);
        Message resp = MessageFactory.createResponse(req, code,
                reason, contact_address);
        respond(resp);
}


@Override
public void onTransAckTimeout(AckTransactionServer transaction) {
    Log.e("IMHandler"," <onTransAckTimeout>  ##################");
    // TODO Auto-generated method stub

}



@Override
public void onTransFailureAck(InviteTransactionServer ts, Message ack) {
    Log.e("IMHandler"," <onTransFailureAck>  ##################");
    // TODO Auto-generated method stub

}



@Override
public void onReceivedMessage(SipProvider sip_provider, Message message) {
    Log.e("IMHandler"," <onReceivedMessage>  ##################");

    //NameAddress contact_address = new NameAddress("win@192.168.1.10");
//  Message resp = MessageFactory.createResponse(req, 200,SipResponses.reasonOf(200), contact_address);
//  ts.respondWith(resp);

    // TODO Auto-generated method stub

}


// Implements TextToSpeech.OnInitListener.
public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        int result = mTts.setLanguage(Locale.US);
        if (result == TextToSpeech.LANG_MISSING_DATA ||
             result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("IMHandler", "Language is not available.");
        } else {
             Log.i("IMHandler", "Language found and TTS is initiated.");
        }
    } else {
        Log.e("IMHandler", "Could not initialize TextToSpeech.");
    }
}   

}

此类能够接收从 sip 客户端发送的消息并做出响应。但我想使用发送消息方法启动即时消息会话。我正在初始化一个事务客户端,然后调用请求方法,但没有任何反应,没有错误,没有异常,也没有发送消息。有人能告诉我我做错了什么吗?

4

1 回答 1

1

发现发送消息的正确方法是:

 /** Sends a new message. */
 public void send(String recipient, String subject, String content_type, String content)                                                                                                             {  
 //    Receiver.engine(Receiver.mContext).ua.
  NameAddress to_url = new NameAddress("Win@192.168.1.37");//recipient);
  NameAddress from_url=new NameAddress(user_profile.from_url);
  Log.e("IMHANDLER","$$$$$$$$$$$$$ to_url"+to_url+" from_url"+from_url+" recipient"+recipient);
  Message req=MessageFactory.createMessageRequest(sip_provider,to_url,from_url,subject,content_type,content);
  TransactionClient t=new TransactionClient(sip_provider,req,this);
  t.request();
 }
于 2011-05-16T21:50:13.843 回答