-1

我在理解我正在制作的程序的内部运作时遇到问题。该程序应该是一个与服务器联系并自行注册的 RMI 客户端。然后,服务器应该循环调用客户端上的方法。但是,未添加侦听器。

注意输出。在服务器上添加监听器后,该方法打印出正确的大小,但运行服务器的线程没有,因为列表仍然是空的。为什么哦,为什么会这样?

客户

public class GameClient extends Thread implements Remote, Client, ModelChangeListener<Client>{
private static final long serialVersionUID = -394039736555035873L;
protected Queue<GameModelEvent> queue = new ConcurrentLinkedQueue<GameModelEvent>(); 


public GameClient(){

}

public static void main(String[] args){
    GameClient client = new GameClient();
    client.start();
}


protected void bind(){
    System.setProperty("java.rmi.server.codebase","file:bin/");
    try {
        Registry registry = LocateRegistry.getRegistry();
        Client c = (Client)UnicastRemoteObject.exportObject(this, 10999);
        Server stub = (Server) registry.lookup("Server");
        stub.registerClient(c);
    } catch (RemoteException | NotBoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}



@Override
public void run() {
    super.run();
    bind();
    while(!Thread.interrupted()){
        System.out.print(".");
        GameModelEvent event = queue.poll();
        while(event != null){

            System.out.println(event);


            event = queue.poll();
        }

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            return;
        }
    }

}

   .....
}

服务器

public class GameServer extends Thread implements ModelChangeListener<GameServer>, Server{
protected Queue<GameModelEvent> queue = new ConcurrentLinkedQueue<GameModelEvent>(); 
List<Client> clients = Collections.synchronizedList(new ArrayList<Client>());

public static void main(String[] args){
    GameServer server = new GameServer();
    server.start();
}

protected void bind(){
    System.setProperty("java.security.policy","file:policy.policy");
    System.setProperty("java.rmi.server.codebase","file:bin/");

    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new SecurityManager());
    }
    try {
        String name = "Server";
        Server engine = new GameServer();
        Server stub =
            (Server) UnicastRemoteObject.exportObject(engine, 0);
        Registry registry = LocateRegistry.getRegistry();
        registry.rebind(name, stub);
        System.out.println("ComputeEngine bound");
    } catch (Exception e) {
        System.err.println("GameServer exception:");
        e.printStackTrace();
        System.exit(1);
    }
}

public void run() {
    super.run();
    bind();
    while(!Thread.interrupted()){
        System.out.print(clients.size()+ " ");

            try {
                for(Client c : clients)
                c.modifyConnection(null);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        GameModelEvent event = queue.poll();
        while(event != null){

            System.out.println(event);


            event = queue.poll();
        }

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            return;
        }
    }

}
@Override
public void registerClient(Client client) {
    System.out.println("\nAdded client "+client);
    clients.add(client);
    System.out.println("clients size "+clients.size());     
}
 ...
 }

输出

ComputeEngine bound
0 0 0 0 
Added client Proxy[Client,RemoteObjectInvocationHandler[UnicastRef [liveRef: [endpoint:[10.117.2.88:10999](remote),objID:[5c30c56:13c5dfe5faf:-7fff, 1084850783049542281]]]]]
clients size 1
0 0 0 0 0 
4

1 回答 1

3

您正在绑定的实例GameServer与您在 main 方法中创建的实例不同。

它是接收 RMI 调用的绑定实例,而它是线程将使用的主方法实例 - 因此您有两个不同的客户端列表。

GameServer替换行中:

UnicastRemoteObject.exportObject(engine, 0);

和 :

UnicastRemoteObject.exportObject(this, 0);
于 2013-01-21T17:57:15.533 回答