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

public class LoginServer implements Runnable
{
	LoginServerHandler srvHandler = new LoginServerHandler();
	ServerSocket socket = null;
	int port = 7000;
	Thread t = null;

	public LoginServer(int port)
	{
		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("Login server running on port " + port);
		while (true)
		{
			try
			{
        			Socket clientConn = socket.accept();
        			AgentIdentity id = newClientAgent();
        			srvHandler.addConnection(id, clientConn.getInputStream(), clientConn.getOutputStream());
        			System.out.println("Got new connection...");
      			}
      			catch (Exception e)
			{
				e.printStackTrace();
			}
    		}
	}

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



	public static void main(String args[])
	{
		//Hashtable
		try
		{
		new LoginServer(7000);
		while(true)
		{}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}
