// Melvin K Lew <melvin@columbia.edu>
// $Source: /amd/cheers-fast/root/np/melvin/AIS/aisproj/RCS/WBSListenThread.java,v $
// $Date: 1998/04/27 00:44:30 $
// $Author: melvin $
// $Revision: 1.8 $
//
// CS-E6998-03 Advanced Internet Services: Term Project
//
// The JINCS Project: Collaborative Whiteboard Client/Server Application
//
// Group:
//	Johan Andersen <johan@columbia.edu>
//	Richard Denmark <thor@columbia.edu>
//	Melvin Lew <melvin@columbia.edu
//
// A multi-threaded networked Whiteboard Server
//

import java.io.*;
import java.net.*;
import java.util.*;
import WBCListenThread.*;

/**
 * Extends Thread objects, listens for new clients, and spawns threads
 * to handle each client.
 *
 * @author	Melvin K Lew
 * @version	$Revision: 1.8 $, $Date: 1998/04/27 00:44:30 $
 *
 * @see		WBCListenThread
 * @see		WBServer
 */
public class WBSListenThread extends Thread
{
  private Hashtable clienthash = null;
  private boolean listening = true;
  private int port = 1998;
  private ServerSocket serverSocket = null;

  /**
   * Class constructor.
   *
   * @param	clienthash	a hashtable containing client information
   * @param	port		an integer port number to listen to
   *
   * @see	WBServer
   */
  public WBSListenThread(Hashtable clienthash, int port)
  {
    super("WBSListenThread");
    this.clienthash = clienthash;
    this.port = port;
  }

  /**
   * The execution part of this thread that runs until interrupted.
   * Spawn a new WBCListenThread to handle I/O for each client connection.
   *
   * @see	WBCListenThread
   */
  public void run()
  {
    // open the server listening socket at port
    try {
      serverSocket = new ServerSocket(port);
    }
    catch (IOException e) {
      System.err.println("error: cannot listen on port: " + port);
      System.exit(-1);
    }

    // for each new connection we see on the server listening port,
    // spawn a new thread to connect to the client and listen to it.
    try {
      while ( this.listening )
	new WBCListenThread(clienthash, serverSocket.accept()).start();

      serverSocket.close();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }
}
