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 javax.sip.*;
 
 
 import test.tck.*;

This class collects a single event from a given provider. The collector listens for incoming events until the desired event has been received, all following events are ignored. After extractCollectedEvent method has been called, the SipEventCollecter removes itself as a listener from the provider and goes back to its original state (i.e. could be reused). The collector ignores all events except the one it is supposed to collect. If an extract operation is a attempted on an event other than the collected event a TckInternalError is thrown. All exceptions raised within the collector are let to pass through and are responsibility of the using class

This class does not test event dispatching! Event dispatching tests should have been performed before using this class in order to assure coherent test reports.

Author(s):
Emil Ivov Network Research Team, Louis Pasteur University, Strasbourg, France.
Version:
1.0
 
 
     private static long MESSAGE_QUEUE_EMPTIES_FOR = 500;
 
     private RequestCollector requestCollector = null;
 
     private ResponseCollector responseCollector = null;
 
     private TimeoutCollector timeoutCollector = null;
 
 
 
     private static Logger logger = Logger.getLogger(SipEventCollector.class);
 
     private void initCollectors(SipProvider sipProvider) {
         this. = new RequestCollector(sipProvider);
         this. = new ResponseCollector(sipProvider);
         this. = new TimeoutCollector(sipProvider);
                 sipProvider);
                 sipProvider);
     }

    
Collects the first javax.sip.RequestEvent fired by the specified provider. All subsequent events and events other than javax.sip.RequestEvent are ignored.

Parameters:
provider the provider that this collector should listen to
Throws:
java.util.TooManyListenersException in case the specified provider throws that exception. It is the responsibility of the calling class to encapsulate it with the corresponding error (e.g. TckInternalError, AssertFailure or TiUnexpectedError) and throw it in its turn to upper layers.
 
     public void collectRequestEvent(SipProvider provider)
             throws TooManyListenersException {
 
         initCollectors(provider);
         provider.addSipListener();
     }

    
Collects the first javax.sip.ResponseEvent fired by the specified provider. All subsequent events and events other than javax.sip.ResponseEvent are ignored.

Parameters:
provider the provider that this collector should listen to
Throws:
java.util.TooManyListenersException in case the specified provider throws that exception. It is the responsibility of the calling class to encapsulate it with the corresponding error (e.g. TckInternalError, AssertFailure or TiUnexpectedError) and throw it in its turn to upper layers.
    public void collectResponseEvent(SipProvider provider)
            throws TooManyListenersException {
        initCollectors(provider);
        provider.addSipListener();
    }

    
Collects the first javax.sip.TimeoutEvent fired by the specified provider. All subsequent events and events other than javax.sip.TimeoutEvent are ignored.

Parameters:
provider the provider that this collector should listen to
Throws:
java.util.TooManyListenersException in case the specified provider throws that exception. It is the responsibility of the calling class to encapsulate it with the corresponding error (e.g. TckInternalError, AssertFailure or TiUnexpectedError) and throw it in its turn to upper layers.
    public void collectTimeoutEvent(SipProvider provider)
            throws TooManyListenersException {
        initCollectors(provider);
        provider.addSipListener();
    }

    
Collects the first javax.sip.TransactionTerminated fired by the specified provider. All subsequent events and events other than javax.sip.TimeoutEvent are ignored.

Parameters:
provider the provider that this collector should listen to
Throws:
java.util.TooManyListenersException in case the specified provider throws that exception. It is the responsibility of the calling class to encapsulate it with the corresponding error (e.g. TckInternalError, AssertFailure or TiUnexpectedError) and throw it in its turn to upper layers.
    public void collectTransactionTermiatedEvent(SipProvider provider)
            throws TooManyListenersException {
        initCollectors(provider);
    }

    
Collects the first javax.sip.TransactionTerminated fired by the specified provider. All subsequent events and events other than javax.sip.TimeoutEvent are ignored.

Parameters:
provider the provider that this collector should listen to
Throws:
java.util.TooManyListenersException in case the specified provider throws that exception. It is the responsibility of the calling class to encapsulate it with the corresponding error (e.g. TckInternalError, AssertFailure or TiUnexpectedError) and throw it in its turn to upper layers.
    public void collectDialogTermiatedEvent(SipProvider provider)
            throws TooManyListenersException {
        initCollectors(provider);
    }

    
Returns the collected javax.sip.RequestEvent or null if no event has been collected. After this method is called the SipEventCollector will remove itself from the corresponding javax.sip.SipProvider and reset its internal state so that it could be reused.

