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.ctx491;
 
 
 import javax.sip.*;
 
 
 import  test.tck.TestHarness;
 import  test.tck.msgflow.callflows.ProtocolObjects;
 
 import java.util.*;
 
 import  junit.framework.TestCase;

This class is a UAC template.

Author(s):
M. Ranganathan
 
 
 public class Shootist implements SipListener {
 
     private SipProvider provider;
 
     private int reInviteCount;
 
     private ContactHeader contactHeader;
 
     private ListeningPoint listeningPoint;
 
     private int counter;
 
     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 boolean okReceived;
 
     private boolean byeOkRecieved;
 
     private boolean byeSent;
 
     int reInviteReceivedCount;
 
     private static Logger logger = Logger.getLogger(Shootist.class);
 
     static {
         if (.getAllAppenders().equals(NullEnumeration.getInstance())) {
 
             .addAppender(new ConsoleAppender(new SimpleLayout()));
 
         }
     }
 
     private ProtocolObjects protocolObjects;
 
     private Dialog dialog;
 
     private boolean requestPendingSeen;
 
     public Shootist(ProtocolObjects protocolObjects) {
         super();
        this. = protocolObjects;
    }
    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);
        if (request.getMethod().equals(.))
            processBye(requestserverTransactionId);
        else if (request.getMethod().equals(.))
            processInvite(requestserverTransactionId);
        else if (request.getMethod().equals(.))
            processAck(requestserverTransactionId);
    }
    public void processInvite(Request requestServerTransaction st) {
        try {
            this.++;
            Dialog dialog = st.getDialog();
            Response response = ..createResponse(.,
                    request);
            ((ToHeaderresponse.getHeader(.)).setTag(((ToHeaderrequest
                    .getHeader(.)).getTag());
            Address address = ..createAddress("Shootme <sip:"
                    +  + ":" +  + ">");
            ContactHeader contactHeader = .
                    .createContactHeader(address);
            response.setHeader(contactHeader);
            st.sendResponse(response);
            ReInviteTest.assertEquals("Dialog for reinvite must match original dialog"dialog,
                    this.);
        } catch (Exception ex) {
            .error("unexpected exception"ex);
            ReInviteTest.fail("unexpected exception");
        }
    }
    public void processAck(Request requestServerTransaction tid) {
        try {
            .info("Got an ACK! sending bye : " + tid);
            if (tid != null) {
                Dialog dialog = tid.getDialog();
                ReInviteTest.assertSame("dialog id mismatch"dialogthis.);
                Request bye = dialog.createRequest(.);
                MaxForwardsHeader mf = ..createMaxForwardsHeader(10);
                bye.addHeader(mf);
                ClientTransaction ct = .getNewClientTransaction(bye);
                dialog.sendRequest(ct);
                this. = true;
            }
        } catch (Exception ex) {
            .error("unexpected exception"ex);
            ReInviteTest.fail("unexpected exception");
        }
    }
    public void processBye(Request requestServerTransaction serverTransactionId) {
        try {
            .info("shootist:  got a bye .");
            if (serverTransactionId == null) {
                .info("shootist:  null TID.");
                return;
            }
            Dialog dialog = serverTransactionId.getDialog();
            ReInviteTest.assertSame("dialog mismatch"dialogthis.);
            .info("Dialog State = " + dialog.getState());
            Response response = ..createResponse(200, request);
            serverTransactionId.sendResponse(response);
            .info("shootist:  Sending OK.");
            .info("Dialog State = " + dialog.getState());
            ReInviteTest.assertEquals("Should be terminated"dialog.getState(),
                    .);
        } catch (Exception ex) {
            .error("unexpected exception"ex);
            ReInviteTest.fail("unexpected exception");
        }
    }
    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 {
            if (response.getStatusCode() == .) {
                this. = true;
                return;
            }
            if (response.getStatusCode() == .
                    && ((CSeqHeaderresponse.getHeader(.)).getMethod().equals(
                            .)) {
                Dialog dialog = tid.getDialog();
                ++;
                Request inviteRequest = dialog.createRequest(.);
                ((SipURIinviteRequest.getRequestURI()).removeParameter("transport");
                ((ViaHeaderinviteRequest.getHeader(.)).setTransport("udp");
                inviteRequest.addHeader();
                MaxForwardsHeader mf = ..createMaxForwardsHeader(10);
                inviteRequest.addHeader(mf);
                ClientTransaction ct = provider.getNewClientTransaction(inviteRequest);
                dialog.sendRequest(ct);
            } else if (response.getStatusCode() == .
                    && ((CSeqHeaderresponse.getHeader(.)).getMethod().equals(
                            .)) {
                this. = true;
            } 
        } catch (Exception ex) {
            ex.printStackTrace();
            .error(ex);
            ReInviteTest.fail("unexpceted exception");
        }
    }
    public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
        .info("Transaction Time out");
        .info("TimeoutEvent " + timeoutEvent.getTimeout());
    }
    public SipProvider createSipProvider() {
        try {
             = ..createListeningPoint(,
                    .);
             = ..createSipProvider();
            return ;
        } catch (Exception ex) {
            .error(ex);
            ReInviteTest.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(fromName,
                    fromSipAddress);
            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(1718L,
                    .);
            // Create a new MaxForwardsHeader
            MaxForwardsHeader maxForwards = .
                    .createMaxForwardsHeader(70);
            // Create the request.
            Request request = ..createRequest(requestURI,
                    .callIdHeadercSeqHeaderfromHeadertoHeaderviaHeaders,
                    maxForwards);
            // 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();
            // Add the extension header.
            Header extensionHeader = ..createHeader("My-Header",
                    "my header value");
            request.addHeader(extensionHeader);
            String sdpData = "v=0\r\n" + "o=4855 13760799956958020 13760799956958020"
                    + " IN IP4  129.6.55.78\r\n" + "s=mysession session\r\n"
                    + "p=+46 8 52018010\r\n" + "c=IN IP4  129.6.55.78\r\n" + "t=0 0\r\n"
                    + "m=audio 6022 RTP/AVP 0 4 18\r\n" + "a=rtpmap:0 PCMU/8000\r\n"
                    + "a=rtpmap:4 G723/8000\r\n" + "a=rtpmap:18 G729A/8000\r\n"
                    + "a=ptime:20\r\n";
            request.setContent(sdpDatacontentTypeHeader);
            // 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);
            extensionHeader = ..createHeader("My-Other-Header",
                    "my new header value ");
            request.addHeader(extensionHeader);
            Header callInfoHeader = ..createHeader("Call-Info",
                    "<http://www.antd.nist.gov>");
            request.addHeader(callInfoHeader);
            // 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.
            ReInviteTest.assertTrue(this..getState() == null);
            // send the request out.
            this..sendRequest();
        } catch (Exception ex) {
            .error("Unexpected exception"ex);
            ReInviteTest.fail("unexpected exception");
        }
    }
    public void checkState() {
        ReInviteTest.assertTrue("Expecting a requestPending"this.);
    }
    /*
     * (non-Javadoc)
     * 
     * @see javax.sip.SipListener#processIOException(javax.sip.IOExceptionEvent)
     */
    public void processIOException(IOExceptionEvent exceptionEvent) {
        .error("IO Exception!");
        ReInviteTest.fail("Unexpected exception");
    }
    /*
     * (non-Javadoc)
     * 
     * @see javax.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