views:

63

answers:

1

Dear Experts,

i am stuck in a situation, where i need to move balls in multi-user way, i m using RMI in a distributed animation of moving BALLS .

My goal is to move multiple balls in a way, so that multiple clients observe the same movement/position of balls, i m using ball object which is remote object.

My problem is: i am calling the move function, which is remote and increasing no of clients causes calling that function more frequently and it causes ball speed more and more because of increasing no of calls to this method from different clients.

can somebody please recommend me that how could i cope with this problem that when ball is already moving on one client then other ball don't call the function but just utilize that.

here is code :

public void start() {
        Play = true;
        Thread t = new Thread(this);
        t.start();
    }

public void run() {

    while (Play == true) {
        runball();
        pit.repaint();
        try {
            Thread.sleep(50);
        } catch (InterruptedException ie) {
            stop();
        }
    }
}

public void runball() {

    try {
            aBall.setBounds(pit.getWidth(), pit.getHeight());
            aBall.move();
            }

        catch (Exception e) {
            e.printStackTrace();
        }
}

and there is my remote move method:

public  void move() throws RemoteException {
        // ... Move the ball at the give velocity.
        m_x += m_velocityX;
        m_y += m_velocityY;

    if (m_x < 0) { // If at or beyond left side
        m_x = 0; // Place against edge and
        m_velocityX = -m_velocityX;

    } else if (m_x > m_rightBound) { // If at or beyond right side
        m_x = m_rightBound; // Place against right edge.
        m_velocityX = -m_velocityX;
    }

    if (m_y < 0) { // if we're at top
        m_y = 1;
        m_velocityY = -m_velocityY;

    } else if (m_y > m_bottomBound) { // if we're at bottom
        m_y = m_bottomBound;
        m_velocityY = -m_velocityY;

    }

}

Can somebody please guide me , there is some problem in this design and i m using the RMI in wrong way? or recommend me some design by which i can accomplish my goal.

thanks a lot,

jibby

A: 

I don't think your clients should be telling the server to move the ball(s).

Instead, there should be a server thread that moves the ball at the required rates. The clients should simply be asking the server where the ball currently is.

Stephen C
By the time the client code gets the result, the information might well be out of date. I don't think telling the server where to put the ball is a bad idea. What if he were aiming a telescope instead?
Tony Ennis
Unless the client is moving the ball to its final destination in chunks instead of just naming the destination and letting the server code move it there. It's hard to tell in the code above why the loop is there.
Tony Ennis
The problem is that he has N client all telling the server to move the ball. If you look at what "move()" does, you'll see why that is a problem.
Stephen C
@Static Void Main - I realized that. I simply don't understand why you've designed the system so that the clients are controlling the "physics".
Stephen C
The loop is nothing else but a general "game loop" updating the objects in this case (move ball one step) and rendering the new graphics.
Static Void Main
@Stephen C:i didn't see any other design/example implementation i know the concept of polling and pushing and one of them have better application here but i was unable to find the concrete /compact implementation, now i worked a lot on this and i want it to work.
Static Void Main
see my question here:http://stackoverflow.com/questions/3846422/optimized-design-for-multiuser-simulation-with-rmi-polling-pushing
Static Void Main
Well, that's quite another thing entirely.
Tony Ennis