0

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
            }
        }
    }
yotommy
  • 1,472
  • 1
  • 12
  • 17
Osman Esen
  • 1,704
  • 2
  • 21
  • 46
  • Guava has a `Stopwatch` class... – fge Apr 01 '14 at 17:04
  • It sounds like you want to know the time between two events. The first event is when you get the high-priority message. What is the second event, that is, under what conditions do you report the value of the stopwatch? – yotommy Apr 01 '14 at 17:14
  • The point is that, when I receive a high priority message, I have to start the stopwatch. When an another high priority message arrives, I'll reset the stopwatch and start it again (count from 0). If a low priority message arrives instead, it will wait the stopwatch until count = 20 sec before doing anything, since it is low priority. – Osman Esen Apr 01 '14 at 17:23
  • Do you want to take an action when a certain amount of time has passed? You haven't specified how this information is to be used, and determines the form of the answer. – yotommy Apr 01 '14 at 17:26
  • The whole point is that I have to send this message in a JSP, so the message will be displayed in a web page. A high priority message has to be displayed 20 seconds without getting interruptet by a new incoming low priority message. But a low priority message can get interrupted by an incorming high priority message whenever a high prio message arrives. That's why I have to delay the low priority messages using stopwatch. When the count reaches 21 seconds, the low priority message has to get displayed. – Osman Esen Apr 01 '14 at 17:34

1 Answers1

1

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.

yotommy
  • 1,472
  • 1
  • 12
  • 17