package net108.chat;

import java.io.*;
import net108.util.*;


/**
 * Represents the client of a simple chat system, accepts user input
 * that is sent to the server to be broadcast to any other chat
 * clients currently running.
 */
public class Client extends net108.util.Client
{
    /**
     * Create a client that connects to a server already running on
     * given hostName.
     */
    public Client (String hostName, int port)
    {
        super(hostName, port);
        addInputHandler(new net108.util.PrintInputHandler());
    }

    /**
     * A generic read loop for client.
     */
    public void chatLoop ()
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        while (true)
        {
            System.out.print(">> ");
            System.out.flush();
            try
            {
                send(in.readLine());
            }
            catch (IOException e)
            {}
        }
    }

    /**
     * Starts the client up.
     */
    public static void main (String[] args)
    {
        String hostname = Constants.HOST;
        if (args.length > 0)
        {
            hostname = args[0];
        }
            
        new Client(hostname, Constants.PORT).chatLoop();
    }
}
