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.challenge;
 
 
 
 
 import  test.tck.msgflow.callflows.ProtocolObjects;

This class is a UAC template.

Author(s):
M. Ranganathan
 
 
 public class Shootist implements SipListener {
 
     private SipProvider provider;
 
     private ContactHeader contactHeader;
 
     private ListeningPoint listeningPoint;
 
     private static String PEER_ADDRESS = .;
 
     private static int PEER_PORT = .;
 
     private static String peerHostPort =  + ":" + ;
 
     // To run on two machines change these to suit.
     public static final String myAddress = "127.0.0.1";
 
     private static final int myPort = 5060;
 
     protected ClientTransaction inviteTid;
 
     private int dialogsEndedEvents;
 
     private static Logger logger = Logger.getLogger(Shootist.class);
 
     static {
         if (.getAllAppenders().equals(NullEnumeration.getInstance())) {
 
             .addAppender(new ConsoleAppender(new SimpleLayout()));
 
         }
     }
 
     private ProtocolObjects protocolObjects;
 
     private Dialog dialog1dialog2;
    public Shootist(ProtocolObjects protocolObjects) {
        super();
        this. = protocolObjects;
    }
    public SipProvider createSipProvider() {
        try {
             = ..createListeningPoint(
                    .);
             = .
                    .createSipProvider();
            return ;
        } catch (Exception ex) {
            .error(ex);
            ChallengeTest.fail("unable to create provider");
            return null;
        }
    }
    public void sendInvite() {
        try {
            // Note that a provider has multiple listening points.
            // all the listening points must have the same IP address
            // and port but differ in their transport parameters.
            String fromName = "BigGuy";
            String fromSipAddress = "here.com";
            String fromDisplayName = "The Master Blaster";
            String toSipAddress = "there.com";
            String toUser = "LittleGuy";
            String toDisplayName = "The Little Blister";
            // create >From Header
            SipURI fromAddress = ..createSipURI(
                    fromNamefromSipAddress);
            Address fromNameAddress = .
                    .createAddress(fromAddress);
            fromNameAddress.setDisplayName(fromDisplayName);
            FromHeader fromHeader = .
                    .createFromHeader(fromNameAddressnew Integer((int) (Math
                            .random() * .)).toString());
            // create To Header
            SipURI toAddress = ..createSipURI(
                    toUsertoSipAddress);
            Address toNameAddress = .
                    .createAddress(toAddress);
            toNameAddress.setDisplayName(toDisplayName);
            ToHeader toHeader = ..createToHeader(
                    toNameAddressnull);
            // create Request URI
            SipURI requestURI = ..createSipURI(
                    toUser);
            // Create ViaHeaders
            ArrayList viaHeaders = new ArrayList();
            int port = .getListeningPoint(.)
                    .getPort();
            ViaHeader viaHeader = .
                    .createViaHeader(port,
                            .null);
            // add via headers
            viaHeaders.add(viaHeader);
            // Create ContentTypeHeader
            ContentTypeHeader contentTypeHeader = .
                    .createContentTypeHeader("application""sdp");
            // Create a new CallId header
            CallIdHeader callIdHeader = .getNewCallId();
            // JvB: Make sure that the implementation matches the messagefactory
            callIdHeader = .
                    .createCallIdHeader(callIdHeader.getCallId());
            // Create a new Cseq header
            CSeqHeader cSeqHeader = .
                    .createCSeqHeader(1L, .);
            // Create a new MaxForwardsHeader
            MaxForwardsHeader maxForwards = .
                    .createMaxForwardsHeader(70);
            // Create the request.
            Request request = ..createRequest(
                    requestURI.callIdHeadercSeqHeader,
                    fromHeadertoHeaderviaHeadersmaxForwards);
            // Create contact headers
            // Create the contact name address.
            SipURI contactURI = ..createSipURI(
                    fromName);
            contactURI.setPort(.getListeningPoint(
                    .).getPort());
            Address contactAddress = .
                    .createAddress(contactURI);
            // Add the contact address.
            contactAddress.setDisplayName(fromName);
             = .
                    .createContactHeader(contactAddress);
            request.addHeader();
            // The following is the preferred method to route requests
            // to the peer. Create a route header and set the "lr"
            // parameter for the router header.
            Address address = .
                    .createAddress("<sip:" +  + ":" + 
                            + ">");
            // SipUri sipUri = (SipUri) address.getURI();
            // sipUri.setPort(PEER_PORT);
            RouteHeader routeHeader = .
                    .createRouteHeader(address);
            ((SipURIaddress.getURI()).setLrParam();
            request.addHeader(routeHeader);
            // Create the client transaction.
            this. = .getNewClientTransaction(request);
            this. = this..getDialog();
            // Note that the response may have arrived right away so
            // we cannot check after the message is sent.
            ChallengeTest.assertNull(this..getState());
            // send the request out.
            this..sendRequest();
        } catch (Exception ex) {
            .error("Unexpected exception"ex);
            ChallengeTest.fail("unexpected exception");
        }
    }
    public void processRequest(RequestEvent requestReceivedEvent) {
        Request request = requestReceivedEvent.getRequest();
        ServerTransaction serverTransactionId = requestReceivedEvent
                .getServerTransaction();
        .info("\n\nRequest " + request.getMethod() + " received at "
                + ..getStackName()
                + " with server transaction id " + serverTransactionId);
        ChallengeTest.fail"Unexpected request" );
    }
    public void processResponse(ResponseEvent responseReceivedEvent) {
        .info("Got a response");
        Response response = (ResponseresponseReceivedEvent.getResponse();
        Transaction tid = responseReceivedEvent.getClientTransaction();
        .info("Response received with client transaction id " + tid
                + ":\n" + response.getStatusCode());
        if (tid == null) {
            .info("Stray response -- dropping ");
            return;
        }
        .info("transaction state is " + tid.getState());
        .info("Dialog = " + tid.getDialog());
        .info("Dialog State is " + tid.getDialog().getState());
        SipProvider provider = (SipProviderresponseReceivedEvent.getSource();
        try {
            // JvB: new: process challenge response too
            if (response.getStatusCode() == .
                    && ((CSeqHeaderresponse.getHeader(.))
                            .getMethod().equals(.)) {
                // Test: Dialog object should be reusable? send new request with
                // updated CSeq
                // and challenge response (not actually added here)
                Request origInvite = (Requesttid.getRequest().clone();
                CSeqHeader cseq = (CSeqHeaderorigInvite
                        .getHeader(.);
                cseq.setSeqNumber(cseq.getSeqNumber() + 1);
                // clear Via branch
                ViaHeader via = (ViaHeaderorigInvite
                        .getHeader(.);
                via.removeParameter("branch");
                // Cannot reuse Dialog like this!
                // Dialog dialog = tid.getDialog();
                // dialog.sendRequest(
                // provider.getNewClientTransaction(origInvite) );
                 = provider.getNewClientTransaction(origInvite);
                ChallengeTest.assertNull );
                this. = .getDialog(); // new one
                ChallengeTest.assertSameresponseReceivedEvent.getDialog() );
                ChallengeTest.assertNotSameresponseReceivedEvent.getDialog() );
                .sendRequest();
            } else if (response.getStatusCode() == .) {
                // BYE challenge response
                Dialog d = tid.getDialog();
                ChallengeTest.assertNotNulld );
                // State became terminated as soon as we sent BYE
                ChallengeTest.assertEquals.d.getState() );
                Request bye = d.createRequest. );
                // Normally, would add challenge response header here
                d.sendRequestprovider.getNewClientTransaction(bye) );
            } else if (response.getStatusCode() == .
                    && ((CSeqHeaderresponse.getHeader(.))
                            .getMethod().equals(.)) {
                // Request cancel = inviteTid.createCancel();
                // ClientTransaction ct =
                // sipProvider.getNewClientTransaction(cancel);
                Dialog dialog = tid.getDialog();
                CSeqHeader cseq = (CSeqHeaderresponse
                        .getHeader(.);
                Request ackRequest = dialog.createAck(cseq.getSeqNumber());
                .info("Ack request to send = " + ackRequest);
                .info("Sending ACK");
                dialog.sendAck(ackRequest);
                // Then wait a bit, send BYE
                Thread.sleep( 1000 );
                dialog.sendRequestprovider.getNewClientTransactiondialog.createRequest(.)));
            } else if (response.getStatusCode() == .
                    && ((CSeqHeaderresponse.getHeader(.))
                            .getMethod().equals(.)) {
                .info"Got 200 OK for BYE" );
                ChallengeTest.assertSameresponseReceivedEvent.getDialog() );
                ChallengeTest.assertEquals"Dialog2 terminated"..getState() );
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            .error(ex);
            ChallengeTest.fail("unexpceted exception");
        }
    }
    public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
        .info("Transaction Time out");
        .info("TimeoutEvent " + timeoutEvent.getTimeout());
    }
    public void checkState() {
        ChallengeTest.assertEquals"Dialog1 terminated"..getState() );
        ChallengeTest.assertEquals"Dialog2 terminated:" + .getClass(), ..getState() );
        ChallengeTest.assertEquals"Expect 2 Dialog instances", 2,  );
    }
    /*
     * (non-Javadoc)
     *
     * @see javax.sip.SipListener#processIOException(javax.sip.IOExceptionEvent)
     */
    public void processIOException(IOExceptionEvent exceptionEvent) {
        .error("IO Exception!");
        ChallengeTest.fail("Unexpected exception");
    }
    /*
     * (non-Javadoc)
     *
     * @seejavax.sip.SipListener#processTransactionTerminated(javax.sip.
     * TransactionTerminatedEvent)
     */
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
        .info("Transaction Terminated Event!");
    }
    /*
     * (non-Javadoc)
     *
     * @see
     * javax.sip.SipListener#processDialogTerminated(javax.sip.DialogTerminatedEvent
     * )
     */
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        .info("Dialog Terminated Event!");
        ++;
    }
New to GrepCode? Check out our FAQ X