Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package examples.nistgoodies.leakaudit;
  
  
  import javax.sip.*;
 import java.util.*;
This example demonstrates how an application can monitor the SIP Stack for leaked dialogs and transactions.

This code is in the public domain.

Author(s):
R. Borba (Natural Convergence)
 
 public class LeakingApp implements SipListener {
 
     /// Dialogs indexed by call id
     private Map dialogs = new HashMap();
 
     /// Current CSeq
     private long cseq = 0;
 
     /// Stack objects
     private HeaderFactory headerFactory;
     private MessageFactory messageFactory;
     private AddressFactory addressFactory;
     private AllowHeader ALLOW;
 
     /// Constructor
     public LeakingApp() {
         SipFactory l_oSipFactory = SipFactory.getInstance();
         try {
              = l_oSipFactory.createHeaderFactory();
              = l_oSipFactory.createMessageFactory();
              = l_oSipFactory.createAddressFactory();
              = .createAllowHeader(. + ',' + . + ',' + .
                     + ',' + . + ',' + . + ',' + . + ',' + .);
         }
         catch (Exception e) {
             ..println("LeakingApp: Error creating stack objects. Exception:  " + e.toString());
         }
     }
 
     // ---------------------
     // SipListener interface
     // ---------------------
 
     public void processRequest(RequestEvent a_oEvent) {
         Request l_oRequest = a_oEvent.getRequest();
         String l_sCallID = ((CallIdHeaderl_oRequest.getHeader(.)).getCallId();
         ..println("processRequest(): CallID=" + l_sCallID +
                 ", Request= " + l_oRequest.getMethod());
     }
 
     public void processResponse(ResponseEvent a_oEvent) {
         Response l_oResponse = a_oEvent.getResponse();
         String l_sCallID = ((CallIdHeaderl_oResponse.getHeader(.)).getCallId();
         ..println("processResponse(): CallID=" + l_sCallID +
                 ", Response= " + l_oResponse.toString());
     }
 
     public void processTimeout(TimeoutEvent a_oEvent) {
         ..println("processTimeout()");
     }
 
     public void processIOException(IOExceptionEvent exceptionEvent) {
         ..println("IOExceptionEvent: " + exceptionEvent.toString());
     }
 
     public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {
         ..println("TransactionTerminatedEvent: " + transactionTerminatedEvent.toString());
     }
 
     public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {
         ..println("DialogTerminatedEvent: " + dialogTerminatedEvent.toString());
     }
 
 
     /// Creates a request containing an INVITE message
     private Request createRequest() {
         try {
             CallIdHeader callIdHeader = ..getNewCallId();
             CSeqHeader cseqHeader = .createCSeqHeader(++.);
             SipURI requestURI = .createSipURI("to-user""127.0.0.1");
             requestURI.setTransportParam("udp");
 
             SipURI fromURI = .createSipURI("from-user"..getIPAddress());
             Address fromAddress = .createAddress("from-user"fromURI);
            String fromTag = String.valueOf((int) (Math.random() * 10000));
            FromHeader fromHeader = .createFromHeader(fromAddressfromTag);
            SipURI toURI = .createSipURI("to-user""127.0.0.1");
            Address toAddress = .createAddress("to-user"toURI);
            String toTag = String.valueOf((int) (Math.random() * 10000));
            ToHeader toHeader = .createToHeader(toAddresstoTag);
            ListeningPoint listeningPoint = ..getListeningPoints()[0];
            ViaHeader viaHeader = .createViaHeader(..getIPAddress(),
                    listeningPoint.getPort(),
                    listeningPoint.getTransport(),
                    null);
            // add via headers
            ArrayList viaList = new ArrayList();
            viaList.add(viaHeader);
            MaxForwardsHeader maxForwards = .createMaxForwardsHeader(70);
            Request request = .createRequest(requestURI,
                    .,
                    callIdHeader,
                    cseqHeader,
                    fromHeader,
                    toHeader,
                    viaList,
                    maxForwards);
            // Add the Contact header
            SipURI sipUri = .createSipURI(null..getIPAddress());
            Address contactAddress = .createAddress(sipUri);
            ContactHeader contactHeader = .createContactHeader(contactAddress);
            request.addHeader(contactHeader);
            // Add Allow headers
            request.addHeader();
            return request;
        }
        catch (ParseException e) {
            ..println("ParseException " + e.getMessage());
        }
        catch (InvalidArgumentException e) {
            ..println("InvalidArgumentException " + e.getMessage());
        }
        catch (Exception e) {
            ..println("Exception " + e.getMessage());
        }
        return null;
    }
    // Creates a transaction for this request
    private ClientTransaction createTransaction(Request request) {
        try {
            return ..getNewClientTransaction(request);
        }
        catch (Exception e) {
            ..println("Exception " + e.getMessage());
        }
        return null;
    }
    /// Sends an INVITE request to a dummy location
    public void sendRequest() {
        try {
            // Create the request and the transaction
            Request request = createRequest();
            ClientTransaction transaction = createTransaction(request);
            // Send the request
            // System.out.println("Sending Request: " + request.toString());
            transaction.sendRequest();
            // Remember this call id and this transaction
            CallIdHeader callIdHeader = (CallIdHeaderrequest.getHeader(.);
            .put(callIdHeader.getCallId(), transaction);
            ..println("Sent INVITE, Call ID = " + callIdHeader.getCallId());
        }
        catch (Exception e) {
            ..println("Exception " + e.getMessage());
        }
    }
    /// Leaks a dialog into the stack
    public void leakDialog() {
        // Insert a foreign dialog from nowhere just so we can detect it
        // Note: Normally we would ask the application to "forget" about one of its call ids by removing one of
        // the entries in the "dialogs" hashmap". However, the stack only creates a dialog for a client transaction
        // *after* the client transaction gets a response. Since our little example is not getting any response for
        // its INVITEs (there's nobody there to respond), removing a dialog from its hashmap doesn't have any effect
        // on this test. So, we have to do it the hard way, i.e., insert a fake dialog by force into the stack and
        // wait until it is detected as leaked.
        // You should see something like this at the console:
        //
        //      Leaked dialogs:
        //          dialog id: 1234, dialog state: null
        //          Total: 1 leaked dialogs detected and removed.
        //
        // Also notice that leaked transactions are a little harder to simulate so I'm not even trying in this example.
        Request request = createRequest();
        SIPClientTransaction transaction = (SIPClientTransactioncreateTransaction(request);
        SIPDialog dialog = ((SIPTransactionStack.).createDialog(transaction);
        dialog.setDialogId("1234");
        ((SIPTransactionStack.).putDialog(dialog);
        ..println("Intentionally inserted a leaked dialog, CallID = " + dialog.getCallId());
    }
    /// Returns a list (set) of active call IDs used by this application
    public Set getActiveCallIDs() {
        return .keySet();
    }
New to GrepCode? Check out our FAQ X