Question

I have implemented a SIP Servlet, where I receive two types of messages from the clients. I can receive either a high priority message and low priority message, which I separate when I'm reading the URIs of the messages as shown in the code below. I have to implement a basic stopwatch, which increments the "count" integer declared in code below. How do I make such a stopwatch and resetting it ?.

protected void doRequest(SipServletRequest reqfromclient) throws javax.servlet.ServletException, java.io.IOException {
    if( reqfromclient.getMethod().equals("MESSAGE") ) {
        String MESSAGE = reqfromclient.getContent().toString();
        System.out.println("The arrived message is " + MESSAGE);          

        // Assign the callee URI
        String URICallee = reqfromclient.getTo().getURI().toString();

         //Assign the caller URI
        String URICaller = reqfromclient.getFrom().getURI().toString();


        //DECLARE STOPWATCH
        int count = 0;


        // Now the Highprio and Lowprio alerts have to be separated
        if(URICallee.endsWith("policeHigh.com")) {
            // RESET STOPWATCH
            //START THE STOPWATCH. INCREMENT COUNT EVERY SECOND
        }

        else if(URICallee.endsWith("policeLow.com")) {              
            if(count == 21) {                   
            //something
            }
        }
    }
Was it helpful?

Solution

To execute some arbitrary code based on a timer, use the ServletTimer class which can be created from the TimerService. There are several parts to this:

  • When you want to set a timer, obtain a reference to the TimerService.
  • Use the TimerService to create a ServletTimer with the required timeout period.
  • Store the ID of the ServletTimer somewhere (as an attribute in the SipSession or SipApplicationSession).
  • If the timer needs to be cancelled, retrieve the timer ID from the session, retrieve the timer using sipApplicationSession.getTimer(id) and call cancel() on it.
  • You will need some class that implements the TimerListener interface. It can be your servlet class if you wish. Implement the timeout method with the logic that your application needs to perform when the timeout expires. As discussed in this link, declare the class to be a listener in the SIP deployment descriptor.
  • Optional: when your call is over, call sipApplicationSession.invalidate(), which will cancel any outstanding timers.

A simple example is shown here. The example is flawed in that it stores the ServletTimer as a field of the servlet class, so it will get overwritten as subsequent calls come in. Storing the ID as an attribute of the SipApplicationSession will keep it from getting overwritten.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top