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.tck.msgflow;
 
 
 import java.util.List;
 
 

Title: TCK

Description: JAIN SIP 1.1 Technology Compatibility Kit

Author(s):
Emil Ivov Network Research Team, Louis Pasteur University, Strasbourg, France
Ivelin Ivanov
Version:
1.0
 
 
 public class MessageFlowHarness extends TestHarness {
     protected static final String EXTENSION_HDR = "Status-Extension";
 
     protected static int counter;
 
 
 
     // timeout values depend on pc, mine is not that powerful :)
     protected static long MESSAGES_ARRIVE_FOR = 2500;
 
     // it is really important to delete as a failure messes up following tests
     // so let's try real hard - 10 is a good number
     protected static int RETRY_OBJECT_DELETES = 10;
 
     protected static long RETRY_OBJECT_DELETES_AFTER = 500;
 
     protected static long STACKS_START_FOR = 1000;
 
     protected static long STACKS_SHUT_DOWN_FOR = 500;
 
     protected static long TRANSACTION_TIMES_OUT_FOR = 38000;
 
     protected ListeningPoint riListeningPoint = null;
 
     protected ListeningPoint tiListeningPoint = null;
 
     protected SipProvider riSipProvider = null;
 
     protected SipProvider tiSipProvider = null;
 
     protected SipEventCollector eventCollector = new SipEventCollector();
 
    protected SipStack riSipStack;
    protected SipStack tiSipStack;
    public MessageFlowHarness(String name) {
        this(name,true);
    }
    protected MessageFlowHarness(String nameboolean autoDialog) {
        super(nameautoDialog);
        ..println("Initializing test " + name);
        try {
            if (  != null)
                .resetFactory();
             = .createSipStack(getRiProperties(autoDialog));
            assertTrue"RI must be gov.nist" instanceof SIPTransactionStack );
            .resetFactory();
            .setPathNamegetImplementationPath() );
             = .createSipStack(getTiProperties());
            if ( == ) {
                throw new TckInternalError("riSipStack should not the same as tiSipStack");
            }
        } catch (TckInternalError ex){
            throw ex;
        } catch (Exception ex) {
            fail("initialization failed");
        }
    }
    // issue 17 on dev.java.net specify the headerFactory to use
    // report and fix thereof larryb@dev.java.net
    protected void addStatus(HeaderFactory headerFactoryRequest request) {
        try {
            Header extension = headerFactory.createHeader(,
                    new Integer(++).toString());
            request.addHeader(extension);
        } catch (ParseException ex) {
            // do nothing
        }
    }
    protected void addStatus(Request requestResponse response) {
        Header extension = request.getHeader();
        if (extension != null)
            response.addHeader(extension);
    }

    
Initialises both RI and TI sip stacks and stack factories.

Throws:
java.lang.Exception All Let all exceptions that come from the underlying stack to pass through and surface at JUnit Level.
    public void setUp() throws java.lang.Exception {
                "udp");
                "udp");
        // JvB: don't forget to start them!
        .start();
        .start();
        
        // If we don't wait for them to start first messages get lost and are
        // therefore reported as test failures.
        sleep();
    }

    
Sets all JAIN SIP objects to null and resets the SipFactory.

    public void tearDown() throws java.lang.Exception {
        // Delete RI SipProvider
        int tries = 0;
        for (tries = 0; tries < tries++) {
            try {
                .deleteSipProvider();
            } catch (ObjectInUseException ex) {
                // System.err.println("Retrying delete of riSipProvider!");
                sleep();
                continue;
            }
            break;
        }
        if (tries >= )
            throw new TckInternalError("Failed to delete riSipProvider!");
        // Delete RI ListeningPoint
        for (tries = 0; tries < tries++) {
            try {
                .deleteListeningPoint();
            } catch (ObjectInUseException ex) {
                // System.err.println("Retrying delete of riListeningPoint!");
                sleep();
                continue;
            }
            break;
        }
        if (tries >= )
            throw new TckInternalError("Failed to delete riListeningPoint!");
         = null;
         = null;
        // Delete TI SipProvider
        for (tries = 0; tries < tries++) {
            try {
                .deleteSipProvider();
            } catch (ObjectInUseException ex) {
                // System.err.println("Retrying delete of tiSipProvider!");
                sleep();
                continue;
            }
            break;
        }
        if (tries >= )
            throw new TiUnexpectedError("Failed to delete tiSipProvider!");
        // Delete TI ListeningPoint
        for (tries = 0; tries < tries++) {
            try {
                .deleteListeningPoint();
            } catch (ObjectInUseException ex) {
                // System.err.println("Retrying delete of tiListeningPoint!");
                sleep();
                continue;
            }
            break;
        }
        if (tries >= )
            throw new TiUnexpectedError("Failed to delete tiListeningPoint!");
        .stop();
        .stop();
         = null;
         = null;
        // Wait for stack threads to release resources (e.g. port)
        sleep();
    }
    // ========================= Utility Methods =========================
    
