FileDocCategorySizeDatePackage
RMIChatClient.javaAPI DocExample2678Tue Jan 20 22:32:46 GMT 1998dcj.util.Collaborative

RMIChatClient

public class RMIChatClient extends RMICollaboratorImpl implements ActionListener
Source code from "Java Distributed Computing", by Jim Farley. Class: RMIChatClient Example: 10-1 Description: A collaborator in an RMI-based chat session. Includes an AWT interface for the chat display.

Fields Summary
TextArea
chatArea
TextField
chatInput
Constructors Summary
public RMIChatClient(String name, String host, String mname)

    super(name);
    Properties p = new Properties();
    p.put("host", host);
    p.put("mediatorName", mname);
    connect(p);
    initGraphics();
  
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

    // See if there's something to say...
    String msg = chatInput.getText();
    if (msg.length() > 0) {
      try {
        // Broadcast our message to the rest of the chat clients
        boolean success = broadcast("chat", msg);
        if (success) {
          System.out.println("Sent message OK.");
        }
        else {
          System.out.println("Failed to send message.");
        }
        // Clear the chat input field
        chatInput.setText("");
      }
      catch (Exception exc) {
      }
    }
  
protected voidinitGraphics()

    Frame f = new Frame();
    f.setLayout(null);
    f.addNotify();
    f.setSize(f.getInsets().left + 405, f.getInsets().top + 324);
    chatArea = new java.awt.TextArea();
    chatArea.setBounds(f.getInsets().left, f.getInsets().top,405, 300);
    f.add(chatArea);
    chatInput = new java.awt.TextField();
    chatInput.setBounds(f.getInsets().left + 84,f.getInsets().top + 300,264,24);
    f.add(chatInput);
    Button button = new java.awt.Button("Send");
    button.setBounds(f.getInsets().left + 348,f.getInsets().top + 300,60,24);
    f.add(button);
    button.addActionListener(this);
    Label label = new java.awt.Label("Chat here:");
    label.setBounds(f.getInsets().left,f.getInsets().top + 300,84,24);
    label.setAlignment(label.RIGHT);
    f.add(label);
    f.setTitle("RMI Chat Client");
    f.show();
  
public booleannotify(java.lang.String tag, java.lang.String msg, Identity src)

    // Print the message in the chat area.
    chatArea.append("\n" + src.getName() + ": " + msg);
    return true;