RMI如何实现轮询机制? [英] How Polling mechanism can be realized with RMI?

查看:105
本文介绍了RMI如何实现轮询机制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为带有RMI服务器回调的多用户/网络回合制游戏创建的设计/体系结构之后,我尝试创建一个分布式动画,其中我的模型(Ball)是远程对象,它通过回调机制从客户端更新客户端.服务器.

Following the design/architecture i created for multiuser/network turn-based game with RMI server callbacks, I have tried to create a distributed animation in which my model(Ball) is remote object and it updates the clients via callback mechanism from server.

代码的当前情况是:

模型远程对象,它正在迭代客户端列表并调用它们的更新方法,

The model remote object, which is iterating client list and calling update method of them,

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");
       } 
    }

正在实现回调接口并具有更新方法实现的客户端:

The client which is implementing callback interface and has update method implementation :

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实施推送机制,在这种情况下,我需要实施轮询)

My general perception is that i m implementing pushing mechanism with RMI and in that scenario i need to implement polling)

如果是这种情况,我如何使用RMI实施轮询机制?

if that is the case how can i implement the polling mechanism with RMI?

感谢您的任何反馈.

jibbylala

jibbylala

推荐答案

轮询独立于用于实现客户端和服务器的协议.

Polling is independent of the protocol you use to implement the client and server.

客户端通过不断循环进行轮询.在循环内部,有一个向服务器请求信息的请求.服务器发送回所需的信息或未就绪"消息.客户端执行其操作,并等待直到下一个请求需要发送为止.

A client polls by looping endlessly. Inside the loop there's a request to the server for information. The server sends either the desired information or a "not ready" message back. The client does its thing and waits until the next request needs to be sent.

如果您恰巧选择RMI,则表示RMI客户端和服务器.但是无论如何,轮询机制都是相同的.

If you happen to choose RMI, it means an RMI client and server. But the polling mechanism is the same regardless.

将问题分解成碎片-这样思考和解决问题会更容易.

Break the problem into pieces - it'll be easier to think about and solve that way.

忘记开始轮询.您可以编写,启动RMI服务器并创建单独的客户端以发出单个请求吗?如果可以做到,则将其放入带有睡眠的循环中以实现延迟,然后完成.

Forget about polling to start. Can you write an RMI server, start it up, and create a separate client to make a single request? If you can do that, then you put it inside a loop with a sleep to implement the delay and you're done.

这篇关于RMI如何实现轮询机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