Returns:
the collected javax.sip.RequestEvent
Throws:
test.tck.TckInternalError in case the method is called without first calling the collectRequestEvent
        return this.extractCollectedRequestEvent(true);
    }
    public RequestEvent extractCollectedRequestEvent(boolean toReset) {
        if ( == null)
            throw new TckInternalError(
                    "A request collect was attempted when the requestCollector was null");
        RequestEvent collectedEvent = .;
        if (toReset) {
            resetCollectors();
            try {
                Thread.sleep(200);
            } catch (InterruptedException ex) {
            }
        }
        return collectedEvent;
    }

    
Returns the collected javax.sip.ResponseEvent or null if no event has been collected. After this method is called the SipEventCollector will remove itself from the corresponding javax.sip.SipProvider and reset its internal state so that it could be reused.

Returns:
the collected javax.sip.ResponseEvent
Throws:
test.tck.TckInternalError in case the method is called without first calling the collectResponseEvent
        if ( == null)
            throw new TckInternalError(
                    "A response collect was attempted when the responseCollector was null");
        ResponseEvent collectedEvent = .;
        resetCollectors();
        try {
            Thread.sleep(200);
        } catch (InterruptedException ex) {
        }
        return collectedEvent;
    }

    
Returns the collected javax.sip.TimeoutEvent or null if no event has been collected. After this method is called the SipEventCollector will remove itself from the corresponding javax.sip.SipProvider and reset its internal state so that it could be reused.

Returns:
the collected javax.sip.TimeoutEvent
Throws:
test.tck.TckInternalError in case the method is called without first calling the collectTimeoutEvent
        if ( == null)
            throw new TckInternalError(
                    "A timeout collect was attempted when the timeoutCollector was null");
        TimeoutEvent collectedEvent = .;
        resetCollectors();
        return collectedEvent;
    }

    
Returns the collected javax.sip.DialogTerminatedEvent or null if no event has been collected. After this method is called the SipEventCollector will remove itself from the corresponding javax.sip.SipProvider and reset its internal state so that it could be reused.

Returns:
the collected javax.sip.TimeoutEvent
Throws:
test.tck.TckInternalError in case the method is called without first calling the collectTimeoutEvent
        if ( == null)
            throw new TckInternalError(
                    "A dialog-terminated collect was attempted when the dialogTerminationCollector was null");
        DialogTerminatedEvent collectedEvent = this..;
        resetCollectors();
        return collectedEvent;
    }

    
Returns the collected javax.sip.DialogTerminatedEvent or null if no event has been collected. After this method is called the SipEventCollector will remove itself from the corresponding javax.sip.SipProvider and reset its internal state so that it could be reused.

