/* ***************************************************************************+
 * ITX package (cnrg.itx) for telephony application programming.              *
 * Copyright (c) 1999  Cornell University, Ithaca NY                          *
 *                                                                            *
 * This program is free software; you can redistribute it and/or modify       *
 * it under the terms of the GNU General Public License as published by        *
 * the Free Software Foundation, either version 2 of the License, or (at      * 
 * your option) any later version.                                            *
 *                                                                            *
 * The ITX package is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY *
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   *
 * for more details.                                                          * 
 *                                                                            *
 * A copy of the license is distributed with this package.  Look in the docs  *
 * directory, filename GPL.                                                   *
 *                                                                            * 
 * Contact information:                                                       *
 * Donna Bergmark                                                             *
 * 484 Rhodes Hall                                                            *
 * Cornell University                                                         *
 * Ithaca, NY 14853-3801                                                      *
 * bergmark@cs.cornell.edu                                                    *
 ******************************************************************************/

package cnrg.itx.gtwy;
import cnrg.itx.datax.*;

/**
 * The user-defined destination of voice data packets from the computer network to the gateway.
 * 
 * @author James Wann
 * @version 1.0a
 * @see PlayChannel
 * @see cnrg.itx.datax.Destination
 */
public class PlayDestination implements Destination
{
	/**
	 * The Line instance related to the destination.
	 */
	private Line myLine;
	
	/**
	 * The ChannelControl instance for calling native code.
	 */
	private ChannelControl myControl;
	
	/**
	 * true if destination is open for receiving packets; false otherwise.
	 */
	private boolean isOpen;
	
	/**
	 * true if the destination has to be muted; false otherwise.
	 */
	private boolean isMute;
	
	/**
	 * The number of bytes written to the buffer.
	 */
	private int bytesWritten = 0;
	
	/**
	 * The number of packets written.  Size specified as transfer size to the phone.
	 */
	private int iNumPackets = 0 ;
	
	/**
	 * @param l the <code>Line</code> instance related to <code>PlayDestination</code>.
	 * @param cc the <code>ChannelControl</code> instance for calling native code.
	 * @see Line
	 * @see ChannelControl
	 */
	public PlayDestination(Line l, ChannelControl cc)
	{
		myLine = l;
		myControl = cc;
		isOpen = true;
		isMute = false;
	}
	
	/**
	 * @return the <code>Line</code> instance for <code>PlayDestination</code>
	 * @see Line
	 */
	public Line getLine()
	{
		return myLine;
	}	
	
	/**
	 * Used to close the destination to receiving voice packets.
	 */
	public void close()
	{
		isOpen = false;
	}
		
	/**
	 * Used to mute the voice data sent to the destination.
	 * 
	 * @param mode true if the destination has to be muted; false otherwise.
	 * @return true if the destination has been muted; false otherwise.
	 */
	public boolean mute(boolean mode)
	{
		isMute = mode;
		return isMute;
	}
		
	/**
	 * Writes voice data to a specified buffer on the gateway.  Data will be played out on a phone later
	 * on.
	 * 
	 * @param data the voice data.
	 */
	public void write(byte[] data) throws DataException
	{
		if (isOpen && !isMute)
		{
			int lineNumber = myLine.getLineNumber();
			
			myControl.writeToPlay(lineNumber, data);
			bytesWritten += data.length;
			iNumPackets++;
		}
	}
	
	/**
    * The Gateway is not concerned about properties.  Returns null.
    */ 
   public PropertiesCollection getProperties() throws DataException
   {
	   return null;
   }

   /**
    * No properties are set.
    */
   public void setProperties(PropertiesCollection pc) throws DataException
   {
   }

   /**
    * The Gateway does not set peer properties.
    */
   public void setPeerProperties(PropertiesCollection pc) throws DataException
   {
   }

   /**
	* @return the Stats object which describes the current state of the destination.  
	*/
   public Stats getStatistics()
   {
	   Stats networkStats = new Stats();
	   int lineNumber = myLine.getLineNumber();
		
	   networkStats.addStat("Device", "Gateway Line Destination");
	   networkStats.addStat("<Gateway Line Destination " + lineNumber + "> (Audio) Bytes sent to wire", new Integer(bytesWritten));
	   networkStats.addStat("<Gateway Line Destination " + lineNumber + "> Packets sent to wire ", new Integer(iNumPackets));

	   return networkStats; 
   }
}