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

import java.net.*;
import java.io.*;

import cnrg.itx.signal.SignalEvent.*;

/**
 * This class is the server thread in all DesktopSignaling components.  The server spawns a new thread
 * to handle each new request.
 */
class DesktopSignalingServer extends Thread
{	
	/** Listening Socket **/
	private ServerSocket recvSock = null;
	/** Listening port **/
	private int myPort = -1;
	/** Accepting Socket **/
	private Socket rcvS = null;
	/** Number of requests served **/
	private int rNum = 0;
	/** SigPacketHandler handle **/
	private SigPacketHandler mySPH = null;	
	/** DesktopSignaling handle **/
	private DesktopSignaling myDS = null;
	/** To run or not to run **/
	private boolean flag = true;
	
	public DesktopSignalingServer(DesktopSignaling ds){
		myDS = ds;
		try{
			recvSock = new ServerSocket(0, 20);
			myPort = recvSock.getLocalPort();
			myDS.setServerPort(myPort);	
			System.out.println("DesktopSignalingServer listening on port-> "+myPort+"\n");
		}
		catch(IOException ioe){
		}
	}
	
	/**
	 * Method called when the thread is started.
	 * 
	 * @param   None.
	 * @return  void
	 */
	public void run(){
		
		while(flag){
			try{
				System.out.println("Ready to accept requests...");
				rcvS = recvSock.accept();
				rcvS.setSoTimeout(myDS.TIMEOUT);

				System.out.println("Received something...");
				rNum++;
				mySPH = new SigPacketHandler(rcvS, rNum, myDS);
				mySPH.start();										
			}
			catch (SocketException se){
			}
			catch(IOException ioe){
			}
		}
		System.out.println("DesktopSignalingServer Thread exiting on Port: " + myPort);
	}
	
	/**
	 * This method kills the Server thread by closing the listening socket and exiting the run() method
	 * 
	 * @param   None.
	 * @return  void
	 */
	public void killServer(){
		flag = false;
		try{
			recvSock.close();
		}
		catch(IOException ioe){
			System.out.println("DesktopSignalingServer thread shut down by the user...");
		}
	}
}