Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.load.concurrency;
  import javax.sip.Dialog;
 import  junit.framework.TestCase;

This class is a UAC template. Shootist is the guy that shoots and shootme is the guy that gets shot.

Author(s):
M. Ranganathan
 
 
 public class Shootme extends TestCase implements SipListener {
 
     static AddressFactory addressFactory;
     static MessageFactory messageFactory;
     static HeaderFactory headerFactory;
     static SipStack sipStack;
 
     static String transport;
 
 
 
 
     protected static final String usageString =
         "java "
             + "examples.shootist.Shootist \n"
             + ">>>> is your class path set to the root?";
 
 
     private int terminationCount;
     private int droppedCall;
 
     public void processRequest(RequestEvent requestEvent) {
         Request request = requestEvent.getRequest();
         ServerTransaction serverTransactionId =
             requestEvent.getServerTransaction();
 
 
 
         if (request.getMethod().equals(.)) {
             processInvite(requestEventserverTransactionId);
         } else if (request.getMethod().equals(.)) {
             processAck(requestEventserverTransactionId);
         } else if (request.getMethod().equals(.)) {
             processBye(requestEventserverTransactionId);
         }
 
     }

    
Process the ACK request. Send the bye and complete the call flow.
 
     public void processAck(
         RequestEvent requestEvent,
         ServerTransaction serverTransaction) {
         SipProvider sipProvider = (SipProviderrequestEvent.getSource();
         try {
             // System.out.println("shootme: got an ACK " );
             // maybe a late arriving ack.
             if (serverTransaction == nullreturn;
             Dialog dialog = serverTransaction.getDialog();
                 dialog = serverTransaction.getDialog();
             Request byeRequest = dialog.createRequest(.);
             ClientTransaction tr =
                 sipProvider.getNewClientTransaction(byeRequest);
             dialog.sendRequest(tr);
         } catch (Exception ex) {
             ex.printStackTrace();
             fail("unexpected exception");
         }
     }

    
Process the invite request.
 
     public void processInvite(
         RequestEvent requestEvent,
        ServerTransaction serverTransaction) {
        SipProvider sipProvider = (SipProviderrequestEvent.getSource();
        Request request = requestEvent.getRequest();
        try {
            Response response = .createResponse(180, request);
            ToHeader toHeader = (ToHeaderresponse.getHeader(.);
            String toTag = new Integer((int) (Math.random() * 1000)).toString();
            toHeader.setTag(toTag); // Application is supposed to set.
            //System.out.println("toTag = " + toTag);
            Address address =
                .createAddress("Shootme <sip:127.0.0.1:5070;transport=" +  + ">");
            ContactHeader contactHeader =
                .createContactHeader(address);
            response.addHeader(contactHeader);
            ServerTransaction st = requestEvent.getServerTransaction();
            if (st == null) {
                try {
                    st = sipProvider.getNewServerTransaction(request);
                } catch (TransactionAlreadyExistsException ex) {
                    ..println("Tx aready exists -- ignoring ");
                    return;
                }
            }
            byte[] content = request.getRawContent();
            if (content != null) {
                ContentTypeHeader contentTypeHeader =
                .createContentTypeHeader("application""sdp");
                // System.out.println("response = " + response);
                response.setContent(contentcontentTypeHeader);
            }
            Dialog dialog = st.getDialog();
            //System.out.println("dialog = " + dialog);
            st.sendResponse(response);
            response = .createResponse(200, request);
            toHeader = (ToHeaderresponse.getHeader(.);
            toHeader.setTag(toTag); // Application is supposed to set.
            response.addHeader(contactHeader);
            st.sendResponse(response);
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("unexpected exception");
        }
    }

    
Process the bye request.
    public void processBye(
        RequestEvent requestEvent,
        ServerTransaction serverTransactionId) {
        SipProvider sipProvider = (SipProviderrequestEvent.getSource();
        ..println("sip provider = " + sipProvider);
        Request request = requestEvent.getRequest();
        try {
            Response response =
                .createResponse(200, requestnullnull);
            serverTransactionId.sendResponse(response);
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("unexpected exception");
        }
    }
    public void processResponse(ResponseEvent responseReceivedEvent) {
        Response response = (ResponseresponseReceivedEvent.getResponse();
        Transaction tid = responseReceivedEvent.getClientTransaction();
        if (tid == null)  {
            ..println("Stray response -- dropping!");
            return;
        }
        try {
            if (response.getStatusCode() == .
                && ((CSeqHeaderresponse.getHeader(.))
                    .getMethod()
                    .equals(
                    .)) {
                Dialog dialog = tid.getDialog();
                // Save the tags for the dialog here.
                Request request = tid.getRequest();
                dialog.sendAck(request);
            }
            Dialog dialog = tid.getDialog();
            assertTrue("Dialog should not be null",dialog != null);
            //System.out.println("dialog = " + dialog);
        } catch (SipException ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
        Transaction transaction;
        Request request = null;
        if (timeoutEvent.isServerTransaction()) {
            transaction = timeoutEvent.getServerTransaction();
        } else {
            transaction = timeoutEvent.getClientTransaction();
            request = ((ClientTransactiontransaction).getRequest();
        }
        /*
         * System.out.println("request = " + request); System.out.println("state = " +
         * transaction.getState()); System.out.println("dialog = " +
         * transaction.getDialog()); System.out.println( "dialogState = " +
         * transaction.getDialog().getState()); System.out.println("Transaction
         * Time out"); System.out.println("Transaction " + transaction);
         * System.out.println("request " + transaction.getRequest());
         */
        this. ++;
        ..println("droped call " + this.);
    }
    public SipProvider createSipProvider() throws Exception {
        ListeningPoint listeningPoint = .createListeningPoint("127.0.0.1", 5070, );
        SipProvider sipProvider = .createSipProvider(listeningPoint);
        return sipProvider;
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        ..println("IOException occured while retransmitting requests:" + exceptionEvent);
    }
    public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {
        //System.out.println("Transaction Terminated event: " + transactionTerminatedEvent );
    }
    public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {
        //System.out.println("Dialog Terminated event: " + dialogTerminatedEvent);
        this. ++;
        if ( %100 == 0 ) ..println("DialogTermination count = " + this.);
    }
    public static void main(String args[]) throws Exception {
        ProtocolObjects.init("shootme"true);
        Shootme shootme = new Shootme();
        SipProvider sipProvider = shootme.createSipProvider();
        sipProvider.addSipListener(shootme);
    }
New to GrepCode? Check out our FAQ X