/* * Created on 2005-apr-08 */ import java.rmi.*; import java.util.*; import java.lang.Thread; import java.lang.Runnable; import ip4.ass211.client.Client211GUI; import ip4.ass211.client.BallActionEvent; import ip4.ass211.client.BallActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /** * @author Henrik Johansson */ public class Client211 implements BallActionListener { /** * Bollsevern som klienten är ansluten till */ protected RemoteServer server; /** * platsen servern finns på */ private String host; /** * serverns portnummer */ private int port; /** * namnet på servicen */ private String name; /** * Det grafiska användargreänssnittet som användaren använder för at * interaera med servern */ protected Client211GUI gui; public Client211(String name) { this(name, "127.0.0.1"); } public Client211(String name, String host) { this(name, host, 1099); } public Client211(String name, String host, int port) { this.host = host; this.port = port; this.name = name; setup(); gui = new Client211GUI(); gui.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { close(); } }); gui.addBallActionListener(this); new Thread(new Runnable() { public void run() { while(true) { gui.setBalls(getBalls()); try { Thread.sleep(25); } catch (InterruptedException e) { System.err.println("trouble sleeping"); } } } }).start(); gui.setVisible(true); } /** * @see ip4.ass211.client.BallActionListener#ballAction(ip4.ass211.client.BallActionEvent) */ public void ballAction(BallActionEvent e) { if(e.getType() == BallActionEvent.ADD_BALLS) { addBalls(e.getNumberOfBalls()); } else if(e.getType() == BallActionEvent.PAUSE_BALLS) { pause(); } } /** * Anropas innan klienten avslutas för eventuella upprensningar. */ protected void close() { System.out.println("Client211.close()"); } /** * initerar kontakten med serverm */ protected void setup() { try { String lookup = "//"+host+":"+port+"/"+name; System.out.println(lookup); server = (RemoteServer) Naming.lookup(lookup); } catch(Exception e) { e.printStackTrace(); System.exit(1); } } /** * Hämtar bollar (positioner och storlekar) som finns på servern * * @return vektor med serverns bollpositioner */ public Vector getBalls() { try { return server.getBalls(); } catch (RemoteException e) { e.printStackTrace(); System.exit(2); } return null; } /** * Lägger till bollar på servern * * @param num antal bollar som skall läggas till på servern. */ public void addBalls(int num) { while(num-- > 0) { try { server.addBall(); } catch(RemoteException e) { e.printStackTrace(); System.exit(3); } } } /** * växlar pausläge för bollarna på servern. pauserade bollar sätts i rörelse, rörliga * bollar pauseras. */ public void pause() { try { server.pauseBalls(); } catch (RemoteException e) { e.printStackTrace(); System.exit(4); } } /** * usage: Client211 <servicename> [<host> [<port>]] * * @param args kommandoargumenten, [ []] * @throws Exception */ public static void main(String[] args) throws Exception { if(args.length == 0) { System.err.println("usage: Client211 [ []]"); } else if(args.length == 1) { new Client211(args[0]); } else if(args.length == 2) { new Client211(args[0], args[1]); } else { new Client211(args[0], args[1], Integer.parseInt(args[2])); } } }