import java.net.*; import java.io.*; import java.applet.*; public abstract class PS10Applet extends DoubleBufferApplet { int clientID = -1; Socket s; DataInputStream in; PrintStream out; PS10Listener listener; String serverHost; public static final int PORT = 6800; // override this to provide specific applet behaviour. // You get messages for your own ID as well. public abstract void receiveMessage(int otherID, int x, int y); // use this method to send out (ID labelled) messages public void sendMessage(int x, int y) { if (out != null) out.println(clientID + " " + x + " " + y); else System.out.println("Tried to send a message, but wasn't connected."); } public void init() { System.out.println("Init called"); serverHost = this.getCodeBase().getHost(); super.init(); } // start - open a socket to the same host as we're on at the given port // create some streams and a listener object to handle input stream public void start() { System.out.println("Start called"); try { s = new Socket(serverHost, PORT); in = new DataInputStream(s.getInputStream()); out = new PrintStream(s.getOutputStream()); listener = new PS10Listener(this, in); System.out.println("Connected to " + s.getInetAddress().getHostName() + ":" + s.getPort()); } catch (IOException e) { System.out.println(e.toString()); } super.start(); } // Stop: notify the server, tell our listener thread to stop, and destroy // all the thread objects (to catch possible errors) public void stop() { System.out.println("stop called - disconnecting from server."); sendMessage(-99, -99); out.println("BYE"); listener.stop(); listener = null; in = null; out = null; s = null; super.stop(); } public void setID(int id) { if (clientID == -1) { clientID = id; System.out.println("ID set to " + id); } else { System.out.println("Refused to set our ID to " + id); } } } // Object to handle socket input. Parses messages and routes them back // through the applet call interface class PS10Listener extends Thread { DataInputStream in; PS10Applet applet; public PS10Listener(PS10Applet ac, DataInputStream in) { this.in = in; this.applet = ac; this.start(); } public void run() { String line; try { while (true) { line = in.readLine(); if (line == null) break; else if (line.startsWith("0 WELCOME")) // our welcome message - get ID applet.setID(Integer.parseInt(line.substring(10))); // skip 0 WELCOME else { // must be a foreign mouseclick int p1, p2, p3, id, x, y; try { p1 = line.indexOf(' ', 0); // first space p2 = line.indexOf(' ', p1+1); // second space p3 = line.length(); // end of string id = Integer.parseInt(line.substring(0, p1)); x = Integer.parseInt(line.substring(p1+1, p2)); y = Integer.parseInt(line.substring(p2+1, p3)); applet.receiveMessage(id, x, y); } catch (StringIndexOutOfBoundsException e) { System.out.println("Error parsing input " + line); } catch (NumberFormatException n) { System.out.println("Error parsing input " + line); } } } } catch (IOException e) { System.out.println(e.toString()); } finally { System.out.println("PS10 Listener thread died - were we stopped?"); } } }