Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.tck.msgflow.callflows.redirect;
  
  import javax.sip.*;
  import javax.sip.header.*;
  
  
 
 import java.util.*;
 
 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 TestHarness implements SipListener {
 
     private ProtocolObjects protocolObjects;
 
     private static final String myAddress = "127.0.0.1";
 
     public static final int myPort = 5070;
 
     protected ServerTransaction inviteTid;
 
     private Response okResponse;
 
     private Request inviteRequest;
 
     private Dialog dialog;
 
     private SipProvider sipProvider;
 
     private int inviteCount = 0;
 
     private int dialogTerminationCount = 0;
 
     private int dialogCount;
 
     private int byeOkRecieved;
 
     private int ackCount;
 
     private static Logger logger = Logger.getLogger(Shootme.class);
 
     class MyTimerTask extends TimerTask {
         Shootme shootme;
 
         public MyTimerTask(Shootme shootme) {
             this. = shootme;
 
         }
 
         public void run() {
             .sendInviteOK();
         }
 
     }
 
     protected static final String usageString = "java "
             + "examples.shootist.Shootist \n"
             + ">>>> is your class path set to the root?";
 
 
 
     public void processRequest(RequestEvent requestEvent) {
         Request request = requestEvent.getRequest();
         ServerTransaction serverTransactionId = requestEvent
                 .getServerTransaction();
 
         .info("\n\nRequest " + request.getMethod() + " received at "
                 + ..getStackName()
                 + " with server transaction id " + serverTransactionId);
 
         if (request.getMethod().equals(.)) {
             processInvite(requestEventserverTransactionId);
         } else if (request.getMethod().equals(.)) {
             processAck(requestEventserverTransactionId);
         } else {
             fail("unexpected request recieved");
         }
     }
 
     public void processResponse(ResponseEvent responseEvent) {
         if (((CSeqHeaderresponseEvent.getResponse()
                 .getHeader(.)).getMethod().equals(.))
             this.++;
         else {
             fail("unexpected response received");
         }
    }

    
Process the ACK request. Send the bye and complete the call flow.
    public void processAck(RequestEvent requestEvent,
            ServerTransaction serverTransaction) {
        try {
            this.++;
            .debug("shootme: got an ACK " );
            .debug("Dialog State = " + .getState()
                            + " sending BYE ");
            // This check is required because it may be an ACK retransmission
            // If this is an ACK retransmission, we dont worry about sending BYE
            // again.
            if (.getState() == .) {
                Request bye = .createRequest(.);
                ClientTransaction ct = this.
                        .getNewClientTransaction(bye);
                .sendRequest(ct);
            }
        } catch (Exception ex) {
            .error("unexpected exception"ex);
            fail("unexpected exception sending bye");
        }
    }

    
Process the invite request.
    public void processInvite(RequestEvent requestEvent,
            ServerTransaction serverTransaction) {
        SipProvider sipProvider = (SipProviderrequestEvent.getSource();
        Request request = requestEvent.getRequest();
        try {
            this.++;
            .info("shootme: got an Invite " + this.);
            assertTrue(request.getHeader(.) != null);
            Response response = ..createResponse(
                    .request);
            ToHeader toHeader = (ToHeaderresponse.getHeader(.);
            
We distinguish here after the display header in the Request URI to manage the different tags
            Address address = .
                    .createAddress("Shootme <sip:" +  + ":" + 
                            + ";transport=" + . + ">");
            ServerTransaction st = requestEvent.getServerTransaction();
            if (st == null) {
                st = sipProvider.getNewServerTransaction(request);
            }
            Dialog dialog = st.getDialog();
            assertTrue(this. != dialog);
            this.++;
            this. = dialog;
            .info("Shootme: dialog = " + dialog);
            st.sendResponse(response);
            ContactHeader contactHeader = .
                    .createContactHeader(address);

            
We distinguish here after the display header in the Request URI to create a final response
            if (((SipURI) (request.getRequestURI()))
                    .getParameter("redirection") == null) {
                Response moved = ..createResponse(
                        .request);
                moved.addHeader(contactHeader);
                toHeader = (ToHeadermoved.getHeader(.);
                toHeader.setTag("4321"); // Application is supposed to set.
                st.sendResponse(moved);
                // Check that the stack is assigning the right state to the
                // dialog.
                assertTrue("dialog state should be terminated"dialog
                        .getState() == .);
            } else {
                Response ringing = .
                        .createResponse(.request);
                toHeader = (ToHeaderringing.getHeader(.);
                toHeader.setTag("5432"); // Application is supposed to set.
                st.sendResponse(ringing);
                assertEquals("server tx state should be proceeding"st
                        .getState(),.);
                this. = .
                        .createResponse(.request);
                toHeader = (ToHeader.getHeader(.);
                toHeader.setTag("5432"); // Application is supposed to set.
                .addHeader(contactHeader);
                this. = st;
                // Defer sending the OK to simulate the phone ringing.
                this. = request;
                new Timer().schedule(new MyTimerTask(this), 500);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("Error sending response to INVITE");
        }
    }
    private void sendInviteOK() {
        try {
            .sendResponse();
            .info("Dialog = " + .getDialog());
            .info("shootme: Dialog state after response: "
                    + .getStatusCode() + " "
                    + .getDialog().getState());
            assertEquals("invite tx state should be terminated".getState(),.);
        } catch (SipException ex) {
            .error("unexpected exception"ex);
            fail("unexpected exception");
        } catch (InvalidArgumentException ex) {
            .error("unexpceted exception"ex);
            fail("unexpected exception");
        }
    }
    public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
        Transaction transaction;
        if (timeoutEvent.isServerTransaction()) {
            transaction = timeoutEvent.getServerTransaction();
        } else {
            transaction = timeoutEvent.getClientTransaction();
        }
        .info("state = " + transaction.getState());
        .info("dialog = " + transaction.getDialog());
        .info("dialogState = " + transaction.getDialog().getState());
        .info("Transaction Time out");
        fail("unexpected timeout occured");
    }
    public SipProvider createProvider() throws Exception {
                "127.0.0.1".);
        return this.;
    }
    public Shootme(ProtocolObjects protocolObjects) {
        this. = protocolObjects;
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        .info("IOException");
        fail("unexpected exception");
    }
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
        .info("Transaction terminated event recieved");
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        .info("Dialog terminated event recieved dialog = "
                + dialogTerminatedEvent.getDialog());
        this.++;
    }
    public void checkState() {
        assertTrue(this. == 2 && this. >= 1
                && this. >= 1);
    }
New to GrepCode? Check out our FAQ X