解决 RMI 应用程序中的可扩展性、性能和优化问题?

发布于 2024-09-28 05:13:05 字数 2645 浏览 5 评论 0原文

我的问题是:这种设计对于一个球来说工作正常,但我无法让它对于多个球工作,我在更换“这个”时基本上遇到了问题updateClients () 中的关键字。

我以为我需要做这样的事情,但我失败了:

System.out.println("in ballimpl" + j.size());
            for (ICallback aClient : j) {
                aClient.updateClients(BallImpl[i]);
            }

代码的当前情况是:

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

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

感谢您的任何反馈。

吉比拉拉

my problem is: this design is working fine for one ball but i m unable to get it work for multiple balls, i have basically problem in replacing the "this" keyword in updateClients ().

i thought i need to do something like this but i m failed:

System.out.println("in ballimpl" + j.size());
            for (ICallback aClient : j) {
                aClient.updateClients(BallImpl[i]);
            }

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

thanks for any feedback.

jibbylala

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

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

发布评论

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

评论(1

不知所踪 2024-10-05 05:13:05

将 RMI 逻辑与 Ball 逻辑分开。

您应该能够在不需要任何 RMI 模块的情况下运行球模拟。只是在本地运行它,测试它。然后,您应该找到一种方法将该进程包装在 RMI 中,以便您仍然可以在本地运行它来测试它,而无需任何类型的 RMI 接口。该代码块是引擎,能够以尽可能原子的形式对其进行测试非常重要。将额外的部分与其集成只会增加毫无疑问是最复杂代码的复杂性。

不要让任何额外的接口进入您的引擎。它应该非常具体,并且使用您的引擎所需的软件包很少。您的软件需要任何新功能,请在引擎中适当实现它以支持通用设计。包装它以在引擎核心之外提供特定的功能。这可以保护发动机设计免受环境变化的影响。它还允许对发动机进行更完整的测试。

有时我们会在某些东西只能以一种方式使用的情况下例外。但在这种情况下,不使用 RMI 进行测试似乎对于让引擎正常工作至关重要。如果由于大量客户端连接,您的引擎运行速度超过了网络可以跟上的速度,那么您是否希望整个游戏变慢,或者您希望客户端落后?我说,你希望能够做出这样的选择。

Separate your RMI logic from your Ball logic.

You should be able to run your ball simulation without needing any RMI modules. Just to run it locally, to test it. Then you should find a way to wrap that process in RMI so that you can still run it locally to test it without any sort of RMI interface. This block of code is the engine, and it is very important to be able to test it in as atomic a form as possible. Having extra parts integrated with it just increases the complexity of what will undoubtedly be some of the most complex code.

Don't let any extra interfaces into your engine. It should be very specific and few the packages required to use your engine. Any new functionality your software needs, implement it appropriately in the engine to support generic design. Wrap that to provide specific functionality outside the core of the engine. This protects the engine design against changes to the environment. It also allows for more complete testing of the engine.

We make exceptions sometimes in cases where something will only ever be used in one way. But in this case, testing without RMI would seem to be critical to getting your engine working correctly. If your engine runs faster than the network can keep up due to large numbers of clients connecting, do you want the whole game to slow down, or do you want the clients to lag behind? I say, you want to be able to make that choice.

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