Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package examples.cancel;
  
  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 static final String myAddress = "127.0.0.1";
 
     private static final String transport = "udp";
 
     private static final int myPort = 5070;
 
     private ServerTransaction inviteTid;
 
     private Response okResponse;
 
     private Request inviteRequest;
 
     private SipProvider sipProvider;
 
     private Dialog dialog;
 
     private static Logger logger = Logger.getLogger(Shootme.class);
 
     private static final String unexpectedException = "Unexpected Exception ";
 
     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 if (request.getMethod().equals(.)) {
             processBye(requestEventserverTransactionId);
         } else if (request.getMethod().equals(.)) {
             processCancel(requestEventserverTransactionId);
         }
 
     }
 
     public void processResponse(ResponseEvent responseEvent) {
     }

    
Process the ACK request. Send the bye and complete the call flow.
 
     public void processAck(RequestEvent requestEvent,
             ServerTransaction serverTransaction) {
         SipProvider sipProvider = (SipProviderrequestEvent.getSource();
         try {
             if (.getState() == .) {
                 Request byeRequest = .createRequest(.);
                ClientTransaction tr = sipProvider
                        .getNewClientTransaction(byeRequest);
                .info("shootme: got an ACK -- sending bye! ");
                .sendRequest(tr);
                .info("Dialog State = " + .getState());
            }
        } catch (Exception ex) {
            .error(ex);
            fail();
        }
    }

    
Process the invite request.
    public void processInvite(RequestEvent requestEvent,
            ServerTransaction serverTransaction) {
        SipProvider sipProvider = (SipProviderrequestEvent.getSource();
        Request request = requestEvent.getRequest();
        try {
            .info("shootme: got an Invite sending RINGING");
            // logger.info("shootme: " + request);
            Response response = ..createResponse(180, request);
            ToHeader toHeader = (ToHeaderresponse.getHeader(.);
            toHeader.setTag("4321"); // Application is supposed to set.
            Address address = ..createAddress("Shootme <sip:"
                    +  + ":" +  + ">");
            ContactHeader contactHeader = .
                    .createContactHeader(address);
            response.addHeader(contactHeader);
            ServerTransaction st = requestEvent.getServerTransaction();
            if (st == null) {
                st = sipProvider.getNewServerTransaction(request);
                .info("Created a new server transaction for "
                        + request.getMethod() + " serverTransaction = " + st);
            }
             = st.getDialog();
            st.sendResponse(response);
            this. = ..createResponse(200, request);
            toHeader = (ToHeader.getHeader(.);
            toHeader.setTag("4321"); // 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), 300);
        } catch (Exception ex) {
            .error(ex);
            fail();
        }
    }
    private void sendInviteOK() {
        try {
            if (.getState() != .) {
                .info("shootme: got an Invite sending OK");
                .sendResponse();
            }
        } catch (Exception ex) {
            .error(ex);
        }
    }

    
Process the bye request.
    public void processBye(RequestEvent requestEvent,
            ServerTransaction serverTransactionId) {
        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) {
            .error(ex);
            fail();
        }
    }
    public void processCancel(RequestEvent requestEvent,
            ServerTransaction serverTransactionId) {
        Request request = requestEvent.getRequest();
        try {
            .info("shootme:  got a cancel.");
            if (serverTransactionId == null) {
                .info("shootme:  null tid.");
                return;
            }
            TestCase.assertTrue( != serverTransactionId);
            Response response = ..createResponse(200, request);
            serverTransactionId.sendResponse(response);
            if (.getState() != .) {
                response = ..createResponse(
                        .);
                .sendResponse(response);
            }
        } catch (Exception ex) {
            .error(ex);
            fail();
        }
    }
    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");
    }
    public SipProvider createProvider() {
        try {
                    );
             = ..createSipProvider(lp);
            .info("udp provider " + );
            return ;
        } catch (Exception ex) {
            .error(ex);
            fail();
            return null;
        }
    }
    public static void main(String args[])throws Exception  {
        .addAppender(new ConsoleAppender(new SimpleLayout()));
        ProtocolObjects.init("shootme");
        Shootme shootme = new Shootme();
        shootme.createProvider();
        shootme.sipProvider.addSipListener(shootme);
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        // TODO Auto-generated method stub
    }
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
        if (transactionTerminatedEvent.isServerTransaction()) {
            ServerTransaction serverTx = transactionTerminatedEvent
                    .getServerTransaction();
            String method = serverTx.getRequest().getMethod();
            .info("Server Tx : " + method + " terminated ");
        }
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        // TODO Auto-generated method stub
    }
New to GrepCode? Check out our FAQ X