I have made java RMI application - Chat.
Chat Class:
public class Chat extends UnicastRemoteObject implements ChatInterface {
public String name;
public ChatInterface client=null;
public Chat(String n) throws RemoteException {
this.name=n;
}
public String getName() throws RemoteException {
return this.name;
}
public void setClient(ChatInterface c){
client=c;
}
public ChatInterface getClient(){
return client;
}
public void send(String s) throws RemoteException{
System.out.println(s);
}
}
ChatClient class:
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
public class ChatClient {
public static void main (String[] argv) {
try {
System.setProperty("java.rmi.server.hostname","127.0.0.1");
System.setSecurityManager(new RMISecurityManager());
Scanner s=new Scanner(System.in);
System.out.println("Enter Your name and press Enter:");
String name=s.nextLine().trim();
ChatInterface client = new Chat(name);
ChatInterface server = (ChatInterface)Naming.lookup("rmi://localhost/ABC");
String msg="["+client.getName()+"] got connected";
server.send(msg);
System.out.println("[System] Chat Remote Object is ready:");
server.setClient(client);
while(true){
msg=s.nextLine().trim();
msg="["+client.getName()+"] "+msg;
server.send(msg);
}
}catch (Exception e) {
System.out.println("[System] Server failed: " + e);
}
}
}
ChatInterface interface:
public interface ChatInterface extends Remote{
public String getName() throws RemoteException;
public void send(String msg) throws RemoteException;
public void setClient(ChatInterface c)throws RemoteException;
public ChatInterface getClient() throws RemoteException;
}
ChatServer class:
public class ChatServer {
public static void main (String[] argv) {
try {
System.setProperty("java.rmi.server.hostname","127.0.0.1");
System.setSecurityManager(new RMISecurityManager());
Scanner s=new Scanner(System.in);
System.out.println("Enter Your name and press Enter:");
String name=s.nextLine().trim();
Chat server = new Chat(name);
Naming.rebind("rmi://localhost/ABC", server);
System.out.println("[System] Chat Remote Object is ready:");
while(true){
String msg=s.nextLine().trim();
if (server.getClient()!=null){
ChatInterface client=server.getClient();
msg="["+server.getName()+"] "+msg;
client.send(msg);
}
}
}catch (Exception e) {
System.out.println("[System] Server failed: " + e);
}
}
}
Chat - the same code in both projects
ChatInterface - the same code in both projects too
ChatClient - ChatServer - they ones that have different code
It looks like this: How it looks inside Eclipse (hierarchy)
And problem is, that this chat works something like... We got all messages (like whole history) but when user send new message. For example, we have user1, user2, user3.
And User3 sees nothing, cuz didn't send any message, so have nothing.
And that's bad :/. It's like user have to send something to get messages. Even only "space", but still something. Otherwise he won't get any message.
I need to make this to automatically send all users new messages when they are send by one user. Like "update".'
It needs to be like
public interface ChatInterface extends Remote{
public String getName() throws RemoteException;
public void send(String msg) throws RemoteException;
public void setClient(ChatInterface c)throws RemoteException;
public ChatInterface getClient() throws RemoteException;
}
The problem starts here. The nature of setClient()
is that the server only knows about one client at a time. Surely the server should have a list of clients, and the method should be addClient(ChatInterface c)
.
The purpose of getClient()
as a remote method eludes me.