/*This code written by Nishant Agarwal
  Jul 12th, 2004
  nishant@purecode.us
*/

package server;

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

public class MyThread implements Runnable {
    Thread t;
    Socket sock;

    public MyThread(Socket s) {
        sock = s;
        t = new Thread(this);
    }

    public void run() {
        //declare resources we need
        BufferedReader in = null;
        PrintWriter out = null;
        
        //initialize the resources
        try {
            in  = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            out = new PrintWriter(sock.getOutputStream(), true);
	} catch (IOException e) {
            System.err.println("Could not create streams in server thread: " +
                               e.getMessage());
            e.printStackTrace();
	} 

	//talk according to protocol
        Protocol protocol = new MyProtocol(in, out);
        try {
            protocol.serverAddReply();
	} catch (IOException e) {
            System.err.println("Error in talking to client in server thread: " +
                               Thread.currentThread().toString());
            e.getMessage();
            e.printStackTrace();
	} catch (NumberFormatException e) {
            System.err.println("Client did not give integers: " +
                               e.getMessage());
	}

        //close the resources
        try {
            in.close();
            out.close();
            sock.close();
        } catch (IOException e) {
            System.err.println("Server Thread could not clean up: " +
                               e.getMessage()); 
        }  
    }
}
