import java.applet.*; import java.awt.*; import java.io.*; import java.rmi.*; import java.rmi.server.UnicastRemoteObject; public interface ChatService extends Remote { public void distributeMessage (String msg) throws RemoteException; public void subscribe (ChatCallback callback) throws RemoteException; public void unsubscribe (ChatCallback callback) throws RemoteException; } public interface ChatCallback extends Remote { public void showMessage (String msg) throws RemoteException; } public class ChatClient3 extends Applet { ChatService chat_service = null; ChatCallback callback = null; TextField user_name; TextField input_field; TextArea output_area; public void init () { user_name = new TextField (20); input_field = new TextField (40); output_area = new TextArea (10, 40); output_area.setEditable (false); this.add (user_name); this.add (input_field); this.add (output_area); } public boolean action (Event e, Object what) { if (e.target == input_field) { if (chat_service != null) { try { chat_service.distributeMessage (user_name.getText () + ": " + (String) e.arg); } catch (Exception ex) { System.out.println("error: " + ex.getMessage()); } } input_field.setText (""); return true; } return false; } public void start () { subscribe ("//" + getCodeBase().getHost() + "/ChatService"); } public void stop () { unsubscribe (); } public void subscribe (String rmi_name) { try { callback = new ChatCallbackImpl (output_area); chat_service = (ChatService) Naming.lookup (rmi_name); chat_service.subscribe (callback); } catch (Exception e) { System.out.println("error: " + e.getMessage()); } } public void unsubscribe () { if (chat_service == null || callback == null) return; try { chat_service.unsubscribe (callback); } catch (Exception e) { System.out.println("error: " + e.getMessage()); } chat_service = null; callback = null; } } public class ChatCallbackImpl extends UnicastRemoteObject implements ChatCallback { private TextArea output_area; public ChatCallbackImpl (TextArea output_area) throws RemoteException { super(); this.output_area = output_area; } public void showMessage (String msg) throws RemoteException { output_area.appendText (msg + "\n"); } } public class ChatServiceImpl xtends UnicastRemoteObject implements ChatService { private Vector callbacks = new Vector (); public ChatServiceImpl() throws RemoteException { super(); } public synchronized void distributeMessage (String msg) throws RemoteException { for (int i = 0; i < callbacks.size (); i++) ((ChatCallback)callbacks.elementAt (i)).showMessage (msg); } public synchronized void subscribe (ChatCallback callback) throws RemoteException { callbacks.addElement (callback); } public synchronized void unsubscribe (ChatCallback callback) throws RemoteException { callbacks.removeElement (callback); } public static void main(String args[]) { System.setSecurityManager(new MySecurityManager()); try { ChatServiceImpl obj = new ChatServiceImpl(); Naming.rebind("//panoramix.pal.xerox.com/ChatService", obj); } catch (Exception e) { System.out.println("ChatServiceImpl err: " + e.getMessage()); } } }