import java.net.*; import java.io.*; public class pongServer { public static boolean listening = true; public static long lastTime = 0; public static pongIdentity myID = new pongIdentity(); public static void main(String[] args) throws IOException { if (args.length != 2) { System.err.println("Usage: java pongServer clientName portNumber"); System.exit(1); } // // The pongServer has this task: // // 1. Log into the masterServer. // 2. Listen for client requests and spawn off a thread to handle it. // 3. When asked, log off the masterServer and shut down. // Socket masterServerSocket = null; ObjectInputStream serialIn = null; ObjectOutputStream serialOut = null; try { masterServerSocket = new Socket(Pong.MASTERHOST, Pong.MASTERPORT); serialOut = new ObjectOutputStream(masterServerSocket.getOutputStream()); serialIn = new ObjectInputStream(masterServerSocket.getInputStream()); } catch (UnknownHostException e) { System.err.println("Host unknown : " + e.getMessage()); System.exit(1); } catch (IOException e) { System.err.println("IO exception : " + e.getMessage()); System.exit(1); } // First, attempt to log into the masterServer. myID.clientName = args[0]; myID.myIP = InetAddress.getLocalHost(); try { myID.myPort = Integer.parseInt(args[1]); } catch (NumberFormatException e) { System.err.println("Illegal port number - " + e.getMessage()); System.exit(1); } serialOut.writeObject(new Integer(Pong.SERVER_LOGIN)); serialOut.writeObject(myID); int response = 0; if ((response = readResponse(serialIn)) != Pong.SERVER_ACK) { System.err.println("pongServer: No ACK from LOGIN: " + response); System.exit(1); } // // Close the masterServer connection so that pongServerThread // can re-establish a separate link to the masterServer. // // Note that since SERVER_LOGIN was sent this pongServer is already // "logged in" to the masterServer, despite closing this socket. // serialIn.close(); serialOut.close(); masterServerSocket.close(); // // Next, listen for pongClient requests. // ServerSocket serverSocket = null; listening = true; try { serverSocket = new ServerSocket(myID.myPort); } catch (IOException e) { System.err.println("pongServer: could not listen: " + e.getMessage()); System.exit(1); } // // Start up my idle time monitoring thread. // // if (!myID.clientName.equals("Jeff Bauer")) { lastTime = System.currentTimeMillis(); idleThread idle = new idleThread(myID); idle.start(); // } while (listening) { new pongServerThread(myID, serverSocket.accept()).start(); lastTime = System.currentTimeMillis(); if (!listening) break; } serverSocket.close(); // // Log out of the masterServer. // try { masterServerSocket = new Socket(Pong.MASTERHOST, Pong.MASTERPORT); serialOut = new ObjectOutputStream(masterServerSocket.getOutputStream()); serialIn = new ObjectInputStream(masterServerSocket.getInputStream()); } catch (UnknownHostException e) { System.err.println("pongServer: Host unknown : " + e.getMessage()); System.exit(1); } catch (IOException e) { System.err.println("pongServer: IO exception : " + e.getMessage()); System.exit(1); } serialOut.writeObject(new Integer(Pong.SERVER_LOGOUT)); serialOut.writeObject(myID); response = 0; response = readResponse(serialIn); // // Two possible correct error codes -- either the LOGOUT worked or I // was auto-logged anyway from a masterServer timeout. // if ((response != Pong.SERVER_ACK) && (response != Pong.SERVER_NOT_LOGGED_IN)) { System.err.println("pongServer: No ACK from LOGOUT: " + response); System.exit(1); } serialIn.close(); serialOut.close(); masterServerSocket.close(); } static int readResponse(ObjectInputStream serialIn) { int response = 0; try { response = ((Integer) serialIn.readObject()).intValue(); } catch (IOException e) { return Pong.CLIENT_READ_FAILED; } catch (ClassNotFoundException e) { return Pong.CLIENT_READ_FAILED; } return response; } }