/*
 * Created on Jan 19, 2005
 *
 */
package thegroup.projectcddb.server;

/**
 * @author Alan Savage
 *
 */
import java.net.*;
import java.io.*;
import java.util.StringTokenizer;

/**
 * a thread that deals with a freeDB client
 */
final class MultiClientThread extends Thread {
	private Socket socket = null;
	private BufferedReader clientInput = null;
	private PrintWriter serverOutput = null;
	private StringTokenizer parsedResponse = null;
    private ServerProtocol serverProtocol = null;
	// We should instantiate a ServerProtocol object here
	
	/**
	 * creates a new MultiClientThread for dealing with a freeDB client
	 * @param socket Socket on which the freeDB client is connected
	 * @param name name of thread
	 */
	MultiClientThread(Socket socket, String name) {
		super(name);
		this.socket = socket;
                setDaemon(true);
	}
	
	/**
	 * deals with freeDB client when start() is called
	 */
	public void run() {
                MultiServer.log.info("Connected to: " + socket.getInetAddress());
                System.out.println("Connected to: " + socket.getInetAddress());
                
		// gets the input and output streams from the socket and init ServerProtocol
		getIOStreams();
		serverProtocol = new ServerProtocol();
                
		// get command from user and respond according to protocol
		String clientCommand;
		String serverResponse = null;
		try {
                        String banner = serverProtocol.getBanner();
                        serverOutput.println(banner);
                        MultiServer.log.finest(banner);
			while(true) {
                            for(int i=0; i < 100000; i++);
                            clientCommand = clientInput.readLine();
                            MultiServer.log.finest(clientCommand);
                            serverResponse = serverProtocol.processInput(clientCommand);
                            parsedResponse = new StringTokenizer(serverResponse, "\n");
                            while(parsedResponse.hasMoreTokens()) {
                            	serverResponse = parsedResponse.nextToken();
	                            serverOutput.println(serverResponse);
	                            for(int i=0; i < 100000; i++);
	                            MultiServer.log.finest(serverResponse);
                            }
                            if(clientCommand.equals("quit")) break;
			} // end while
		} catch(IOException e) {
			MultiServer.log.warning("Unable to receive input from client at " + socket.getInetAddress());
		}
		catch(Exception e){}
		
		// close IO Streams and socket
		close();
                System.out.println("Disconnected from: " + socket.getInetAddress());
		
	} // end run
	
	private void getIOStreams() {
		try {
			clientInput = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			serverOutput = new PrintWriter(socket.getOutputStream(), true);
		} catch(IOException e) {
			MultiServer.log.warning("Unable to open I/O Streams to " + socket.getInetAddress());
		}
	} // end getIOStreams
	
	private void close() {
		try {
			clientInput.close();
			serverOutput.close();
			socket.close();
		} catch(IOException e) {
			MultiServer.log.warning("Unable to close I/O Streams or connection at " + socket.getInetAddress());
		}
	} // end close
} // end MultiClientThread
