Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.unit.gov.nist.javax.sip.stack;
  
  
  import javax.sip.Dialog;
 
 
 import  test.tck.TestHarness;
 import  test.tck.msgflow.callflows.ProtocolObjects;
 import  test.tck.msgflow.callflows.ScenarioHarness;
 
 public class UdpPrackTimeoutTest extends ScenarioHarness implements SipListener {
 
     protected Shootist shootist;
 
     protected Shootme shootme;
 
     private static Logger logger = Logger.getLogger("test.tck");
 
     static {
         if (!.isAttached(console)) {
             .addAppender(console);
         }
     }
 
     public UdpPrackTimeoutTest() {
         super("reliableResponseTimeout"true);
     }
 
     public void setUp() throws Exception {
         try {
             testedImplFlag = !;
              = !testedImplFlag;
             super.transport = "udp";
             super.setUp();
 
             .info("PrackTest: setup()");
              = new Shootist(getTiProtocolObjects());
             SipProvider shootistProvider = .createProvider();
             providerTable.put(shootistProvider);
 
              = new Shootme(getRiProtocolObjects());
             SipProvider shootmeProvider = .createProvider();
             providerTable.put(shootmeProvider);
 
             shootistProvider.addSipListener(this);
             shootmeProvider.addSipListener(this);
 
             if (getTiProtocolObjects() != getRiProtocolObjects())
                 getTiProtocolObjects().start();
             getRiProtocolObjects().start();
         } catch (Exception ex) {
             .error("unexpected excecption "ex);
             fail("unexpected exception");
         }
     }
 
     public void tearDown() throws Exception {
         try {
             Thread.sleep(50000);
             this..checkState();
             this..checkState();
             getTiProtocolObjects().destroy();
             if (getRiProtocolObjects() != getTiProtocolObjects())
                 getRiProtocolObjects().destroy();
             Thread.sleep(1000);
             this..clear();
 
             logTestCompleted();
         } catch (Exception ex) {
            .error("unexpected exception"ex);
            fail("unexpected exception ");
        }
        super.tearDown();
    }
    public class Shootist implements SipListener {
        private SipProvider sipProvider;
        private AddressFactory addressFactory;
        private MessageFactory messageFactory;
        private HeaderFactory headerFactory;
        private SipStack sipStack;
        private ContactHeader contactHeader;
        private ClientTransaction inviteTid;
        private Dialog dialog;
        private String transport;
        private boolean prackTriggerReceived;
        private boolean prackConfirmed;
        public static final int myPort = 5070;
        private String toUser = "LittleGuy";
        private Response lastResponse;
        public Shootist(ProtocolObjects protObjects) {
             = protObjects.addressFactory;
             = protObjects.messageFactory;
             = protObjects.headerFactory;
             = protObjects.sipStack;
             = protObjects.transport;
        }
        public SipProvider createProvider() throws Exception {
            ListeningPoint lp = .createListeningPoint("127.0.0.1",
                    );
             = .createSipProvider(lp);
            .info( + " SIP provider " + );
            return ;
        }
        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 .");
                if (serverTransactionId == null) {
                    .info("shootist:  null TID.");
                    return;
                }
                Dialog dialog = serverTransactionId.getDialog();
                .info("Dialog State = " + dialog.getState());
                Response response = .createResponse(200, request);
                serverTransactionId.sendResponse(response);
                .info("shootist:  Sending OK.");
                .info("Dialog State = " + dialog.getState());
            } catch (Exception ex) {
                TestHarness.fail(ex.getMessage());
                System.exit(0);
            }
        }
        public void processResponse(ResponseEvent responseReceivedEvent) {
            .info("Got a response");
            Response response = (ResponseresponseReceivedEvent.getResponse();
             = response;
            ClientTransaction tid = responseReceivedEvent.getClientTransaction();
            CSeqHeader cseq = (CSeqHeaderresponse.getHeader(.);
            .info("Response received : Status Code = "
                    + response.getStatusCode() + " " + cseq);
            if (cseq.getMethod() == .) {
                 = true;
            }
            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();
             = tid.getDialog();
            try {
                if (response.getStatusCode() == .) {
                    if (cseq.getMethod().equals(.)) {
                        Request ackRequest = .createAck(((CSeqHeaderresponse.getHeader(.)).getSeqNumber());
                        .info("Sending ACK");
                        .sendAck(ackRequest);
                    }
                } else if ( response.getStatusCode() == .) {
//                  prackTriggerReceived = true;
//                  RequireHeader requireHeader = (RequireHeader) response.getHeader(RequireHeader.NAME);
//                  if ( requireHeader.getOptionTag().equalsIgnoreCase("100rel")) {
//                      Dialog dialog = tid.getDialog();
//                      Request prackRequest = dialog.createPrack(response);
//                      // create Request URI
//                      SipURI requestURI = addressFactory.createSipURI(toUser,
//                              "127.0.0.1:" + Shootme.myPort);
//                      prackRequest.setRequestURI(requestURI);
//                      ClientTransaction ct = provider.getNewClientTransaction(prackRequest);
//                      dialog.sendRequest(ct);
//                  }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                TestHarness.fail("Unexpected exception " + ex.getMessage());
            }
        }
        public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
            .info("Transaction Time out");
        }
        public void sendInvite() {
            try {
                String fromName = "BigGuy";
                String fromSipAddress = "here.com";
                String fromDisplayName = "The Master Blaster";
                String toSipAddress = "there.com";
                String toDisplayName = "The Little Blister";
                // create >From Header
                SipURI fromAddress = .createSipURI(fromName,
                        fromSipAddress);
                Address fromNameAddress = .createAddress(fromAddress);
                fromNameAddress.setDisplayName(fromDisplayName);
                FromHeader fromHeader = .createFromHeader(
                        fromNameAddress"12345");
                // create To Header
                SipURI toAddress = 
                        .createSipURI(toSipAddress);
                Address toNameAddress = .createAddress(toAddress);
                toNameAddress.setDisplayName(toDisplayName);
                ToHeader toHeader = .createToHeader(toNameAddress,
                        null);
                // create Request URI
                SipURI requestURI = .createSipURI(,
                        "127.0.0.1:" + .);
                // 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(requestURI,
                        .callIdHeadercSeqHeaderfromHeader,
                        toHeaderviaHeadersmaxForwards);
                // Create contact headers
                String host = "127.0.0.1";
                SipURI contactUrl = .createSipURI(fromNamehost);
                ListeningPoint lp = .getListeningPoint();
                contactUrl.setPort(lp.getPort());
                // Create the contact name address.
                SipURI contactURI = .createSipURI(fromNamehost);
                contactURI.setPort(.getListeningPoint()
                        .getPort());
                Address contactAddress = .createAddress(contactURI);
                // Add the contact address.
                contactAddress.setDisplayName(fromName);
                 = .createContactHeader(contactAddress);
                request.addHeader();
                /*
                 * When the UAC creates a new request, it can insist on reliable
                 * delivery of provisional responses for that request. To do that,
                 * it inserts a Require header field with the option tag 100rel into
                 * the request.
                 */
                RequireHeader requireHeader = 
                        .createRequireHeader("100rel");
                request.addHeader(requireHeader);
                // Create the client transaction.
                 = .getNewClientTransaction(request);
                this. = .getDialog();
                // send the request out.
                .sendRequest();
            } catch (Exception ex) {
                TestHarness.fail("sendInvite failed because of " + ex.getMessage());
            }
        }
        public void processIOException(IOExceptionEvent exceptionEvent) {
            .info("IOException happened for "
                    + exceptionEvent.getHost() + " port = "
                    + exceptionEvent.getPort());
        }
        public void processTransactionTerminated(
                TransactionTerminatedEvent transactionTerminatedEvent) {
            .info("Transaction terminated event recieved");
        }
        public void processDialogTerminated(
                DialogTerminatedEvent dialogTerminatedEvent) {
            .info("dialogTerminatedEvent");
        }
        public void checkState() {
            TestHarness.assertNotNull(this.);
            TestHarness.assertTrue(this..getStatusCode() == 500 );
        }
    }
    public class Shootme implements SipListener {
        protected static final int PRACK_CODE = 183;
        private SipProvider sipProvider;
        private AddressFactory addressFactory;
        private MessageFactory messageFactory;
        private HeaderFactory headerFactory;
        private SipStack sipStack;
        private static final String myAddress = "127.0.0.1";
        protected ServerTransaction inviteTid;
        private Request inviteRequest;
        private Dialog dialog;
        private String toTag;
        private String transport;
        private boolean prackRequestReceived;
        private boolean inviteReceived;
        private boolean errorResponseSent;
        private boolean transactionTimedOut;
        public static final int myPort = 5080;
        public Shootme(ProtocolObjects protObjects) {
             = protObjects.addressFactory;
             = protObjects.messageFactory;
             = protObjects.headerFactory;
             = protObjects.sipStack;
             = protObjects.transport;
        }
        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);
            } else if (request.getMethod().equals(.)) {
                processPrack(requestEventserverTransactionId);
            }
        }
        private void processPrack(RequestEvent requestEvent,
                ServerTransaction serverTransactionId) {
             = true;
            try {
                .info("shootme: got an PRACK! ");
                .info("Dialog State = " + .getState());

                
JvB: First, send 200 OK for PRACK
                Request prack = requestEvent.getRequest();
                Response prackOk = .createResponse(200, prack);
                serverTransactionId.sendResponse(prackOk);

                
Send a 200 OK response to complete the 3 way handshake for the INIVTE.
                Response response = .createResponse(200,
                        );
                ToHeader to = (ToHeaderresponse.getHeader(.);
                to.setTag(this.);
                Address address = .createAddress("Shootme <sip:"
                        +  + ":" +  + ">");
                ContactHeader contactHeader = 
                        .createContactHeader(address);
                response.addHeader(contactHeader);
                .sendResponse(response);
            } catch (Exception ex) {
                TestHarness.fail(ex.getMessage());
            }
        }
        public void processResponse(ResponseEvent responseEvent) {
        }

        
Process the ACK request. Send the bye and complete the call flow.
        public void processAck(RequestEvent requestEvent,
                ServerTransaction serverTransaction) {
            try {
                .info("shootme: got an ACK! Sending  a BYE");
                .info("Dialog State = " + .getState());
                // JvB: there should not be a transaction for ACKs; requestEvent
                // can be used to get it instead
                // Dialog dialog = serverTransaction.getDialog();
                Dialog dialog = requestEvent.getDialog();
                SipProvider provider = (SipProviderrequestEvent.getSource();
                Request byeRequest = dialog.createRequest(.);
                ClientTransaction ct = provider.getNewClientTransaction(byeRequest);
                dialog.sendRequest(ct);
            } catch (Exception ex) {
                ex.printStackTrace();
                TestHarness.fail(ex.getMessage());
            }
        }

        
Process the invite request.
        public void processInvite(RequestEvent requestEvent,
                ServerTransaction serverTransaction) {
             = true;
            SipProvider sipProvider = (SipProviderrequestEvent.getSource();
            Request request = requestEvent.getRequest();
            try {
                .info("shootme: got an Invite sending Trying");
                // logger.info("shootme: " + request);
                Response response = .createResponse(.,
                        request);
                ServerTransaction st = requestEvent.getServerTransaction();
                if (st == null) {
                    st = sipProvider.getNewServerTransaction(request);
                }
                 = st.getDialog();
                st.sendResponse(response);
                // reliable provisional response.
                Response okResponse = .createResponse(,
                        request);
                ToHeader toHeader = (ToHeaderokResponse.getHeader(.);
                this. = "4321";
                toHeader.setTag(); // Application is supposed to set.
                this. = st;
                this. = request;
                .info("sending reliable provisional response.");
                RequireHeader requireHeader = 
                        .createRequireHeader("100rel");
                okResponse.addHeader(requireHeader);
                .sendReliableProvisionalResponse(okResponse);
            } catch (Exception ex) {
              ex.printStackTrace();
                TestHarness.fail(ex.getMessage());
            }
        }

        
Process the bye request.
        public void processBye(RequestEvent requestEvent,
                ServerTransaction serverTransactionId) {
            Request request = requestEvent.getRequest();
            try {
                .info("shootme:  got a bye sending OK.");
                Response response = .createResponse(200, request);
                serverTransactionId.sendResponse(response);
                .info("Dialog State is "
                        + serverTransactionId.getDialog().getState());
            } catch (Exception ex) {
                TestHarness.fail(ex.getMessage());
                System.exit(0);
            }
        }
        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 Timed out");
             = true;
            if(transaction instanceof ServerTransaction) {
                try {
                    Response response = .createResponse(.,
                            transaction.getRequest());
                    ServerTransaction st = (ServerTransactiontransaction;
                    if (st == null) {
                        st = .getNewServerTransaction(transaction.getRequest());
                    }
                     = st.getDialog();
                    st.sendResponse(response);
                     = true;
                } catch (Exception ex) {
                    TestHarness.fail(ex.getMessage());
                    System.exit(0);
                }
            }
        }
        public SipProvider createProvider() throws Exception {
            ListeningPoint lp = .createListeningPoint("127.0.0.1",
                    );
             = .createSipProvider(lp);
            .info( + " SIP provider " + );
            return ;
        }
        public void processIOException(IOExceptionEvent exceptionEvent) {
            .info("IOException");
        }
        public void processTransactionTerminated(
                TransactionTerminatedEvent transactionTerminatedEvent) {
            .info("Transaction terminated event recieved");
        }
        public void processDialogTerminated(
                DialogTerminatedEvent dialogTerminatedEvent) {
            .info("Dialog terminated event recieved");
        }
        public void checkState() {
            TestHarness.assertTrue( && );
        }
    }
    boolean myFlag;
    public void testPrack() {
        this..sendInvite();
    }
New to GrepCode? Check out our FAQ X