package utility;
import java.util.*;
import java.io.*;
import java.net.*;

public abstract class DataClientHandler  implements Runnable
{

	public Thread t = null;
  	protected Connection conn = null;
  	volatile boolean stop = false;


  	public DataClientHandler()
	{

		t=new Thread(this);
		t.start();

	}
  	public DataClientHandler(InputStream in, OutputStream out)
  	{
    		conn = new Connection(in, out);
  		t=new Thread(this);
		//t.start();

	}
  	public Connection getConnection()
	{
		return conn;
	}


	synchronized public void  removeConnection()
	{
    		//stop = true;
		/*if some problem later comment code below*/
		synchronized (conn)
		{
			try
			{
				System.out.println("closing data clint handler");
				conn.in.close();
				conn.out.close();
			}
			catch(Exception e)
			{
				System.out.println("cudn't close connection :" +e);
			}
		}
	}


	public  abstract boolean sendStream(String fileName) throws IOException;
  	public abstract boolean readStream() throws IOException;


	public void run()
	{
    		System.out.println("Starting data connection  handler...  " );
    		//while (!stop)
		{
      			try
			{
        			boolean result = readStream();
        			if(result == true)
				{
					System.out.println("Reading successful");

      				}
      			}


			catch (Exception e)
			{
				System.out.println(e);
				e.printStackTrace();
				removeConnection();
				//break;
			}
    		}
  		System.out.println("Stopping connection  handler for connection  " );
	}


}
