package chat;

import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.*;
import java.util.*;


public class ChatServerImpl extends UnicastRemoteObject implements ChatServer
{
	private Vector clients = new Vector();
	
	public ChatServerImpl() throws RemoteException
	{}
	
	public void enter(String name, ChatClient client) throws RemoteException
	{
		if(client == null)
			throw new RemoteException("Cliente Invalido");
			
		clients.addElement(client);
		System.out.println(name+" entrou na sala");
	}
	
	public void exit(ChatClient client) throws RemoteException
	{
		clients.removeElement(client);
	}
	
	public void sendMessage(String name, String message, String Contato) throws RemoteException
	{
		for(int x=0; x<clients.size();x++)
//			if (Contato.equals(clients[x].getNome()))
			((ChatClient)clients.elementAt(x)).printMessage(name+": "+message);
	}

	public static void main(String [] args)
	{
     try
     {
        ChatServerImpl server = new ChatServerImpl();
        
        String url = "//localhost:1234/chatserver";

        LocateRegistry.createRegistry(1234);
        Naming.rebind(url,server);
     }
     catch(Exception e){System.out.println(e);}
	}
}


