Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * Conditions Of Use
  *
  * This software was developed by employees of the National Institute of
  * Standards and Technology (NIST), and others.
  * This software is has been contributed to the public domain.
  * As a result, a formal license is not needed to use the software.
  *
  * This software is provided "AS IS."
 * NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
 * AND DATA ACCURACY.  NIST does not warrant or make any representations
 * regarding the use of the software or the results thereof, including but
 * not limited to the correctness, accuracy, reliability or usefulness of
 * the software.
 *
 *
 */
 package test.unit.gov.nist.javax.sip.stack.reinvitechallenge;
 
 
 import javax.sip.*;
 
 import  junit.framework.TestCase;
 
 
 import  test.tck.msgflow.callflows.ProtocolObjects;

This class is a UAC template.

Author(s):
M. Ranganathan
 
 
 public class Shootme  implements SipListener {
 
 
     private ProtocolObjects  protocolObjects;
 
 
     // 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{
         if (.getAllAppenders().equals(NullEnumeration.getInstance())) {
 
             .addAppender(new ConsoleAppender(new SimpleLayout()));
 
 
         }
     }
 
     private Dialog dialog;
 
     private boolean okRecieved;
 
     class ApplicationData {
         protected int ackCount;
     }
 
     public Shootme(ProtocolObjects protocolObjects) {
         this. = protocolObjects;
     }
 
 
     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);
         }
    }

    
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());
            int ackCount = ((ApplicationData.getApplicationData()).;
            if (ackCount == 1) {
                 = .getDialog();
                Thread.sleep(100);
                this.sendReInvite(sipProvider);
            } 
            ((ApplicationData.getApplicationData()).++;
               
          
                
        } catch (Exception ex) {
            String s = "Unexpected error";
            .error(s,ex);
            ReInviteTest.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 ");
                ReInviteTest.assertSame("Dialog mismatch "st.getDialog(),this.);
            }
            // 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);
            ReInviteTest.fail(s);
        }
    }
    public void sendReInvite(SipProvider sipProviderthrows Exception {
        Request inviteRequest = .createRequest(.);
        ((SipURIinviteRequest.getRequestURI()).removeParameter("transport");
        MaxForwardsHeader mf = ..createMaxForwardsHeader(10);
        inviteRequest.addHeader(mf);
        ((ViaHeaderinviteRequest.getHeader(.))
                .setTransport(.);
        Address address = ..createAddress("Shootme <sip:"
                +  + ":" +  + ">");
        ContactHeader contactHeader = .
                .createContactHeader(address);
        inviteRequest.addHeader(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);
            ReInviteTest.fail(s);
        }
    }
    public void processResponse(ResponseEvent responseReceivedEvent) {
        .info("Got a response");
        Response response = (ResponseresponseReceivedEvent.getResponse();
        Transaction tid = responseReceivedEvent.getClientTransaction();
        
        SipProvider provider = (SipProviderresponseReceivedEvent.getSource();
        .info("Response received with client transaction id "
                + tid + ":\n" + response);
        try {
            if (response.getStatusCode() == .
                    && ((CSeqHeaderresponse.getHeader(.))
                            .getMethod().equals(.)) {
                this.  = true;
                ReInviteTest.assertNotNull( "INVITE 200 response should match a transaction"tid );
                Dialog dialog = tid.getDialog();
                CSeqHeader cseq = (CSeqHeaderresponse.getHeader(.);
                Request request = dialog.createAck(cseq.getSeqNumber());
                dialog.sendAck(request);
            } else if ( response.getStatusCode() == .)  {
            	 AuthenticationHelper authenticationHelper =
                     ((SipStackExt.).getAuthenticationHelper(new AccountManagerImpl(),
                    		 .);
                 tid = authenticationHelper.handleChallenge(response, (ClientTransactiontidprovider, 5);
                 Thread.sleep(100);
                 if ( .getState() == .) {
                	 .sendRequest((ClientTransactiontid);
                 } else {
                	 ((ClientTransaction)tid).sendRequest();
                 }
            } else {
            	ReInviteTest.fail("Unexpected response "response.getStatusCode());
            }
            if ( tid != null ) {
                Dialog dialog = tid.getDialog();
                .info("Dalog State = " + dialog.getState());          }
        } catch (Exception ex) {
            String s = "Unexpected exception";
            .error(s,ex);
            ReInviteTest.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 {
        ListeningPoint lp = ..createListeningPoint(,
                .);
        SipProvider sipProvider = ..createSipProvider(lp);
        return sipProvider;
    }
    public static void main(String args[]) throws Exception {
        .addAppendernew ConsoleAppender(new SimpleLayout()));
        ProtocolObjects protocolObjects = new ProtocolObjects("shootme""gov.nist","udp",true,false);
        Shootme shootme = new Shootme(protocolObjects);
        shootme.createSipProvider().addSipListener(shootme);
    }
    public void checkState() {
        ReInviteTest.assertTrue("Must see ack", 2 <= data.ackCount);
        ReInviteTest.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