import java.util.*; import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.*; public class Server extends UnicastRemoteObject implements RemoteServer { private int xLim = 500, yLim = 500; private Vector balls = new Vector(); public static void main(String[] args) { try { Server server = new Server(); LocateRegistry.createRegistry(1099); Naming.rebind("server", server); System.out.println("server bound"); } catch(Exception e) { System.out.println("Exception generated: " + e.getMessage()); } } public Server() throws RemoteException {} public synchronized void addBall() throws RemoteException { balls.add(new Ball()); } private synchronized void removeBall(Ball ball) { balls.remove(ball); } public synchronized void pauseBalls() { for(int i = 0; i < balls.size(); i++) { if(((Ball)balls.elementAt(i)).active) { ((Ball)balls.elementAt(i)).active = false; } else { ((Ball)balls.elementAt(i)).active = true; } } } public synchronized Vector getBalls() throws RemoteException { Vector ballsRaw = new Vector(); for(int i = 0; i < balls.size(); i++) { Ball ball = (Ball)balls.elementAt(i); ballsRaw.add(new Integer(ball.x)); ballsRaw.add(new Integer(ball.y)); ballsRaw.add(new Integer(ball.r)); } return ballsRaw; } class Ball extends Thread { private boolean alive = true, active = true; private Random rnd = new Random(); public int rMax = 70, r = rnd.nextInt(rMax), x = xLim / 2 - r / 2, y = yLim / 2 - r / 2, dx, dy, dMax = 5, ddx = 1, ddy = 1; Ball() { if(rnd.nextInt(2) == 1) ddx = -1; dx = (1 + rnd.nextInt(dMax)) * ddx; if(rnd.nextInt(2) == 1) ddy = -1; dy = (1 + rnd.nextInt(dMax)) * ddy; start(); } public void run() { while(alive) { while(active) { if((x <= 0) || (x >= xLim - r)) { dx *= -1; r -= 2; } if((y <= 30) || (y >= yLim - r)) { dy *= -1; r -= 3; } if(r <= 0) { active = false; alive = false; removeBall(this); } else { x += dx; y += dy; } try { sleep(10 + r); } catch(Exception e) {} } try { sleep(50); } catch(Exception e) {} } } } }