Returns:
the collected javax.sip.TimeoutEvent
Throws:
test.tck.TckInternalError in case the method is called without first calling the collectTimeoutEvent
        if ( == null)
            throw new TckInternalError(
                    "A timeout collect was attempted when the transactionTerminationCollector was null");
                .removeSipListener();
        resetCollectors();
        return collectedEvent;
    }
    private void resetCollectors() {
        
sipProvider = null; requestCollector = null; responseCollector = null; timeoutCollector = null; dialogTerminationCollector = null; transactionTerminationCollector = null;
        // Wait for the message queue to go empty
        MessageFlowHarness.sleep();
    }
    private void assertInitialState(Class eventClass) {
        boolean failureFlag = false;
        if (eventClass.equals(RequestEvent.class)) {
            if ( != null
                    && . != null) {
                failureFlag = true;
            }
        } else if (eventClass.equals(ResponseEvent.class)) {
            if ( != null
                    && . != null) {
                failureFlag = true;
            }
        } else if (eventClass.equals(TimeoutEvent.class)) {
            if ( != null
                    && . != null) {
                failureFlag = true;
            }
        } else if (eventClass.equals(TransactionTerminatedEvent.class)) {
            if (this. != null
                    && this.. != null)
                failureFlag = true;
        } else if (eventClass.equals(DialogTerminatedEvent.class)) {
            if (this. != null
                    && this.. != null)
                failureFlag = true;
        }
        if (failureFlag)
            throw new TckInternalError(
                    "Attempting to start a collect operation "
                            + "on a collector that is not in initial state!");
    }
    // ========================= COLLECTOR
    // ADAPTERS==============================
    private class RequestCollector implements SipListener {
        private RequestEvent collectedEvent = null;
        private SipProvider provider;
        public RequestCollector(SipProvider sipProvider) {
            this. = sipProvider;
        }
        public void processRequest(RequestEvent evt) {
            // Ignore secondary and null events
            if ( != null || evt == null)
                return;
             = evt;
        }
        public void processResponse(ResponseEvent responseEvent) {
        }
        public void processTimeout(TimeoutEvent timeoutEvent) {
        }
        public void processIOException(IOExceptionEvent exceptionEvent) {
            // TODO Auto-generated method stub
            .debug("processIOException");
        }
        public void processTransactionTerminated(
                TransactionTerminatedEvent transactionTerminatedEvent) {
            // TODO Auto-generated method stub
            .debug("processTransactionTerminated");
        }
        public void processDialogTerminated(
                DialogTerminatedEvent dialogTerminatedEvent) {
            // TODO Auto-generated method stub
            .debug("processDialogTerminated");
        }
    }
    private class ResponseCollector implements SipListener {
        private ResponseEvent collectedEvent = null;
        private SipProvider provider;
        public ResponseCollector(SipProvider sipProvider) {
            this. = sipProvider;
        }
        public void processRequest(RequestEvent requestEvent) {
        }
        public void processResponse(ResponseEvent evt) {
            // Ignore secondary and null events
            if ( != null || evt == null)
                return;
             = evt;
        }
        public void processTimeout(TimeoutEvent timeoutEvent) {
            .debug("processTimeout");
        }
        public void processIOException(IOExceptionEvent exceptionEvent) {
            .error("processIOException");
        }
        public void processTransactionTerminated(
                TransactionTerminatedEvent transactionTerminatedEvent) {
            .info("transaction terminated event recieved");
        }
        public void processDialogTerminated(
                DialogTerminatedEvent dialogTerminatedEvent) {
            .info("processDialogTerminated");
        }
    }
    private class TransactionTerminationCollector implements SipListener {
        private SipProvider provider;
        public TransactionTerminationCollector(SipProvider sipProvider) {
            this. = sipProvider;
        }
        public void processRequest(RequestEvent requestEvent) {
        }
        public void processResponse(ResponseEvent responseEvent) {
        }
        public void processTimeout(TimeoutEvent timeoutEvent) {
        }
        public void processIOException(IOExceptionEvent exceptionEvent) {
        }
        public void processTransactionTerminated(
                TransactionTerminatedEvent transactionTerminatedEvent) {
            this. = transactionTerminatedEvent;
        }
        public void processDialogTerminated(
                DialogTerminatedEvent dialogTerminatedEvent) {
        }
    }
    private class DialogTerminationCollector implements SipListener {
        private DialogTerminatedEvent collectedEvent;
        private SipProvider provider;
        public DialogTerminationCollector(SipProvider sipProvider) {
            this. = sipProvider;
        }
        public void processRequest(RequestEvent requestEvent) {
        }
        public void processResponse(ResponseEvent responseEvent) {
        }
        public void processTimeout(TimeoutEvent timeoutEvent) {
        }
        public void processIOException(IOExceptionEvent exceptionEvent) {
        }
        public void processTransactionTerminated(
                TransactionTerminatedEvent transactionTerminatedEvent) {
        }
        public void processDialogTerminated(
                DialogTerminatedEvent dialogTerminatedEvent) {
            this. = dialogTerminatedEvent;
        }
    }
    private class TimeoutCollector implements SipListener {
        private TimeoutEvent collectedEvent = null;
        private SipProvider provider;
        public TimeoutCollector(SipProvider sipProvider) {
            this. = sipProvider;
        }
        public void processRequest(RequestEvent requestEvent) {
            .processRequest(requestEvent);
        }
        public void processResponse(ResponseEvent responseEvent) {
        }
        public void processTimeout(TimeoutEvent evt) {
            // Ignore secondary and null events
            if ( != null || evt == null)
                return;
             = evt;
        }
        public void processIOException(IOExceptionEvent exceptionEvent) {
            // TODO Auto-generated method stub
            .info("processIOException");
        }
        public void processTransactionTerminated(
                TransactionTerminatedEvent transactionTerminatedEvent) {
            // TODO Auto-generated method stub
            .info("processTransactionTerminated");
        }
        public void processDialogTerminated(
                DialogTerminatedEvent dialogTerminatedEvent) {
            // TODO Auto-generated method stub
            .info("processDialogTerminated");
        }
    }
New to GrepCode? Check out our FAQ X