views:

26

answers:

1

Dear Experts,

I am working on a distributed animation of moving BALLS with RMI.

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.

The ball is moving fine, when it is only one, but when i m trying to increase the number of balls, i m failing.

here are some code snippets where i applied loops to work for multiple balls:

on server:

  b[0] = new BallImpl(0, 50, 2, 3 ,Color.YELLOW,20);
  b[1] = new BallImpl(50, 50, 4, 3,Color.BLUE,10);
  b[2] = new BallImpl(40, 40, 5, 5, Color.PINK,30);
  b[3] = new BallImpl(60, 70, 4, 6, Color.GREEN,40);

    for (int i = 0; i < currentNumBalls; i++) {

      Naming.rebind ("rmi://localhost/BouncingBall", b[i]);  // registers Ball object
      System.out.println ("remote ball object registered.");
    }

on client site :

that how i look up for remote balls:

 for (int i = 0; i < currentNumBalls; i++) {
        try {
            this.aBall[i] = (Ball) Naming.lookup("rmi://localhost/BouncingBall");

        } catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
    start();    

thats moving balls code:

public void moveballs() {

        for (int i = 0; i < currentNumBalls; i++) {
            try {

                aBall[i].setBounds(pit.getWidth(), pit.getHeight());
                aBall[i].move();

                pit.repaint();

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

and that's the drawing code:

 public void drawballs(Graphics g) {

    for (int i = 0; i < currentNumBalls; i++) {
        try {

            g.setColor(aBall[i].getColor());
            g.fillOval(aBall[i].getX(), aBall[i].getY(), aBall[i].getradius(), aBall[i].getradius());

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

    }
}

Can somebody please guide me why i can see only one ball moving, what about other balls or 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: 

It looks like you are binding all of your balls under the same name. You need to give them different names, like this:

for (int i = 0; i < currentNumBalls; i++) {

  Naming.rebind ("rmi://localhost/BouncingBall"+i, b[i]); //add index to the name
  System.out.println ("remote ball object registered.");
}

Then when looking them up, use this:

 for (int i = 0; i < currentNumBalls; i++) {
    try {
        this.aBall[i] = (Ball) Naming.lookup("rmi://localhost/BouncingBall"+i);

    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}
dogbane
ah,my assumption was wrong, and i thought its not the efficient way, can u think of any other way to accomplish this task.
Static Void Main