import java.util.*; import java.rmi.*; import java.rmi.registry.*; import java.rmi.server.*; class Server implements RemoteServer{ private Vector balls = null; public Server(){ balls = new Vector(); } public synchronized void addBall() throws RemoteException { balls.add(new Ball()); } private synchronized void removeBall(Ball ball) { balls.remove(ball); } public synchronized void pauseBalls() throws RemoteException { for(int i = 0; i < balls.size(); i++) { Ball ball = (Ball)balls.elementAt(i); ball.pause(); } } 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 int xLim = 500, yLim = 500; 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 pause(){ if (active == true) active = false; else active = true; } 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) {} } } } public static void main(String[] args){ /*if (System.getSecurityManager() == null) { System.setSecurityManager(new SecurityManager()); }*/ try { String name = "server"; RemoteServer serv = new Server(); RemoteServer stub = (RemoteServer) UnicastRemoteObject.exportObject(serv, 0); Registry registry = LocateRegistry.getRegistry(); registry.rebind(name, stub); System.out.println("Server bound"); } catch (Exception e) { System.err.println("Server exception:"); e.printStackTrace(); } } }