Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package examples.reinvite;
  
  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  implements SipListener {
 
 
 
     private boolean sendReInviteFlag = true;
 
     // To run on two machines change these to suit.
     public static final String myAddress = "127.0.0.1";
 
     public static final int myPort = 5070;
 
     private ServerTransaction inviteTid;
 
 
     private static Logger logger = Logger.getLogger(Shootme.class);
 
     static {
         try {
         .addAppender(new FileAppender(new SimpleLayout(),
                     . + "shootmeconsolelog.txt"));
         } catch (Exception ex) {
             throw new RuntimeException ("could not open log file");
         }
     }
 
 
     private Dialog dialog;
 
     private boolean okRecieved;
 
     class ApplicationData {
         protected int ackCount;
     }
 
     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 shootme "
                 + " 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);
         }
 
     }

    
Process the ACK request. Send the bye and complete the call flow.
 
     public void processAck(RequestEvent requestEvent,
             ServerTransaction serverTransaction) {
         SipProvider sipProvider = (SipProviderrequestEvent.getSource();
         try {
             .info("shootme: got an ACK "
                     + requestEvent.getRequest());
             TestCase.assertTrue ("dialog mismatch "this. == serverTransaction.getDialog());
 
             int ackCount = ((ApplicationData.getApplicationData()).;
             if (ackCount == 1 && this.) {
                      = .getDialog();
                 this.sendReInvite(sipProvider);
 
             } else
                ((ApplicationData.getApplicationData()).++;
        } catch (Exception ex) {
            String s = "Unexpected error";
            .error(s,ex);
            TestCase.fail(s);
        }
    }

    
Process the invite request.
    public void processInvite(RequestEvent requestEvent,
            ServerTransaction serverTransaction) {
        SipProvider sipProvider = (SipProviderrequestEvent.getSource();
        Request request = requestEvent.getRequest();
        .info("Got an INVITE  " + request);
        try {
            .info("shootme: got an Invite sending OK");
            // logger.info("shootme: " + request);
            Response response = ..createResponse(180, request);
            ToHeader toHeader = (ToHeaderresponse.getHeader(.);
            toHeader.setTag("4321");
            Address address = ..createAddress("Shootme <sip:"
                    +  + ":" +  + ">");
            ContactHeader contactHeader = .
                    .createContactHeader(address);
            response.addHeader(contactHeader);
            ServerTransaction st = requestEvent.getServerTransaction();
            if (st == null) {
                st = sipProvider.getNewServerTransaction(request);
                .info("Server transaction created!" + request);
                .info("Dialog = " + st.getDialog());
                if (st.getDialog().getApplicationData() == null) {
                    st.getDialog().setApplicationData(new ApplicationData());
                }
            } else {
                // If Server transaction is not null, then
                // this is a re-invite.
                .info("This is a RE INVITE ");
                if (st.getDialog() != ) {
                    .error("Whoopsa Daisy Dialog Mismatch "
                            + st.getDialog() + " / " + );
                    TestCase.fail("Dialog mismatch " + st.getDialog() + " dialog  = " + );
                }
            }
            // Thread.sleep(5000);
            .info("got a server tranasaction " + st);
            byte[] content = request.getRawContent();
            if (content != null) {
                .info(" content = " + new String(content));
                ContentTypeHeader contentTypeHeader = .
                        .createContentTypeHeader("application""sdp");
                .info("response = " + response);
                response.setContent(contentcontentTypeHeader);
            }
             = st.getDialog();
            if ( != null) {
                .info("Dialog " + );
                .info("Dialog state " + .getState());
            }
            st.sendResponse(response);
            response = ..createResponse(200, request);
            toHeader = (ToHeaderresponse.getHeader(.);
            toHeader.setTag("4321");
            // Application is supposed to set.
            response.addHeader(contactHeader);
            st.sendResponse(response);
            .info("TxState after sendResponse = " + st.getState());
            this. = st;
        } catch (Exception ex) {
            String s = "unexpected exception";
            .error(s,ex);
            TestCase.fail(s);
        }
    }
    public void sendReInvite(SipProvider sipProviderthrows Exception {
        Request inviteRequest = .createRequest(.);
        ((SipURIinviteRequest.getRequestURI()).removeParameter("transport");
        inviteRequest.addHeader(mf);
        ((ViaHeaderinviteRequest.getHeader(.))
                .setTransport(.);
        Address address = ..createAddress("Shootme <sip:"
                +  + ":" +  + ">");
        ContactHeader contactHeader = .
                .createContactHeader(address);
        inviteRequest.setHeader(contactHeader);
        ClientTransaction ct = sipProvider
                .getNewClientTransaction(inviteRequest);
        .sendRequest(ct);
    }

    
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);
            if (serverTransactionId != null) {
                serverTransactionId.sendResponse(response);
                .info("Dialog State is "
                        + serverTransactionId.getDialog().getState());
            } else {
                .info("null server tx.");
                // sipProvider.sendResponse(response);
            }
        } catch (Exception ex) {
            String s = "Unexpected exception";
            .error(s,ex);
            TestCase.fail(s);
        }
    }
    public void processResponse(ResponseEvent responseReceivedEvent) {
        .info("Got a response");
        Response response = (ResponseresponseReceivedEvent.getResponse();
        Transaction tid = responseReceivedEvent.getClientTransaction();
        CSeqHeader cseq = (CSeqHeaderresponse.getHeader(.);
        .info("Response received with client transaction id "
                + tid + ":\n" + response);
        try {
            if (response.getStatusCode() == .
                    && ((CSeqHeaderresponse.getHeader(.))
                            .getMethod().equals(.)) {
                this.  = true;
                Dialog dialog = tid.getDialog();
                Request ackRequest = dialog.createAckcseq.getSeqNumber() );
                dialog.sendAck(ackRequest);
            }
            if ( tid != null ) {
                Dialog dialog = tid.getDialog();
                .info("Dalog State = " + dialog.getState());
            }
        } catch (Exception ex) {
            String s = "Unexpected exception";
            .error(s,ex);
            TestCase.fail(s);
        }
    }
    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 createSipProvider() throws Exception {
                .);
        SipProvider sipProvider = ..createSipProvider(lp);
        return sipProvider;
    }
    public static void main(String args[]) throws Exception {
        .addAppendernew ConsoleAppender(new SimpleLayout()));
        ProtocolObjects.init("shootme"true);
        Shootme shootme = new Shootme();
        shootme.createSipProvider().addSipListener(shootme);
    }
    public void checkState() {
        TestCase.assertTrue(data.ackCount == 1);
        TestCase.assertTrue();
    }
    /*
     * (non-Javadoc)
     *
     * @see javax.sip.SipListener#processIOException(javax.sip.IOExceptionEvent)
     */
    public void processIOException(IOExceptionEvent exceptionEvent) {
        .error("An IO Exception was detected : "
                + exceptionEvent.getHost());
    }
    /*
     * (non-Javadoc)
     *
     * @see javax.sip.SipListener#processTransactionTerminated(javax.sip.TransactionTerminatedEvent)
     */
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
        .info("Tx terminated event ");
    }
    /*
     * (non-Javadoc)
     *
     * @see javax.sip.SipListener#processDialogTerminated(javax.sip.DialogTerminatedEvent)
     */
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        .info("Dialog terminated event detected ");
    }
New to GrepCode? Check out our FAQ X