import java.io.*; import java.net.*; import java.util.*; // // This is the protocol between the master server and the // individual servers. // public class pongProtocol { // // processCommand -- process the command from the client. // public static boolean processCommand(int command, ObjectInputStream serialIn, ObjectOutputStream serialOut, grimReaperThread reaper) { pongIdentity pongID; // // All objects must first contain a pongIdentity object so we don't // have to keep track of who's coming from where. // if ((pongID = getPongID(reaper, serialIn)) == null) { sendCommand(Pong.SERVER_NAK, serialOut); return false; } switch (command) { case Pong.SERVER_LOGIN: if (reaper.serverListGet(pongID.clientName) != null) { sendCommand(Pong.SERVER_ALREADY_LOGGED_IN, serialOut); return false; } reaper.serverListPut(pongID.clientName, pongID); sendCommand(Pong.SERVER_ACK, serialOut); break; case Pong.SERVER_LOGOUT: if (reaper.serverListGet(pongID.clientName) == null) { sendCommand(Pong.SERVER_NOT_LOGGED_IN, serialOut); return false; } sendCommand(Pong.SERVER_ACK, serialOut); reaper.serverListRemove(pongID.clientName); break; case Pong.SERVER_LIST: if (reaper.serverListGet(pongID.clientName) == null) { sendCommand(Pong.SERVER_NOT_LOGGED_IN, serialOut); return false; } try { serialOut.writeObject(reaper.serverList); } catch (IOException e) { sendCommand(Pong.SERVER_NAK, serialOut); } break; case Pong.SERVER_PING: sendCommand(Pong.SERVER_ACK, serialOut); break; default: sendCommand(Pong.SERVER_NAK, serialOut); break; } return true; } static void sendCommand(int command, ObjectOutputStream serialOut) { try { serialOut.writeObject(new Integer(command)); } catch (IOException e) { return; } } static pongIdentity getPongID(grimReaperThread reaper, ObjectInputStream serialIn) { pongIdentity pongID; try { pongID = (pongIdentity) serialIn.readObject(); } catch (IOException e) { return null; } catch (ClassNotFoundException e) { return null; } reaper.serverListUpdate(pongID.clientName); // update last time heard from return pongID; } }