简体   繁体   English

Java RMI:两个客户端如何与他们的引用通信

[英]Java RMI: how can two clients communicate with their references

I got this homework which is to learn RMI. 我得到了这本用来学习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. 我目前正在尝试使用服务器的“ registerToBank”方法时保留远程引用的列表。 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;
}

} }

The answer is in your title. 答案就在您的标题中。 'With their references'. '有他们的参考'。 All a client has to to is get a remote reference to another client from the server, and then just start using it in the normal way. 客户端所需要做的就是从服务器获得对另一个客户端的远程引用,然后以正常方式开始使用它。

Firewalls permitting. 防火墙允许。

How can every client will communicate to each other? 每个客户如何相互交流? If they communicate via RMI then you have to expose RMI interface from every client. 如果他们通过RMI进行通信,则必须从每个客户端公开RMI接口。 And you can not return a network connection to a remote server so that would use in different server(different location)! 而且您无法将网络连接返回到远程服务器,因此将在其他服务器(不同位置)中使用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM