FileDocCategorySizeDatePackage
ChessServer.javaAPI DocExample4321Sat Sep 12 03:01:00 BST 1998borland.samples.apps.chess.server

ChessServer

public class ChessServer extends Thread

Fields Summary
static Hashtable
playerlist
static Hashtable
gamelist
static boolean[]
availablePort
static int
maxConnections
static final int
basePort
static boolean
shutdown
static int
warningTime
static int
socketnum
Constructors Summary
Methods Summary
static intgetPortNumber()

    for (int i=socketnum - basePort;i<maxConnections;i++) {
      if (availablePort[i]) {
        socketnum = i + basePort + 1;
        availablePort[i] = false;
        return i + basePort;
      }
    }
    System.out.println("looking from the beginning");
    for (int i=0;i<maxConnections;i++) {
      if (availablePort[i]) {
        socketnum = i + basePort + 1;
        availablePort[i] = false;
        return i + basePort;
      }
    }
    return 0;
  
public static voidmain(java.lang.String[] args)


       
    ChessServer m = new ChessServer();
    for (int i = 0;i< args.length;i++) {
      if (args[i].startsWith("connections=")) {
        try {
          maxConnections = Integer.parseInt(args[i].substring(12));
        }
        catch (Exception e){
          System.out.println("connections param " + args[i] + " not a valid int");
        }
      }
      else
        System.out.println("don\'t know what to do with " + args[i]);

    }
    socketnum = basePort;
    m.start();
  
static voidportIdle(int portNum)

    System.out.println("giving back port " + portNum);
    availablePort[portNum - basePort] = true;
  
public voidrun()

    File shutdownfile = new File("shutdown.txt");
    if (shutdownfile.exists())  
      shutdownfile.renameTo(new File("shutdown.xyz"));
    PlayerId.readUserFile();
    Thread h = new HelloThread();
    h.start();
    while (true)  {
      synchronized (this) {
        try {
          wait(60000);   //60000 = one minute
        }
        catch (InterruptedException e) {
        }
      }
      if (shutdownfile.exists())  {
        System.out.println("we be shutting down ");
        shutdown = true;
        synchronized (h) {
          h.stop();
        }
        sendWarning (shutdownfile);
        suspendGames();
        PlayerId.writeUserList();
        break;
      }
      PlayerId.writeUserList();
    } //end of while (true)
    System.out.println("TTFN");
    System.exit(0);
  
public voidsendWarning(java.io.File shutdownFile)

    //could read the delay time out of the shutdown file
    SendThread np;
    System.out.println("tell people they have " + warningTime + " minutes");
    for (Enumeration e = ChessServer.playerlist.elements() ; e.hasMoreElements() ;) {
      np = (SendThread)e.nextElement();
      if (np != null ) {
        np.addMsg(0,"Status","Chess Server shutting down in "+ warningTime + " minutes - sorry");
      }
    }
    synchronized (this) {
    try {
      wait(warningTime * 60000);
    }
    catch (InterruptedException e) {}
    }
  
public voidsuspendGames()

    SendThread np;
    for (Enumeration e = ChessServer.playerlist.elements();e.hasMoreElements();){
      np = (SendThread)e.nextElement();
      np.addMsg(0,"Bye","");
    }