按照我为带有 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");
}
}
正在实现回调接口(interface)并具有更新方法实现的客户端:
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 实现轮询机制?
感谢您的任何反馈。
吉比拉拉
最佳答案
轮询独立于您用于实现客户端和服务器的协议(protocol)。
客户端通过无限循环进行轮询。在循环内有一个向服务器请求信息的请求。服务器发送回所需信息或“未准备好”消息。客户端执行其操作并等待,直到需要发送下一个请求。
如果你碰巧选择了RMI,那就意味着RMI客户端和服务器。但无论如何,轮询机制是相同的。
将问题分解 - 这样会更容易思考和解决。
忘记启动轮询。您可以编写一个 RMI 服务器、启动它并创建一个单独的客户端来发出单个请求吗?如果你能做到这一点,那么你可以将它放入一个带有 sleep 的循环中以实现延迟,然后你就完成了。
关于java - 如何用RMI实现轮询机制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3846422/