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

 
 public class AckReTransmissionTest extends ScenarioHarness implements SipListener {
 
 
     protected Shootist shootist;
 
     private Shootme shootme;
 
     private static Logger logger = Logger.getLogger("test.tck");
 
     static {
         if (!.isAttached(console))
             .addAppender(console);
     }
 
      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 Dialog dialog;
 
             private ServerTransaction reSendSt = null;
             private Response reSendResponse = null;
             private int dropAckCount = 0;
            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);
                }
            }

            
Process the ACK request. Send the bye and complete the call flow.
            public void processAck(RequestEvent requestEvent,
                    ServerTransaction serverTransaction) {
                .info("shootme: got an ACK " + requestEvent.getRequest());
                if ( <= 3){
                    boolean skip = false;
                    //drop the ACK, resend 200 OK
                    try {
                        .sendResponse();
                        .info("shootme: resending the previous 200");
                    }
                    catch (Exception ex) {
                        String s = "Unexpected error";
                        .error(sex);
                        AckReTransmissionTest.fail(s);
                        skip = true;
                    }
                    if(!skip) {
                        ++;
                        return;
                    }
                }
                SipProvider sipProvider = (SipProviderrequestEvent.getSource();
                try {
                    AckReTransmissionTest.assertTrue("ACK was not successfully retransmitted 4 times", 4 == );
                    //Create BYE request
                    Request byeRequest = .createRequest(.);
                    ClientTransaction ct = sipProvider.getNewClientTransaction(byeRequest);
                    .sendRequest(ct);
                     = null;
                     = null;
                    .info("shootme: Sending a BYE");
                } catch (Exception ex) {
                    String s = "Unexpected error";
                    .error(s,ex);
                    AckReTransmissionTest.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 {
                    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());
                    }
                    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);
                     = st;
                     = response;
                    .info("TxState after sendResponse = " + st.getState());
                    this. = st;
                } catch (Exception ex) {
                    String s = "unexpected exception";
                    .error(s,ex);
                    AckReTransmissionTest.fail(s);
                }
            }
            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);
                try {
                    if (response.getStatusCode() == .
                            && ((CSeqHeaderresponse.getHeader(.))
                                    .getMethod().equals(.)) {
                        Dialog dialog = tid.getDialog();
                        CSeqHeader cseq = (CSeqHeaderresponse.getHeader(.);
                        Request ackRequest = dialog.createAck(cseq.getSeqNumber());
                        dialog.sendAck(ackRequest);
                    }
                    if ( tid != null ) {
                        Dialog dialog = tid.getDialog();
                        .info("Dialog State = " + dialog.getState());
                    }
                } catch (Exception ex) {
                    String s = "Unexpected exception";
                    .error(s,ex);
                    AckReTransmissionTest.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 void checkState() {
                AckReTransmissionTest.assertTrue("ACK was not successfully retransmitted 4 times", 4==);
            }
            /*
             * (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 ");
            }
        }
    class Shootist  implements SipListener {
        private SipProvider provider;
        private int reInviteCount;
        private ContactHeader contactHeader;
        private ListeningPoint listeningPoint;
        private int counter;
        private  String PEER_ADDRESS = .;
        private  int PEER_PORT = .;
        private  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 ProtocolObjects protocolObjects;
        private Dialog dialog;
        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);
        }
        public void processBye(Request request,
                ServerTransaction serverTransactionId) {
            try {
                .info("shootist:  got a bye .");
                if (serverTransactionId == null) {
                    .info("shootist:  null TID.");
                    return;
                }
                Response response = ..createResponse(
                        200, request);
                serverTransactionId.sendResponse(response);
            } catch (Exception ex) {
                .error("unexpected exception",ex);
                AckReTransmissionTest.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("Dialog = " + responseReceivedEvent.getDialog());
				.info("Dialog State is "
responseReceivedEvent.getDialog().getState());
			}
            SipProvider provider = (SipProviderresponseReceivedEvent.getSource();
            try {
                if (response.getStatusCode() == .
                        && ((CSeqHeaderresponse.getHeader(.))
                                .getMethod().equals(.)) {
                    Dialog dialog = responseReceivedEvent.getDialog();
                    CSeqHeader cseq = (CSeqHeaderresponse.getHeader(.);
                    Request ackRequest = dialog.createAck(cseq.getSeqNumber());
                    .info("Ack request to send = " + ackRequest);
                    .info("Sending ACK");
                    dialog.sendAck(ackRequest);
                }
            } catch (Exception ex) {
                .error(ex);
                ex.printStackTrace();
                AckReTransmissionTest.fail("unexpected 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);
                AckReTransmissionTest.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();
                // 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);
                ((SipURI)address.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.
                AckReTransmissionTest.assertTrue(this..getState() == null);
                // send the request out.
                this..sendRequest();
            } catch (Exception ex) {
                .error("Unexpected exception"ex);
                AckReTransmissionTest.fail("unexpected exception");
            }
        }
        /*
         * (non-Javadoc)
         *
         * @see javax.sip.SipListener#processIOException(javax.sip.IOExceptionEvent)
         */
        public void processIOException(IOExceptionEvent exceptionEvent) {
            .error("IO Exception!");
            AckReTransmissionTest.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!");
        }
    }
    private SipListener getSipListener(EventObject sipEvent) {
        SipProvider source = (SipProvidersipEvent.getSource();
        SipListener listener = (SipListener) providerTable.get(source);
        assertTrue(listener != null);
        return listener;
    }
    public AckReTransmissionTest()
    {
        super("AckReTransmissionTest"true);
    }
    public void setUp() {
        try {
            this. = "udp";
            super.setUp();
             = new Shootist(getRiProtocolObjects());
            SipProvider shootistProvider = .createSipProvider();
            providerTable.put(shootistProvider);
             = new Shootme(getTiProtocolObjects());
            SipProvider shootmeProvider = .createSipProvider();
            providerTable.put(shootmeProvider);
            shootistProvider.addSipListener(this);
            shootmeProvider.addSipListener(this);
            getRiProtocolObjects().start();
            if (getTiProtocolObjects() != getRiProtocolObjects())
                getTiProtocolObjects().start();
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("unexpected exception ");
        }
    }
    public void testAckReTransmission() {
        this..sendInvite();
    }
    public void tearDown() {
        try {
            Thread.sleep(4000);
            this..checkState();
            getTiProtocolObjects().destroy();
            if (getTiProtocolObjects() != getRiProtocolObjects())
                getRiProtocolObjects().destroy();
            Thread.sleep(1000);
            this..clear();
            logTestCompleted();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public void processRequest(RequestEvent requestEvent) {
        getSipListener(requestEvent).processRequest(requestEvent);
    }
    public void processResponse(ResponseEvent responseEvent) {
        getSipListener(responseEvent).processResponse(responseEvent);
    }
    public void processTimeout(TimeoutEvent timeoutEvent) {
        getSipListener(timeoutEvent).processTimeout(timeoutEvent);
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        fail("unexpected exception");
    }
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
        getSipListener(transactionTerminatedEvent)
                .processTransactionTerminated(transactionTerminatedEvent);
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        getSipListener(dialogTerminatedEvent).processDialogTerminated(
                dialogTerminatedEvent);
    }
New to GrepCode? Check out our FAQ X