/**
 *
 *
 * @author James McCabe
 */
 
// import org.omg.CORBA;
import java.util.*;

/**
 * server for OrChat using CORBA
 */
 
public class OrChatServer extends OrChat._OrChatServerIImplBase {
	
	Hashtable clients;
	/**
	 * constructor
	 */
	public OrChatServer(String name) {
		super(name);
		clients = new Hashtable(10*2);
		//clients = new Vector();

	}

	/**
	 * initialize the server
	 */
	public static void main(String args[]) {
		System.out.println("server: main");
		org.omg.CORBA.ORB orb  = org.omg.CORBA.ORB.init();
		org.omg.CORBA.BOA boa = orb.BOA_init();
		OrChatServer orChatServer = new OrChatServer("oranda");
		boa.obj_is_ready(orChatServer);
		System.out.println("server: obj_is_ready");
		boa.impl_is_ready();
	}
	
	/** 
	 * adds a listener (for a chat client) to the
	 * Vector of listeners
	 */
//	public void addClient(OrChat.OrChatClientI client, String username) {
//		System.out.println("server: addClient username");
//		//clients.addElement(client, username);
//		clients.put(username,client);
//	}

	public void addClient(OrChat.OrChatClientI client, String username) {
		System.out.println("server: addClient username");
	//	clients.addElement(client);

		clients.put(username,client);
	}
	
	/**
	 * send a message to all the client
	 */
	public void sendMessage(String message) {
		System.out.println("server: sendMessage" + message);
		String nextEl = null;
		for (Enumeration e = clients.keys(); e.hasMoreElements();) {
			nextEl = (String) e.nextElement();
			//System.out.println("server: in sendMessage: " + nextEl);
			((OrChat.OrChatClientI)(clients.get(nextEl))).receiveMessage(message);
		}
	}

}