Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.tck.msgflow.callflows.redirect;
  
  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 Shootist extends TestHarness implements SipListener {
 
     private SipProvider sipProvider;
 
     private ProtocolObjects protocolObjects;
 
     private ContactHeader contactHeader;
 
     private ListeningPoint listeningPoint;
 
     private ClientTransaction inviteTid;
 
     private Dialog dialog;
 
     public static final int myPort = 5080;
 
     private int peerPort;
 
     private String peerHostPort;
 
     private int dialogTerminatedCount;
 
     private int transctionTerminatedCount;
 
     private int transactionCount;
 
     private int dialogCount;
 
     private boolean byeReceived;
 
     private boolean redirectReceived;
 
     private SipURI requestURI;
 
     private static Logger logger = Logger.getLogger(Shootist.class);
 
 
 
     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);
 
         // We are the UAC so the only request we get is the BYE.
         if (request.getMethod().equals(.))
             processBye(requestserverTransactionId);
 
     }
 
     public void processBye(Request request,
             ServerTransaction serverTransactionId) {
         try {
             .info("shootist:  got a bye . ServerTxId = " + serverTransactionId);
             this.  = true;
             if (serverTransactionId == null) {
                 .info("shootist:  null TID.");
                 return;
             }
 
             Dialog dialog = serverTransactionId.getDialog();
             assertTrue(dialog == this.);
             .info("Dialog State = " + dialog.getState());
             Response response = ..createResponse(
                     200, request);
             serverTransactionId.sendResponse(response);
             this.++;
             .info("shootist:  Sending OK.");
             .info("Dialog State = " + dialog.getState());
             ViaHeader via = (ViaHeaderrequest.getHeader(.);
             if (via.getTransport().equalsIgnoreCase("UDP")) {
                 assertEquals("Check for Transaction State of Completed".,serverTransactionId.getState());
             } else {
                assertEquals("Check for Transaction State of Completed".,serverTransactionId.getState());
            }
            assertEquals("Check for Dialog state of terminated",.dialog.getState() );
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    public void processResponse(ResponseEvent responseReceivedEvent) {
        .info("Got a response");
        Response response = (ResponseresponseReceivedEvent.getResponse();
        ClientTransaction tid = responseReceivedEvent.getClientTransaction();
        CSeqHeader cseq = (CSeqHeaderresponse.getHeader(.);
        .info("Response received : Status Code = "
                + response.getStatusCode() + " " + cseq);
        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());
        try {
            if (response.getStatusCode() == .) {
                .info("response = " + response);
                if (cseq.getMethod().equals(.)) {
                    Request ackRequest = .createAck(cseq.getSeqNumber());
                    .info("Sending ACK");
                    .sendAck(ackRequest);
                }
            } else if  (response.getStatusCode() == .) {
                // Dialog dies as soon as you get an error response.
                assertTrue(tid.getDialog().getState() == .);
                assertSame("Dialog Identity should be preserved"tid.getDialog(),this.);
                this. = true;
                if (cseq.getMethod().equals(.)) {
                    // lookup the contact header
                    ContactHeader contHdr = (ContactHeaderresponse
                            .getHeader(.);
                    // we can re-use the from header
                    FromHeader from = ((FromHeaderresponse
                            .getHeader(.));
                    // we use the to-address, but without the tag
                    ToHeader to = (ToHeader) (response.getHeader(.)).clone();
                    to.removeParameter("tag");
                    // the call-id can be re-used
                    CallIdHeader callID = ((CallIdHeaderresponse
                            .getHeader(.));
                    // we take the next cseq
                    long seqNo = (((CSeqHeaderresponse
                            .getHeader(.)).getSeqNumber());
                    .info("seqNo = " + seqNo);
                    CSeqHeader cseqNew = .
                            .createCSeqHeader(++seqNo"INVITE");
                    // Create ViaHeaders (either use tcp or udp)
                    ArrayList viaHeaders = new ArrayList();
                    ViaHeader viaHeader = .
                            .createViaHeader("127.0.0.1"
                                    .getListeningPoint(.).getPort(),
                                    .,
                                    null);
                    // add via headers
                    viaHeaders.add(viaHeader);
                    // create max forwards
                    MaxForwardsHeader maxForwardsHeader = .
                            .createMaxForwardsHeader(10);
                    // create invite Request
                    SipURI newUri = (SipURI)this..clone();
                    newUri.setParameter("redirection""true");
                    Request invRequest = .
                            .createRequest(newUri,
                                    "INVITE"callIDcseqNewfromto,
                                    viaHeadersmaxForwardsHeader);
                    // we set the Request URI to the address given
                    SipURI contactURI =
                    ..createSipURI(nullthis..getIPAddress());
                    contactURI.setPort(this..getPort());
                    contactURI.setTransportParam(.);
                    Address address = ..createAddress(contactURI);
                    ContactHeader contact = ..createContactHeader(address);
                    invRequest.addHeader(contact);
                    // the contacat header in the response contains where to redirect
                    // the request to -- which in this case happens to be back to the
                    // same location.
                    ContactHeader chdr = (ContactHeader)response.getHeader(.);
                    SipURI sipUri = (SipURI)chdr.getAddress().getURI();
                    sipUri.setLrParam();
                    RouteHeader routeHeader =
                        ..createRouteHeader(chdr.getAddress());
                    invRequest.addHeader(routeHeader);
                    .info("Sending INVITE to "
                            + contHdr.getAddress().getURI().toString());
                     = .getNewClientTransaction(invRequest);
                    this.++;
                    .info("New TID = " + );
                    Thread.sleep(500);
                    .sendRequest();
                    // The 100 response could have returned and changed the state of the transaction and hence this test is not valid.
                    // assertEquals("Expected calling state was " + inviteTid.getState(), inviteTid.getState(),TransactionState.CALLING);
                    .info("sendReqeust succeeded " + );
                    Dialog dialog = .getDialog();
                    assertTrue("Stack must allocate a new dialog"dialog != this.);
                    this. ++;
                    this. = dialog;
                }
            }
            
end of modified code
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("unexpeced exception");
        }
    }
    public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
        .info("Transaction Time out");
        fail("Unexpected event");
    }
    public SipProvider createProvider() throws Exception {
        .info("Shootist: createProvider()");
                "127.0.0.1".);
        this. = .
                .createSipProvider();
        assertTrue("listening point should be the same as what the provider returns for this transport",
        return ;
    }
    public void sendInvite() {
        try {
            
either use udp or tcp
            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(fromNameAddress"12345");
            // create To Header
            SipURI toAddress = ..createSipURI(
                    toUsertoSipAddress);
            Address toNameAddress = .
                    .createAddress(toAddress);
            toNameAddress.setDisplayName(toDisplayName);
            ToHeader toHeader = ..createToHeader(
                    toNameAddressnull);
            // create Request URI
            this. = ..createSipURI(
                    toUser);
            // Create ViaHeaders
            ArrayList viaHeaders = new ArrayList();
            ViaHeader viaHeader = .
                    .createViaHeader("127.0.0.1"
                            .getListeningPoint(.).getPort(), .,
                            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 = ..createCallIdHeadercallIdHeader.getCallId() );
            // Create a new Cseq header
            CSeqHeader cSeqHeader = .
                    .createCSeqHeader(1L, .);
            // Create a new MaxForwardsHeader
            MaxForwardsHeader maxForwards = .
                    .createMaxForwardsHeader(70);
            // Create the request.
            Request request = ..createRequest(
                    .callIdHeadercSeqHeader,
                    fromHeadertoHeaderviaHeadersmaxForwards);
            // Create contact headers
            String host = "127.0.0.1";
            SipURI contactUrl = ..createSipURI(
                    fromNamehost);
            
either use tcp or udp
            contactUrl.setPort(.getPort());
            contactUrl.setTransportParam(.);
            // Create the contact name address.
            Address contactAddress = .
                    .createAddress(contactUrl);
            contactUrl.setLrParam();
            // Add the contact address.
            contactAddress.setDisplayName(fromName);
             = .
                    .createContactHeader(contactAddress);
            request.addHeader();
            SipURI uri = ..createSipURI(null"127.0.0.1");
            uri.setLrParam();
            uri.setTransportParam(.);
            uri.setPort(this.);
            Address address = ..createAddress(uri);
            RouteHeader routeHeader = ..createRouteHeader(address);
            request.addHeader(routeHeader);
            Header callInfoHeader = ..createHeader(
                    "Call-Info""<http://www.antd.nist.gov>");
            request.addHeader(callInfoHeader);
            // Create the client transaction.
             = .getNewClientTransaction(request);
            this. ++;
            //assertTrue(inviteTid.getState() == TransactionState.CALLING);
            .info("client tx = " + );
            this. = .getDialog();
            this.++;
            assertTrue(this. != null);
            //assertTrue(dialog.getState() == null);
            //  send the request out.
            .sendRequest();
        } catch (Exception ex) {
            .error(ex.getMessage(),ex);
            fail("unexpected exception");
        }
    }
    public Shootist (ProtocolObjects protocolObjects) {
        this. = protocolObjects;
        this. = .;
        this. = "127.0.0.1:";
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        .info("IOException happened for "
                + exceptionEvent.getHost() + " port = "
                + exceptionEvent.getPort());
    }
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
        .info("Transaction terminated event recieved for " +
                transactionTerminatedEvent.getClientTransaction());
        this.++;
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        this.++;
    }
    public void checkState() {
        //assertTrue(dialogTerminatedCount == dialogCount);
        .info("byeRecieved = " + this.);
        .info("redirectRecieved" + this.);
        assertTrue(this.  && this. );
    }
New to GrepCode? Check out our FAQ X