/**
 * @(#)JavaCGIBridgeNotify.java 2.00 07/18/98
 *
 * Copyright Info: This class was written by Gunther Birznieks
 * gunther@clark.net having been inspired by countless other
 * software developers.
 *
 * Version 1.00: 08/10/97
 * Version 2.00: 07/19/98
 *
 * The second version of this software was written for
 * independent study under the guidance of Professor
 * Marty Hall as part of the Johns Hopkins University
 * Computer Science Master's Degree program.
 *
 * Feel free to copy, cite, reference, sample, borrow, resell
 * or plagiarize the contents.  However, if you don't mind,
 * please let me know where it goes so that I can at least
 * watch and take part in the development of the memes. Information
 * wants to be free; support public domain freeware.
 *
 * Donations are appreciated.  If you use this software please
 * consider making a donation to the Electronic Frontier Foundation
 * http://www.eff.org/ or another organization of your choice
 * dedicated to defending liberty on the net.
 *
 */

package com.extropia.net;

import java.util.Vector;

/**
 * This class is sent to Observers of the JavaCGIBridge whenever
 * more data has been received by the JavaCGIBridge or the 
 * HTTP response has completed.
 * <P>
 * It is also sent when the JavaCGIBridge recognizes that a
 * timeout has occured. Since threads cannot directly throw
 * an exception to observers, this is the thread's mechanism
 * for performing this notification.
 *
 * @version     2.00, 18 Jul 1998
 * @author      Gunther Birznieks
 * @author      Marty Hall
 * <P>
 */
public class JavaCGIBridgeNotify {

    /**
     * _data is an object which contains the data
     * being sent from the JavaCGIBridge class. It will
     * either be a Vector of Vectors for parsed data,
     * a byte array for raw data, or a JavaCGIBridgeTimeOutException
     * object for timeout notifications.
     */
    private Object _data;

    /**
     * _endData is a flag indicating whether this message
     * was sent in the middle of receiving the HTTP response
     * or at the end of the whole data transmission.
     */
    private boolean _endData;

    /**
     * Constructs the object with the data object and the
     * flag indicating whether we are at the end of the
     * HTTP response.
     */
    public JavaCGIBridgeNotify(Object data, boolean endData) {
      _data = data;
      _endData = endData;
    } // end of Constructor
     
    /**
     * Returns true if the JavaCGIBridge object has reached the
     * end of the HTTP response.
     *
     * @return boolean Flag indicating whether the end of HTTP
     *                 response was set.
     */
    public boolean isAtEndOfData() { return _endData; }

    /**
     * Returns true if the JavaCGIBridge object passed the time limit
     * for receiving data.
     *
     * @return boolean Flag indicating whether the JavaCGIBridge
     *                 timed out.
     */ 
    public boolean isTimedOut() {
      if (_data instanceof JavaCGIBridgeTimeOutException) {
        return true;
      } else {
        return false;
      }
    } // end of isTimedOut()

    /**
     * Returns parsed Vector data if
     * parsed data is expected to be returned.
     * <P>
     * null will be returned if the data inside this object
     * is not a Vector.
     *
     * @return Vector containing parsed data from JavaCGIBridge 
     */
    public Vector getParsedData() {
        if (_data instanceof Vector) {
            return (Vector)_data;
        } else {
            return null;
        }
    } // end of getVectorData()

    /**
     * Returns an array of bytes for the raw data sent so far
     * if raw data is expected to be received.
     * <P>
     * null will be returned if the data inside this object
     * is not a byte array.
     * 
     * @return byte [] containing raw data from JavaCGIBridge
     */
    public byte [] getRawData() {
        if (_data instanceof byte[]) {
            return (byte[])_data;
        } else {
            return null;
        }
    } // end of getRawData()

    /**
     * Returns the object
     * that was sent in this notification object
     * regardless of whether it is a Vector, byte 
     * array, or a JavaCGIBridgeTimeOutException
     * object.
     *
     * @return Object the data sent from JavaCGIBridge
     */
    public Object getData() { return _data; }

} // end of JavaCGIBridgeNotify class
