如何用RMI实现轮询机制?

发布于 2024-09-26 07:24:01 字数 2463 浏览 8 评论 0原文

按照我为带有 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 实现轮询机制?

感谢您的任何反馈。

吉比拉拉

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 current situation of code is :

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

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

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

thanks for any feedback.

jibbylala

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2024-10-03 07:24:01

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

客户端通过无限循环进行轮询。在循环内有一个向服务器请求信息的请求。服务器发送回所需的信息或“未准备好”消息。客户端执行其操作并等待,直到需要发送下一个请求。

如果你碰巧选择了RMI,那就意味着RMI客户端和服务器。但无论如何,轮询机制是相同的。

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

忘记开始投票。您可以编写一个 RMI 服务器、启动它并创建一个单独的客户端来发出单个请求吗?如果你能做到这一点,那么你可以将它放入一个带有睡眠的循环中以实现延迟,然后你就完成了。

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.

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.

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.

愁杀 2024-10-03 07:24:01

我不相信你可以通过 Java RMI 实现回调。您需要按照您的建议设置轮询,或者让您的“客户端”RMI 服务器可以直接向它们发送消息。

你可以如何以不同的方式做到这一点?我建议使用 JMS 消息传递将命令对象发送到客户端,这将为您处理所有分发。

I don't belive you can implement a callback via Java RMI. You need to either setup polling as you have suggested, or make your "client" RMI servers can you can send message to them directly.

How could you do this differently? I would suggest using JMS messaging to send command objects to the clients, this would handle all the distribution for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文