Creates a SipRequest using the specified factories. The request has the specified method and is meant to be sent from srcProvider to dstProvider. This method is prefered to manual creation of requests as it helps avoid using RI objects instead of corresponding TI objects (or vice versa).

Parameters:
method the request's method
addressFactory the address factory to use when creating addresses
headerFactory the header factory to use when creating headers
messageFactory the message factory to use when creating headers
srcProvider the provider that will eventually be used to send the request
dstProvider the provider that will eventually dispatch the request to a SipListener
contentType if the content parameter is not null then this is its content type.
contentSubType if the content parameter is not null then this is its sub content type.
content the content of the request. if null this parameter is ignored
Returns:
a request generated by the specified factories and destined to go from srcProvider to dstProvider
Throws:
java.lang.Exception if anything should go wrong. further exception handling is left to calling methods (or JUnit).
    protected Request createRequest(String method,
            AddressFactory addressFactoryHeaderFactory headerFactory,
            MessageFactory messageFactorySipProvider srcProvider,
            SipProvider dstProviderString contentTypeString contentSubType,
            Object contentthrows Exception {
        // Source SipUri
        ListeningPoint srclp = srcProvider.getListeningPoints()[0];
        SipURI srcSipURI = addressFactory.createSipURI(nullsrclp
                .getIPAddress());
        srcSipURI.setPort(srclp.getPort());
        srcSipURI.setTransportParam(srclp.getTransport());
        // Destination SipURI
        ListeningPoint dstlp = dstProvider.getListeningPoints()[0];
        SipURI dstSipURI = addressFactory.createSipURI(nulldstlp
                .getIPAddress());
        dstSipURI.setPort(dstlp.getPort());
        dstSipURI.setTransportParam(dstlp.getTransport());
        // CallId
        CallIdHeader callId = srcProvider.getNewCallId();
        callId = headerFactory.createCallIdHeadercallId.getCallId() );
        // CSeq
        CSeqHeader cSeq = headerFactory.createCSeqHeader(1L, method);
        // From
        Address fromAddress = addressFactory.createAddress(srcSipURI);
        FromHeader from = headerFactory.createFromHeader(fromAddress, Integer
                .toString(srcProvider.hashCode()));
        // To
        Address toAddress = addressFactory.createAddress(dstSipURI);
        ToHeader to = headerFactory.createToHeader(toAddressnull);
        // Contact
        ContactHeader contact = headerFactory.createContactHeader(fromAddress);
        List via = new LinkedList();
        ViaHeader viaHeader = headerFactory.createViaHeader(srclp
                .getIPAddress(), srclp.getPort(), srclp.getTransport(),
        // BUG: Use proper RFC3261 branch ID
                "z9hG4bK" + Long.toString(System.currentTimeMillis())
        // branch id
                );
        via.add(viaHeader);
        MaxForwardsHeader maxForwards = headerFactory
                .createMaxForwardsHeader(3);
        Request request = messageFactory.createRequest(dstSipURImethod,
                callIdcSeqfromtoviamaxForwards);
        request.addHeader(contact);
        if (contentType != null && contentSubType != null && content != null) {
            ContentTypeHeader contentTypeHdr = headerFactory
                    .createContentTypeHeader(contentTypecontentSubType);
            request.setContent(contentcontentTypeHdr);
        }
        // pass the headerFactory - issue17 by larryb@dev.java.net
        addStatus(headerFactoryrequest);
        return request;
    }

    
Creates an invite request object using the RI. This invite request is meant to be sent to the TI

