Search code examples
javaregistrationsipjain-sip

how do I reregister with a sip server properly?


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?


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