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

public class NbDataServerHandler extends DataServerHandler
{
	Query query = new Query();
	public NbDataServerHandler()
	{
		System.out.println("starting nb data server handler ...");
	}

	public boolean sendStream(String fileName, int cid) throws IOException
  	{
    		boolean success = true;
    		Connection conn = getConnection(cid);
    		System.out.println("Waiting for connection to be establish");
		while(conn == null)
		{
			conn = getConnection(cid);
		}
		if (conn != null)
		{
      			System.out.println("Sending file :"+fileName);
        		synchronized (conn.out)
			{
         			//System.out.println("Got lock on connection " + cid);
         			BufferedOutputStream buffOut = null;
				try
				{
					buffOut = new BufferedOutputStream(conn.out);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				try
				{
					File f = new File(fileName);
					byte buff[] = new byte [40];
					FileInputStream inFileStream = new FileInputStream (f);
					BufferedInputStream inBuffStream = new BufferedInputStream(inFileStream);
					//BufferedOutputStream outBuffStream = new BufferedOutputStream (cli.getOutputStream());
					int next =0;
					while((next =( inBuffStream.read ( buff,0,buff.length ) )) != -1)
					{
						//System.out.print(new String(buff,0,next));
						buffOut.write(buff,0,next);
						buffOut.flush();
					}
					buffOut.close();
					System.out.println("closing buffered stream");
					removeConnection(cid);
					conn.in.close();
					conn.out.close();
				}
				catch (Exception e)
				{
					success = false;
					e.printStackTrace();
				}
			//if(success)
			//	System.out.println("writeObject successfull");
			}
		}

		return success;
	}

	
	public boolean readStream(AgentIdentity id, String fileName) throws IOException
	{
    		boolean result = false;
		File file = new File(fileName);
		Connection conn = getConnection(id.getId());
    		System.out.println("waiting for connection to be established");

		while(conn == null)
		{
			conn = getConnection(id.getId());
		}
		System.out.println("client connected");
		if (conn != null)
		{
      			ClientHeader receivedRequest = null;
			synchronized (conn.in)
			{
          			FileOutputStream fileOut = new FileOutputStream(file);
				BufferedInputStream buffIn = null;
				BufferedOutputStream buffout = new BufferedOutputStream(fileOut);
				try
				{
					buffIn = new BufferedInputStream(conn.in);
					byte buff[] = new byte[40];
					int next =0;
					System.out.println("File input from Agent :" +id.getLoginName());
					while((next = buffIn.read(buff,0,buff.length)) != -1)
					{
						buffout.write(buff,0,buff.length);
					//	System.out.print(new String(buff,0,next));
					}
					result = true;
					buffout.flush();
					buffIn.close();
					System.out.println("closing buffered stream");
					removeConnection(id);
					conn.in.close();
					conn.out.close();

				}
				catch (StreamCorruptedException e)
				{
					System.out.println(e + " : closing it(1)");
					removeConnection(id);
					result = false;
				}


				catch (java.lang.NullPointerException e)
				{
					System.out.println( e + " : closing it(3)");
					removeConnection(id.getId());
					result = false;
				}
				catch (SocketException e)
				{
					e.printStackTrace();
					removeConnection(id);
				}
				catch (Exception e)
				{
					e.printStackTrace();
					removeConnection(id.getId());
					result = false;
				}
			}
		}
		return result;
	}


}