Parameters:
contentType if the content parameter is not null then this is its content type.
contentSubType if the content parameter is not null then this is its content sub type.
content if the request is to have any content then this parameter is used to set it. Th content parameter is to be left to null if the request won't have any content.
Returns:
an RI->TI invite request
Throws:
test.tck.TckInternalError if anything should gou wrong.
    protected Request createRiInviteRequest(String contentType,
            String contentSubTypeObject contentthrows TckInternalError {
        try {
            return createRequest(.,
                    ,
                    contentTypecontentSubTypecontent);
        } catch (Throwable exc) {
            throw new TckInternalError(
                    "Failed to create an RI->TI invite request"exc);
        }
    }

    
Creates an invite request object using the TI. This invite request is meant to be sent to the RI

Parameters:
contentType if the content parameter is not null then this is its content type.
contentSubType if the content parameter is not null then this is its content sub type.
content if the request is to have any content then this parameter is used to set it. Th content parameter is to be left to null if the request won't have any content.
Returns:
an TI->RI invite request
Throws:
test.tck.TiUnexpectedError if anything should gou wrong.
    protected Request createTiInviteRequest(String contentType,
            String contentSubTypeObject contentthrows TiUnexpectedError {
        try {
            return createRequest(.,
                    ,
                    contentTypecontentSubTypecontent);
        } catch (Throwable exc) {
            throw new TiUnexpectedError(
                    "Failed to create a TI->RI invite request"exc);
        }
    }

    
Creates a register request object using the RI. This register request is meant to be sent to the TI

Returns:
an RI->TI register request
Throws:
test.tck.TckInternalError if anything should gou wrong.
    protected Request createRiRegisterRequest() throws TckInternalError {
        try {
            return createRequest(.,
                    ,
                    nullnullnull);
        } catch (Throwable exc) {
            throw new TckInternalError(
                    "Failed to create an RI->TI register request"exc);
        }
    }

    
Creates a register request object using the TI. This register request is meant to be sent to the RI

Returns:
a TI->RI register request
Throws:
test.tck.TiUnexpectedError if anything should gou wrong.
    protected Request createTiRegisterRequest() throws TiUnexpectedError {
        try {
            return createRequest(.,
                    ,
                    nullnullnull);
        } catch (Throwable exc) {
            throw new TiUnexpectedError(
                    "Failed to create a TI->RI register request"exc);
        }
    }
    public static void waitLongForMessage() {
        sleep(2*);
    }
    
Waits during LISTEN_TIMEOUT milliseconds. This method is called after a message has been sent so that it has the time to propagate though the sending and receiving stack
    public static void waitForMessage() {
        sleep();
    }

    
Wait till a transaction times out.
    protected static void waitForTimeout() {
    }
    
waits a good long time for messages.
    protected static void waitShortForMessage() {
        sleep(/2);
    }

    
Waits during _no_less_ than sleepFor milliseconds. Had to implement it on top of Thread.sleep() to guarantee minimum sleep time.

Parameters:
sleepFor the number of miliseconds to wait
    protected static void sleep(long sleepFor) {
        long startTime = System.currentTimeMillis();
        long haveBeenSleeping = 0;
        while (haveBeenSleeping < sleepFor) {
            try {
                //Thread.sleep(sleepFor - haveBeenSleeping);
                if ( sleepFor - haveBeenSleeping < 750) {
                    Thread.sleep(sleepFor - haveBeenSleeping);
                } else {
                    Thread.sleep(750);
                    ..print(".");
                }
            } catch (InterruptedException ex) {
                // we-ll have to wait again!
            }
            haveBeenSleeping = (System.currentTimeMillis() - startTime);
        }
    }

    
Add a contact for the TI.
    public ContactHeader createTiContact() throws Exception {
        try {
            ContactHeader contact = .createContactHeader();
            // JvB: getIPAddress may return null!
            String ip = .getSipStack().getIPAddress();
            if (ip == null) {
                ListeningPoint lp = (ListeningPoint
                        .getSipStack().getListeningPoints().next();
                ip = lp.getIPAddress();
            }
            SipURI srcSipURI = .createSipURI(nullip);
            srcSipURI.setPort(.getListeningPoint("udp").getPort());
            srcSipURI.setTransportParam("udp");
            Address address = .createAddress(srcSipURI);
            address.setDisplayName("TI Contact");
            contact.setAddress(address);
            return contact;
        } catch (Exception ex) {
            ex.printStackTrace();
            assertTrue(false);
            throw ex;
        }
    }

    
Add a contact for the TI.
    public ContactHeader createRiContact() throws TckInternalError {
        try {
            ContactHeader contact = .createContactHeader();
            // BUG reported by Ben Evans (Open Cloud):
            // Should be using RI's address factory here, not TI's.
            ListeningPoint lp = .getListeningPoints()[0];
            SipURI srcSipURI = .createSipURI(nulllp
                    .getIPAddress());
            srcSipURI.setPort(lp.getPort());
            srcSipURI.setTransportParam(lp.getTransport());
            Address address = .createAddress(srcSipURI);
            address.setDisplayName("RI Contact");
            contact.setAddress(address);
            return contact;
        } catch (Exception ex) {
            throw new TckInternalError(ex.getMessage());
        }
    }
New to GrepCode? Check out our FAQ X