7 Developing RMI Based GUI Client for ChatServer(ChatFrame)
Pravin Jain
Introduction
In the last module, we have looked at how to create an RMI based server for an interactive application like chat server. In this module we continue further and build the GUI and the client code for using the remote objects, created in the previous module.
Demo Of creating Chat Client GUI.
In this demo we are developing a ChatServer. Developing the Chat Server application using the RMI would require the following components.
- A ChatServer interface, which would be the remote interface
- A ChatClient interface, which is again another remote interface from client side
- An implementation of the ChatServer interface (ChatServerImpl)
- An application to create instance of ChatServer and make it available (ChatServerMain)
- A GUI implementation of the ChatClient interface (ChatClientGUIImpl)
- A GUI application for the client side (ChatFrame)
Developing the Chat Server
In the previous module, we have developed the first four components. Now we develop the remaining two classes in this module.
- ChatClientGUIImpl class
- ChatFrame class
Developing the Chat ClientGUIImpl
The ChatClientGUIImpl class is the implementation of the ChatClient interface. This class, has to extend from the UnicastRemoteObject class of the java.remote.server package, and implement the ChatClient interface (the remote interface developed in previous module). This implementation class must have a constructor which has a throws for RemoteException.
In our implementation here, the ChatClientGUIImpl maintains the client’s name, a GUI List component for maintaining all the other currently loggedin client names, and a GUI TextArea component where the text of the chat is maintained. So, we have three instance variables for each of these. The constructor takes these three as parameter and intializes the instance variables.
The implementation of the various methods is as follows:
public String getname(), This method is used for getting the name of the client. It simply returns the name which was initialized in the constructor.
public void joined(String name), this method is provided for a server to indicate that a new client has loggedin and so can be used by the client to update its list of loggedin clients. It does this by appending the name into the clientList.
public void left(String name), this method is provided for a server to indicate that a client has loggedout and so can be used by the client to update its list of loggedin clients. It does this by removing the name from the clientList.
public void showMessage(String from, String message), this method is used by a server to send a message to the client. Asking it to show the message, which it has received from another client for this client. This method simply appends the name of the sender and the message in its chatArea object.
Source code of ChatClientGUIImpl class
package org.epgpathshala.dad.rmi.client;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.awt.List;
import java.awt.TextArea;
public class ChatClientGUIImpl extends UnicastRemoteObject implements ChatClient {
private String name;
private List clientList;
private TextArea chatArea;
public ChatClientGUIImpl(String n, List cl, TextArea ca) throws RemoteException { this.name = n;
this.clientList = cl; this.chatArea = ca;
}
public String getName() { return this.name;
}
public void joined(String name) {
this.clientList.add(name);
}
public void left(String name) { this.clientList.remove(name);
}
public void showMessage(String from, String message) {
this.chatArea.append(“Message from “+from+”: “+message+”\r\n”);
}
}
Developing the ChatFrame
The Chat Frame class contains the main method. It is the GUI client for the ChatServer. The GUI is created using the AWT APIs. Let us look athevarious methods and constructor for this class.
The main method takes three command line arguments for getting the host-name of the ChatServer, The name by which the ChatServer is registered for lookup by client and the name which will be used by this client to login into the server. In the main method, it lookup the ChatServer and then passes the ChatServer and the client Name in the constructor of the Chat Frame. Like a typical GUI application, here also, we simply create the instance of the subclass of Frame and make it visible.
This class has instance variables for the ChatServer, Chat Client, the client name, a text area for the chat, another text area for the entry, a List for managing the list of other client names, and two buttons, one for sending message and the other for logging out.
The constructor is used for initializing all the instance variables, the ChatServer and client name are received as parameters in the constructor, Other are GUI components, which are initialized and then added into the frame.. The constructor is then used for laying out the components and then event handlers in the form of ActionListener are setup for the send and logout buttons, also a windowListener is setup to handle the windowclosing event, to do a logout in case of window closing.
The Chat Client instance is created in the constructor, which is given the client name, the text area for chat and the list, so that these can be updated by the server invoking the method on the Chat Client instance. The event handlers for send button invokes the send Message method on the server for all the selected clients in the list and the event handler for the logout button invokes the logout method on the server.
The code for ChatFrame class is as follows: Source code of ChatFrame class
package org.epgpathshala.dad.rmi.client;
import java.awt.*;
import java.awt.event.*;
import java.rmi.RemoteException;
import java.rmi.Naming;
import org.epgpathshala.dad.rmi.server.*;
public class ChatFrame extends Frame {
private ChatServer server;
private ChatClient client; private String clientName;
private TextArea chatArea = new TextArea(20, 70);
private TextArea entryArea = new TextArea(5, 70);
private List clientList = new List(20, true);
private Button sendButton = new Button(“Send”);
private Button logoutButton = new Button(“Logout”);
public ChatFrame(ChatServer server, String clientName) throws RemoteException { super(“Chat Client – “+clientName);
this.server = server; this.clientName = clientName;
this.client = new ChatClientGUIImpl(clientName, clientList, chatArea); String[] clients = this.server.login(this.client);
for (String name : clients) { clientList.add(name);
}
this.setBounds(0, 0, 730, 500);
setupComponents(); setupEvents();
}
public void setupComponents() {
this.setLayout(new FlowLayout()); this.add(chatArea); this.add(clientList); this.add(entryArea);
Panel buttonPanel = new Panel(); buttonPanel.add(sendButton); buttonPanel.add(logoutButton); this.add(buttonPanel);
}
public void setupEvents() { this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) { try {
server.logout(clientName);
} catch (RemoteException re) {
re.printStackTrace();
}
});
}
System.exit(0);
logoutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) { try {
server.logout(clientName);
} catch (RemoteException re) {
re.printStackTrace();
}
});
}
System.exit(0);
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) { String[] clientNames = clientList.getSelectedItems(); for (String name : clientNames) {
try {
server.sendMessage(clientName, name, entryArea.getText()); chatArea.append(“Message sent to “+name+”: “+entryArea.getText()+”\r\n”);
} catch (RemoteException re) { re.printStackTrace();
}
}
}
});
}
entryArea.setText(“”);
public static void main(String[] args) throws Exception {
String host = args[0]; String chatRoom = args[1];
String clientName = args[2];
ChatServer server = (ChatServer)Naming.lookup(“rmi://”+host+”/”+chatRoom);
ChatFrame frame = new ChatFrame(server, clientName); frame.setVisible(true);}
}
you can view video on Developing RMI Based GUI Client for ChatServer(ChatFrame) |
Suggested Reading:
- Core Java Volume 2 by Cay Horstmann & Gary Cornell, Ninth Edition, Pearson Education.
- Beginning Java Networking by Alexander V Konstantinou and others, Wrox publication.
- Java RMI By William Grosso O’Rielly media
- https://docs.oracle.com/javase/tutorial/rmi/index.html