Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package examples.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 TestCase 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 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) {
 
       Response r = responseEvent.getResponse();
          .info("\n\nResponse " + r.getStatusCode() + " received at "
                 + ..getStackName() );
 
       ClientTransaction ct = responseEvent.getClientTransaction();
       assertNotNullct );
       assertNotNullct.getDialog() );
     }

    
Process the ACK request. Send the bye and complete the call flow.
    public void processAck(RequestEvent requestEvent,
            ServerTransaction serverTransaction) {
        try {
            .info("shootme: got an ACK! " + requestEvent.getRequest());
            .info("Dialog State = " + .getState() + " sending BYE ");
            assertTrue(.getState() == .);
            Request bye = .createRequest(.);
            .info("bye request = " + bye);
            ClientTransaction ct = this.
                    .getNewClientTransaction(bye);
                .sendRequest(ct);
            // JvB: not yet, set upon receiving BYE OK response
            // assertEquals( DialogState.TERMINATED, dialog.getState() );
        } 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 " + request);
            assertTrue(request.getHeader(.) != null );
            Response response = ..createResponse(
                    .request);
            ToHeader toHeader = (ToHeaderresponse.getHeader(.);
            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);
                assertTrue("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), 1000);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("Error sending response to INVITE");
        }
    }
    private void sendInviteOK() {
        try {
                assertTrue(.getState() == .);
                .sendResponse();
                .info("Dialog = " + .getDialog());
                .info("shootme: Dialog state after response: "
                        + .getStatusCode() + " "
                        + .getDialog().getState());
                assertTrue(.getState() == .);
        } catch (SipException ex) {
            .error("unexpected exception"ex);
            fail("unexpected exception");
        } catch (InvalidArgumentException ex) {
            .error("unexpceted exception"ex);
            fail("unexpected exception");
        }
    }

    
Process the bye request.
    public void processBye(RequestEvent requestEvent,
            ServerTransaction serverTransactionId) {
        SipProvider sipProvider = (SipProviderrequestEvent.getSource();
        Request request = requestEvent.getRequest();
        try {
            .info("shootme:  got a bye sending OK.");
            Response response = ..createResponse(
                    200, request);
            serverTransactionId.sendResponse(response);
            .info("Dialog State is "
                    + serverTransactionId.getDialog().getState());
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    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 {
                .);
        return this.;
    }
    public Shootme(ProtocolObjects protocolObjects) {
        this. = protocolObjects;
    }
    public static void main(String args[]) throws Exception {
        Shootme shootme = new Shootme(new ProtocolObjects("shootme"true,"udp",""));
        .addAppender(new ConsoleAppender(new SimpleLayout()));
        shootme.createProvider();
        shootme.sipProvider.addSipListener(shootme);
    }
    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. == this.);
    }
New to GrepCode? Check out our FAQ X