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

public class NbDataServer implements Runnable
{
	NbDataServerHandler srvHandler = new NbDataServerHandler();
	//NbDataServerHandler dataSrvHandler = new NbDataServerHandler();
	Query query = new Query();
	ServerSocket socket = null;
	int port = 7050;
	Thread t = null;
  	public NbDataServer(int port)
	{
		//srvHandler.addDataServer(dataSrvHandler);
		this.port = port;
  		t=new Thread(this);
		t.start();
	}
	public void run()
	{
		try
		{
      			socket = new ServerSocket(port);
    		}
    		catch (IOException e)
		{
      			System.out.println("Failed to bind to port " + port);
      			return;
    		}
		System.out.println("noticeBoard DataServer running on port " + port);
		while (true)
		{
			try
			{
        			Socket clientConn = socket.accept();
				System.out.println("got socket to client for data server ");
				AgentIdentity id =  getId(clientConn);
        			boolean success =authenticate(id);
				if(success)
				{
					srvHandler.addConnection(id, clientConn.getInputStream(), clientConn.getOutputStream());
        				//dataSrvHandler.addConnection(id, clientConn.getInputStream(), clientConn.getOutputStream());
					System.out.println("Got new connection...to nb Data Server from"+id.getId());
				}
      				else
				{
					clientConn.close();

				}
			}
      			catch (Exception e)
			{
				e.printStackTrace();
			}
    		}
	}

  	public AgentIdentity newClientAgent()
	{
    		int id = srvHandler.nextConnectionId();
    		AgentIdentity i = new AgentIdentity(id);
    		return i;
  	}

  	public boolean remove(AgentIdentity id)
	{
    		int aId = id.getId();
    		boolean success = srvHandler.removeConnection(aId);
    		return success;
 	}

	AgentIdentity getId(Socket clientConn)
	{

		AgentIdentity id = null;
		try
		{
			
			Request req = null;
			ClientHeader receivedRequest = null;
			InputStream in = clientConn.getInputStream();
			OutputStream out =clientConn.getOutputStream();
			ObjectInputStream objIn = null;
			objIn = new ObjectInputStream(in);
			receivedRequest = (ClientHeader)objIn.readObject();
			req = receivedRequest.getRequest();
			if(req.getRequestType().compareTo("Add") == 0)
			{
				id = (AgentIdentity)req.getObject();
				System.out.println("Got Id from :"+id.getLoginName() +" "+id.getId());

			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return id;
	}
	boolean authenticate(AgentIdentity id)
	{
		boolean accept = false;
		String loginName = id.getLoginName();
		int connectionId = id.getId();

		ResultSelect rs = null;
		rs = query.selectQuery("select * from loginStatus where LoginName='"+loginName+"' and ConnectionId="+connectionId);
		if (rs.rowVector.size()!=1)
		{
			accept=false;
			return accept;
		}
		try
		{
			if ( loginName.equals (((java.util.Vector)rs.rowVector.elementAt(0)).elementAt(0)) && connectionId==(Integer.parseInt((String)((java.util.Vector)rs.rowVector.elementAt(0)).elementAt(1))))
				accept=true;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return accept;
	}


}
