/* ***************************************************************************+
 * 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.*;
import cnrg.itx.datax.devices.*;
import java.net.*;

/**
 * Takes care of sending voice data from the primary channel from the phone to the computer network.
 * 
 * @author James Wann
 * @version 1.0a
 */
public class RecordChannel implements Runnable
{
	/**
	 * The Line instance corresponding to RecordChannel's line.
	 */
	private Line myLine;
	
	/**
	 * The Connection instance needed for data transfer.
	 */
	private Connection myConnection;
	
	/**
	 * The ChannelControl instance for controlling the primary channel.
	 */
	private ChannelControl myControl;
	
	/**
	 * The Gateway instance.
	 */
	private Gateway myGate;
	
	/**
	 * @param lin the <code>Line</code> instance corresponding to <code>RecordChannel's</code> line.
	 * Contains information regarding the line.
	 * @param c the <code>Connection</code> instance needed for data transfer with the computer network.
	 * @param cc the <code>ChannelControl</code> object responsible for controlling the primary channel.
	 * @param g the <code>Gateway</code> instance.  Needed to access line state.
	 * @see Line
	 * @see ChannelControl
	 * @see Gateway
	 * @see cnrg.itx.datax.Connection
	 */
	protected RecordChannel(Line lin, Connection c, ChannelControl cc, Gateway g)
	{
		myLine = lin;	
		myConnection = c;
		myControl = cc;
		myGate = g;
	}
	
	/**
	 * @see java.lang.Runnable
	 */
	public void run()
	{
		String s = new String();							// the string returned by recordPacket
		boolean isDTMF = true;								// if termination condition is a DTMF digit
		int myLineNumber = myLine.getLineNumber();
		int myChDev = myLine.getChDev();

		Channel myChannel = myConnection.getOutputChannel();
		RecordSource mySource = (RecordSource)myChannel.getSource();
	
		while (isDTMF == true)
		{
			s = myControl.recordPacket(mySource, myChDev, myLineNumber);
			
			// Hangup if connection is being terminated
			if (s.equals("HANGUP"))
			{
				try
				{
					myGate.hangup(myLine);
				}
				catch (GatewayException e)
				{
					System.out.println("<RecordChannel: " + myLine.getLineNumber() + "> -> " + e.getMessage());
				}
				myGate.tearDownPlayConnection(myLine);
				isDTMF = false;
			}
				
			// Otherwise, DTMF digits are being sent.  Send them.
			else
			{
				try
				{
					myGate.sendDTMF(s, myLine);
				}
				catch (GatewayException e)
				{
					System.out.println("<RecordChannel: " + myLine.getLineNumber() + "> -> " + e.getMessage());
				}
			}
		}
	}
}