import java.io.*;

/**
 * This thread reads messages from the server and sends them to the
 * dispatcher in the ClientSocket class.
 *
 * @author	Thor Denmark
 * @version	$Revision: 1.5 $	$Date: 1998/05/04 22:53:06 $
 *
 * @exception	
 *
 * @see		Thread
 * @see		ClientSocket
 */
class ClientReader extends Thread {
  ClientSocket d_socket;

  /**
   * Create a ClientReader for a ClientSocket.
   *
   * @param	c	The ClientSocket for which this will be the Reader
   *
   * @see	ClientSocket
   */
  public ClientReader(ClientSocket c) {
    super("Client Reader");
    this.d_socket = c;
  }

  /**
   * Starts the ClientReader thread.
   *
   * @see	String
   * @see	BufferedReader
   */
  public void run() {
    BufferedReader d_in = null;
    String d_line;

    try {
      d_in =
	new BufferedReader(new InputStreamReader(d_socket.getInputStream()));

      while(true) {
	d_line = d_in.readLine();

	if(d_line == null) { 
	  System.out.println("Server closed connection.");
	  break;
	}

	// THOR -- debugging
	//System.out.println("======== incoming message ========");
	//System.out.println(d_line);
	//System.out.println("==================================");

	// dispatch the incoming command to the ClientSocket
	d_socket.dispatch(d_line);
      }
    }
    catch(IOException e) {
      System.out.println("ClientReader: " + e);
    }
    finally {
      try {
	if(d_in != null)
	  d_in.close();
      }
      catch(IOException e) {
	System.out.println("ClientReader: " + e);
      }
    }
    System.exit(0);
  }
}
