package utility;

public class ConnectionHandler implements Runnable
{
	public  AgentIdentity id;
	public  ServerHandler svrHandler = null;
  	public  Thread t = null;
	public volatile boolean stop = false;

	public ConnectionHandler(AgentIdentity id , ServerHandler sh)
	{
    		this.id = id;
    		svrHandler = sh;
  		t=new Thread(this);
		t.start();
	}
  	public void run()
	{
    		System.out.println("Starting connection  handler   " );
    		while (!stop)
		{
      			try
			{
        			Request req = svrHandler.readRequest(id);
        			if(req != null)
				{
					//System.out.println("Got a request from connection " + connectionId);

					svrHandler.doThis(req,id);
      				}
			}
      			catch (Exception e)
			{
				e.printStackTrace();
				System.out.println(e + " : closing it(4)");
				svrHandler.removeConnection(id);
				break;
			}

		}
  		System.out.println("stopping connection handler  ");
	}
	public void setId(AgentIdentity id)
	{
		this.id = id ;
	}
}
