【发布时间】:2017-06-15 21:45:44
【问题描述】:
我的作业是学习 RMI。我需要创建一个服务器和多个客户端实例。
每个客户端都连接到服务器,服务器必须保存所有客户端的列表。然后服务器将所有客户端的网络连接列表返回给每个客户端,以便它们可以调用彼此的方法而无需使用服务器。
我得到了每个客户端连接到服务器工作的部分。但我不明白要保留什么以及向客户发送什么以便他们可以相互交流。
我目前正在尝试在使用服务器的“registerToBank”方法时保留远程引用列表。我离解决方案还远吗?
谢谢
这是当前代码:
服务器界面
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;
}
服务器:
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();
}
}
}
客户:
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;
}
}
【问题讨论】:
-
每个客户端如何相互通信?如果他们通过 RMI 进行通信,那么您必须向每个客户端公开 RMI 接口。
-
“返回网络连接列表”是什么意思?您不能将网络连接返回到远程服务器,以便在不同的服务器(不同的位置)中使用!
-
@Zico 你能发表你的评论作为答案吗?这是对我有用的解决方案,谢谢!
-
@Zicco 你能告诉我们这到底意味着什么吗?