5.5. Building a Chat Application with Channels

In this section we are going to take the ChannelWriter and ChannelReader applets one step further and create a simple “chat” applet by combining their functionality. Doing so requires only a few changes to our existing code.

5.5.1. The Graphical User Interface

We are going to call our new applet ChannelChat. The ChannelChat applet's user interface, shown in Figure 5.4, is a combination of the ChannelReader and ChannelWriter user interfaces. ChannelChat provides text fields for entering a channel and a message, a “Send” button for adding messages to the channel, and a “Read” button to start reading the channel. It also provides an output area where channel messages are displayed.

Figure 5.4. The ChannelChat interface: a hybrid of ChannelWriter and ChannelReader.


5.5.2. Combining Channel Writing and Reading

For the chat application, we reuse our previous Message and Tail classes. Here is the code for the ChannelChat applet, which combines code from our previous ChannelWriter and ChannelReader applets:

public class ChannelChat extends Applet
  implements ActionListener, Runnable
{
  private JavaSpace space;
  private String channel;
  private Thread channelReader;

  //... variables for user interface components go here

  public void init() {
    space = SpaceAccessor.getSpace();

    //... creation of user interface components
  }

  // "Enter" pressed in a field, or a button was clicked
  public void actionPerformed(ActionEvent event) {
    channel = channelTextField.getText();
    String message = messageTextField.getText();
    Object object = event.getSource();

    if ((object == messageTextField) ||
    (object == sendButton)) {
    // "Enter" pressed in msg field or "Send" clicked
    if (message.equals("") || channel.equals("")) {
       showStatus("Enter both channel & msg fields.");
       return;
    }
    channelTextField.setEditable(false);
    messageTextField.setText("");
    append(channel, message);
  } else {
    // "Enter" pressed in channel field or "Read" clicked
    if (channel.equals("")) {
       showStatus("Enter a channel name.");
       return;
    }
    channelTextField.setEditable(false);

    if (channelReader == null) {
       channelReader = new Thread(this);
       channelReader.start();
    }
  }
}

private void createChannel(String channel) {
  //... code from ChannelWriter applet
}

private void append(String channel, String msg) {
  //... code from ChannelWriter applet

}
private Integer getMessageNumber(String channel) {
  //... code from ChannelWriter applet
}

public void run() {
  //... code from ChannelReader applet
}
}

In this applet, the append, getMessageNumber, and createChannel methods are taken directly from our previous ChannelWriter applet, while run is reused from our ChannelReader applet. The init method is based on the one in ChannelReader, except that there are additional user interface components for sending messages.

The only substantially reworked method is actionPerformed. The method first determines which component (“Send” button or “Read” button) generated the action event. If “Send” was clicked (or “Enter” was pressed in the message text field), actionPerformed calls append to send the message out. On the other hand, if “Read” was clicked (or “Enter” was pressed in the channel text field), actionPerformed starts a new listener thread that executes the run method (just as it did in ChannelReader).

By combining channel writing and reading functionality into a single applet, we've just built a simple multiuser chat application. You should now be able to start up several copies of ChannelChat and carry on a conversation.

We could certainly improve our application by adding features (such as being able to see who sent a message) and making it robust in the face of failure (as you'll explore in a Chapter 9 exercise). However, even in its current form, we've managed to build a fairly sophisticated collaborative application on the basis of a simple distributed data structure.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset