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(); private static boolean runsLocal = false; private Vector remoteClients = new Vector(); public static void main(String[] args) throws RemoteException { if(args.length > 0) { runsLocal = true; } new Server(); } public Server() throws RemoteException { try { // Gör dig tillgänglig utåt if(runsLocal) { LocateRegistry.createRegistry(1099); } else { LocateRegistry.getRegistry(1099); } Naming.rebind("server_callbacks", this); System.out.println("Server bound"); } catch(Exception e) { System.out.println("Exception generated: " + e.getMessage()); } new ClientSender(); } 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++) { 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; } public void registerClient(String host) throws RemoteException { System.out.println("Client registred: " + host); remoteClients.add(host); } public void unregisterClient(String host) throws RemoteException { System.out.println("Client unregistred: " + host); remoteClients.remove(host); } 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) {} } } } class ClientSender extends Thread { public ClientSender() { start(); } public void run() { while(true) { // Sänd något till alla klienter for(int i = 0; i < remoteClients.size(); i++) { try { // Fixa en referens till klienten String host = (String)remoteClients.elementAt(i); String url = "rmi://" + host + "/"; RemoteClient remoteClient = (RemoteClient)Naming.lookup(url + "client_callbacks"); remoteClient.showServerClients("Connected clients " + (new Date()).toString() + ":"); for(int j = 0; j < remoteClients.size(); j++) { remoteClient.showServerClients((String)remoteClients.elementAt(i)); } } catch(Exception e) { remoteClients.remove(i); System.out.println("Exception generated: " + e.getMessage()); } } try { sleep(1000); } catch(Exception e) {} } } } }