按照我为具有 RMI 服务器回调的多用户/网络回合制游戏创建的设计/架构,我尝试创建一个分布式动画,其中我的模型(球)是远程对象,它通过服务器的回调机制更新客户端。
代码的现状是:
模型远程对象,它正在迭代客户端列表并调用它们的更新方法,
public class BallImpl extends UnicastRemoteObject implements Ball,Runnable {
private List<ICallback> clients = new ArrayList<ICallback>();
protected static ServerServices chatServer;
static ServerServices si;
BallImpl() throws RemoteException {
super();
}
....
public synchronized void move() throws RemoteException {
loc.translate((int) changeInX, (int) changeInY);
}
public void start() throws RemoteException {
if (gameThread.isAlive()==false )
if (run==false){
gameThread.start();
}
}
/** Start the ball bouncing. */
// Run the game logic in its own thread.
public void run() {
while (true) {
run=true;
// Execute one game step
try {
updateClients();
} catch (RemoteException e) {
e.printStackTrace();
}
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
}
}
}
public void updateClients() throws RemoteException {
si = new ServerServicesImpl();
List<ICallback> j = si.getClientNames();
System.out.println("in messimpl " + j.size());
if (j != null) {
System.out.println("in ballimpl" + j.size());
for (ICallback aClient : j) {
aClient.updateClients(this);
}
} else
System.err.println("Clientlist is empty");
}
}
正在实现回调接口并具有更新方法实现的客户端:
public final class thenewBallWhatIwant implements Runnable, ICallback {
.....
@Override
public void updateClients(final Ball ball) throws RemoteException {
try {
ball.move();
try {
Thread.sleep(50);
} catch (Exception e) {
System.exit(0);
}
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
.....
}
我的一般看法是我用 RMI 实现推送机制,在那种情况下我需要实现轮询)
如果是这种情况,我如何使用 RMI 实现轮询机制?
感谢您的任何反馈。
吉比拉拉