/* ***************************************************************************+
 * 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.*;

/**
 * Takes care of servicing incoming calls from the phone.  Sets up a connection with a computer if a call
 * has been accepted.  Services the next incoming phone call when the connection ends or the previous
 * call has been rejected.
 * 
 * @author James Wann
 * @version 1.0a
 */
public class ListenChannel extends Thread
{
	/**
	 * The Line instance corresponding to ListenChannel's line.
	 */
	private Line myLine;
	
	/**
	 * 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>ListenChannel's</code> line.
	 * Provides information concerning that line.
	 * @param cc the <code>ChannelControl</code> instance for controlling <code>ListenChannel's</code>
	 * primary channel.
	 * @param g the <code>Gateway</code> instance to access line state.
	 * @see Line
	 * @see Gateway
	 * @see cnrg.itx.datax.Connection
	 */
	protected ListenChannel(Line lin, ChannelControl cc, Gateway g)
	{
		myLine = lin;
		myControl = cc;
		myGate = g;
	}
	
	/**
	 * Puts the <code>ListenChannel</code> to "sleep".  Used when a connection is being set up.
	 * @see Gateway#setUpConnection(Line, Connection)
	 */
	protected synchronized void makeWait()
	{
		try
		{
			wait();
		}
		catch (InterruptedException e)
		{
		}
	}
	
	/**
	 * "Wakes up" the <code>ListenChannel</code> to listen for the next incoming call.
	 */
	protected synchronized void restart()
	{
		notify();
	}
	
	/**
	 * @see java.lang.Runnable
	 */
	public void run()
	{
		int chDev = myLine.getChDev();
		int index = myLine.getLineNumber() - 1;
		String extension = new String();					// an extension number
		Connection isApproved = null;						// data connection if call is approved
		String errorString = new String("<GatewayException> :: Error: No record found");
		
		while (true)
		{
			System.out.println("<Channel: " + myLine.getLineNumber() + "> waiting for a call.");
			
			myControl.waitForCall(chDev);
			
			// Try to reserve ListenChannel's corresponding line.  If so, get the extension number.
			if (myGate.useLine(myLine))
			{
				System.out.println("<Channel: " + myLine.getLineNumber() + "> leave your extension number.");
				extension = myControl.getExtension(chDev, false);
				isApproved = null;
				
				// Dial up extension number
				while (isApproved == null)
				{
					try
					{
						isApproved = myGate.dialComputer(myLine, extension);
					}
					catch (GatewayException e)
					{
						System.out.println("<Channel: " + myLine.getLineNumber() + "> -> " + e.getMessage());
						
						// If number is not in the directory then get another extension number
						if (e.getMessage().equals(errorString))
							extension = myControl.getExtension(chDev, true);
						else
						{
							// If call could not go through, hang up and listen for another call
							if (extension != null)
								myControl.rejectCallMessage(chDev);
							myGate.freeLine(myLine);
							break;
						}
					}
				}
				
				// set up connection if extension number is valid
				if (isApproved != null)
					myGate.setUpConnection(myLine, isApproved);
			}
		}
	}
}