Search code examples
javarmi

Java RMI: how can two clients communicate with their references


I got this homework which is to learn RMI. I need to create a server and multiple instances of clients.

Each client connects to the server and the server must keep a list of all of them. The server then return a list of network connections of all clients to every clients so they can call each other's method without having to use the server.

I got the part where each client connect to the server working. But I don't understand what to keep and what to send to the clients so they can communicate with each other.

I am currently trying to keep a list of remote References when using the servers's "registerToBank" method. Am I far from the solution ?

Thanks

Here is the current code :

Server's interface

package banqueRmiInterface;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.HashMap;

import utils.SuccursaleInformationsContainer;

public interface BanqueRMIInterface extends Remote {
    public int registerToBank(int moneyAmount) throws RemoteException;
}

Server:

package banque;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.RemoteRef;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;

import banqueRmiInterface.BanqueRMIInterface;
import succursale.Succursale;
import utils.SuccursaleInformationsContainer;

public class Banque extends UnicastRemoteObject implements BanqueRMIInterface{
private static final long serialVersionUID = 1L;
int totalMoney = 0;
List<Succursale> remoteObjectList = new ArrayList<Succursale>();

protected Banque() throws RemoteException {
    super();

}

@Override
public int registerToBank(int moneyAmount, Succursale succursale) throws RemoteException{
    succursale.setId(remoteObjectList.size());
    remoteObjectList.add(succursale);
    totalMoney += moneyAmount;
    System.out.println(" succurcale #" + succursale.getId() + " added "  + moneyAmount + "$ to the lot");
    System.out.println("New total of money is: " + totalMoney);

    updateClientsRemoteObjectList();

    return remoteObjectList.size()-1;
}

public static void main(String[] args){
    try {
        startRmiRegistry();

        Naming.rebind("//127.0.0.1/Bank", new Banque()); //Change this for your own ip

        System.out.println("Server ready");

    } catch (Exception e) {
        System.err.println("Server exception: " + e.toString());
      e.printStackTrace();
    }
}

public void updateClientsRemoteObjectList(){
    for(int i = 0; i < remoteObjectList.size(); i++){
        System.out.println("updating client #" +remoteObjectList.get(i).getId() );
        remoteObjectList.get(i).updateRemoteObjectList(remoteObjectList);
    }
}


public static void startRmiRegistry(){
    try {
        java.rmi.registry.LocateRegistry.createRegistry(1099);
        System.out.println("RMI registry ready.");
    } catch (Exception e) {
        System.out.println("Exception starting RMI registry:");
        e.printStackTrace();
    }
}

}

Clients:

public class Succursale implements Serializable{
/**
 * 
 */
private static final long serialVersionUID = -905645444505287895L;
private BanqueRMIInterface look_up;
private int id = 0;
private List<Succursale> remoteObjectList;

public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException, Exception{
    Succursale test = new Succursale();
    test.doThings();

}

public void doThings() throws RemoteException, MalformedURLException, NotBoundException, Exception{
    look_up = (BanqueRMIInterface) Naming.lookup("//127.0.0.1/Bank"); //Change this for your own ip
    System.out.println("Combien d'argent vous avez?");
    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    String userInput;
    int moneyAmount =0;
    if ((userInput = stdIn.readLine()) != null) 
    {
        System.out.println(userInput);
        moneyAmount = Integer.parseInt(userInput);

    }


    id = look_up.registerToBank(moneyAmount, this);

    while(true)
    {
        //do things
    }
}

public void updateRemoteObjectList(List<Succursale> remoteObjectList){
    this.remoteObjectList = remoteObjectList;
}

public int getId(){
    return this.id;
}
public void setId(int id){
    this.id = id;
}

}


Solution

  • How can every client will communicate to each other? If they communicate via RMI then you have to expose RMI interface from every client. And you can not return a network connection to a remote server so that would use in different server(different location)!