Question

I currently have a SIP registration working proprly with Jain-SIP.

I get the challenge, use MD5 on the nonce etc and send my reply.

I then get the 200 OK message.

So thats all fine.

However I want to reregister automatically every X seconds depending on the expires time.

To do this I have tried to use a timer to re run the code every X seconds.

However it leads to a couple of problems:

The SipProvider is already attached and wont run a second time. Or I get an error saying the request has already been sent.

So I was wondering if anyone has any advice on how best to reregister with the server every X seconds? As in the reccomended steps to take?


Source code public void register()throws Exception{

    // all this into a create request method
    String fromName = "xxxxxxxx";
    String fromSipAddress = "sip.network.com";

    String toSipAddress = "sip.network.com";
    String toUser = "xxxxxxxx";

    SipURI fromAddress = addressFactory.createSipURI(fromName,
            fromSipAddress);

    Address fromNameAddress = addressFactory.createAddress(fromAddress);
    FromHeader fromHeader = headerFactory.createFromHeader(
            fromNameAddress, null);

    SipURI toAddress = addressFactory
            .createSipURI(toUser, toSipAddress);
    Address toNameAddress = addressFactory.createAddress(toAddress);
    ToHeader toHeader = headerFactory.createToHeader(toNameAddress,
            null);

    URI requestURI = addressFactory.createURI(
            "sip:" + "sip.network.com");

    List<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
    String ipAddress = lp.getIPAddress();
    ViaHeader viaHeader = headerFactory.createViaHeader(ipAddress,
            lp.getPort(),
            lp.getTransport(), null);

    viaHeaders.add(viaHeader);

    CallIdHeader callIdHeader = sipProvider.getNewCallId();

    CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(1L,
            Request.REGISTER);

    MaxForwardsHeader maxForwards = headerFactory
            .createMaxForwardsHeader(70);

    Request request = messageFactory.createRequest(requestURI,
            Request.REGISTER, callIdHeader, cSeqHeader, fromHeader,
            toHeader, viaHeaders, maxForwards);

    SipURI contactUrl = addressFactory.createSipURI(fromName, fromSipAddress);
    contactUrl.setPort(8002);
    contactUrl.setLrParam();

    SipURI contactURI = addressFactory.createSipURI(fromName, "sip.network.com");
    contactURI.setPort(sipProvider.getListeningPoint(lp.getTransport())
            .getPort());

    Address contactAddress = addressFactory.createAddress(contactURI);

    contactHeader = headerFactory.createContactHeader(contactAddress);
    request.addHeader(contactHeader);
    Header extensionHeader = headerFactory.createHeader("Expires",
        "120");
    request.addHeader(extensionHeader);

    inviteTid = sipProvider.getNewClientTransaction(request);
    inviteTid.sendRequest();
    Log.d("AFTERSENDREQUEST", "SipProvider = : " + sipProvider.toString());

    Log.d("INVITETID", "inviteTid = " + inviteTid.getState());

    dialog = inviteTid.getDialog();

}
public void processResponse(ResponseEvent responseEvent) {
    Message message = Message.obtain();
    message.obj = "received response "+responseEvent.getResponse();
    handler.sendMessage(message);   
    Response response = (Response) responseEvent.getResponse();
    ClientTransaction tid = responseEvent.getClientTransaction();
    CSeqHeader cseq = (CSeqHeader) response.getHeader(CSeqHeader.NAME);

    if (response.getStatusCode() == Response.UNAUTHORIZED){
    try {
        createAuthReply(authHeader, callid);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

UPDATE:

So I've figured that if I keep creating new methods the same as the register() Method and keep the same Call Id and hard code the setting of the port number I can send a few messages like this (Not in a loop).

So I must have to change something in my register code to make sure that it is a NEW request being sent every time or something like that?

Does anyone have any ideas?

Was it helpful?

Solution

//Face Palm!!!!!

Error was the SipProvider gets attached to the Activity (Android) and I had super.finish() in the code which killed the activity at the wrong time so the registering still tried but the activity was gone so the sip provider lost its context.

Thanks for your time calvinscorner, really appreciate it

OTHER TIPS

Okay, I spent quite some time in analyzing this stuff. And really I'm out-of-clue on what are we doing wrong? Everything looks fine to me. Init / Send Register / Start a Timer and on Timer expiry - Again send a register request. It also seems that sipProvider is a global object. Can you just for the sake of debugging purpose perform following two things

Fist, Invoke getNewCallId just after you send first Register request, to see everything is fine.

callIdHeader = sipProvider.getNewCallId();

Or may be try to shift

inviteTid = sipProvider.getNewClientTransaction(request);

ahead of the getNewCallId in new Re-register requst, to veify sipProvider object is available.

What I'm saying is nothing but elementary style of debugging.

You need not require to initialize service provider everytime you want to send a new Register request. Here are the steps that I suggest you should follow.

  • Initialize service provider
  • Send out first registration request.
  • Once you receive 200 OK - retrieve expires field and start a timer based on that expires value.
  • Once timer is fired send a new Register request - Do not initialize service provider again. (You can check for sipProvider pointer again for NON NULL).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top