// ChatServerMain.java // // This class implements a ChatServer using CORBA. // It borrows heavily from Deitel and Deitel's chat server // in their Advance Java text. // // Written by: Stuart Hansen // Date: February 3, 2009 // OMG CORBA packages import org.omg.CORBA.*; import org.omg.CosNaming.*; import org.omg.PortableServer.*; import org.omg.CosNaming.NamingContextPackage.*; // Add the chat package import chatpkg.*; import java.util.*; class ChatServerImpl extends ChatServerPOA implements Runnable { // The server maintains a vector of all registered clients private ArrayList clientList = new ArrayList(); ORB orb; NamingContextExt nameService; POA rootpoa; // The constructor setups up the CORBA elements public ChatServerImpl() { try { // Start by activating this object's ORB and its POA activateORBAndPOA(); // Find the name service nameService = findTheNameService(); // register with the name service registerWithNameService(); System.out.println("Ready to chat"); // wait for invocations from clients new Thread(this).start(); } catch (Exception e) { System.err.println("Problem starting server"); } } // Activates the ORB and the POA public void activateORBAndPOA() throws Exception { String args[] = {"-ORBInitialPort", "1060"}; orb = ORB.init(args, null); // get reference to rootpoa and activate the POAManager rootpoa = POAHelper.narrow( orb.resolve_initial_references("RootPOA")); rootpoa.the_POAManager().activate(); } // Find the name service public NamingContextExt findTheNameService() throws Exception { // get a reference to the name service (orbd). org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); return NamingContextExtHelper.narrow(objRef); } // Register with the name service so that others can find us public void registerWithNameService() throws Exception { // get object reference from the servant org.omg.CORBA.Object ref = rootpoa.servant_to_reference(this); ChatServer meIOR = ChatServerHelper.narrow(ref); // bind (register) the ChatServer to the name service String name = "ChatServer"; NameComponent path[] = nameService.to_name(name); nameService.rebind(path, meIOR); } // A run method so that we can start a new thread to listen for connections public void run() { orb.run(); } // Register a new client synchronized public int register(String clientName) { if (clientList.contains(clientName)) { return 0; } // Notify all other clients of the new client sendNewClientNotification(clientName); // Send the new client the list of all other clients sendListToNewClient(clientName); clientList.add(clientName); return 1; } public ChatClient lookUpClientOnNameService(String name) throws Exception { // Resolve object reference using the name service NameComponent nameComponent = new NameComponent(name, ""); NameComponent path[] = {nameComponent}; org.omg.CORBA.Object corbaObject = nameService.resolve(path); return ChatClientHelper.narrow(corbaObject); } // Walk through the client list notifying all clients of the name // of the new client private void sendNewClientNotification(String clientName) { Iterator it = clientList.iterator(); ChatClient chatClient; while (it.hasNext()) { String name = it.next(); try { chatClient = lookUpClientOnNameService(name); // send the notification chatClient.newClientNotification(clientName); } catch (Exception ex) { System.out.println("Failed in registration notification!"); System.out.println(ex); } } } // This method sends the list of registered clients to // a newly registered client private void sendListToNewClient(String clientName) { Iterator it = clientList.iterator(); ChatClient chatClient; try { chatClient = lookUpClientOnNameService(clientName); // send the notifications while (it.hasNext()) { chatClient.newClientNotification(it.next()); } } catch (Exception ex) { System.out.println("Failed in registration notification!"); System.out.println(ex); } } // Unregister a ChatClient synchronized public void unregister(String clientName) { clientList.remove(clientName); sendDeregistrationNotification(clientName); } // Notify clients of a DeRegistration private void sendDeregistrationNotification(String clientName) { Iterator it = clientList.iterator(); ChatClient chatClient; while (it.hasNext()) { String name = it.next(); try { chatClient = lookUpClientOnNameService(name); // send the notification chatClient.clientUnregisteredNotification(clientName); } catch (Exception ex) { System.out.println("Failed in deregistration notification!"); System.out.println(ex); } } } // Post a message to all registered clients synchronized public void post(chatpkg.Message message) { Iterator it = clientList.iterator(); ChatClient chatClient; while (it.hasNext()) { String name = it.next(); try { chatClient = lookUpClientOnNameService(name); chatClient.sendMessageToClient(message); } catch (Exception ex) { System.out.println("Failed to send message"); System.out.println(ex); } } } // Whisper a message to one other client synchronized public void whisper(String clientName, chatpkg.Message message) { if (clientList.contains(clientName)) { ChatClient chatClient; try { chatClient = lookUpClientOnNameService(clientName); chatClient.whisperMessageToClient(message); } catch (Exception ex) { System.out.println("Failed to whisper message"); System.out.println(ex); } } } } // end class ChatServerImpl public class ChatServerMain { // Main method to execute server public static void main(String[] args) throws Exception { // Make a servant object new ChatServerImpl(); } }