home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / ChessServer.java < prev    next >
Encoding:
Java Source  |  1997-07-03  |  3.3 KB  |  122 lines

  1. package borland.samples.apps.chess.server;
  2.  
  3. import java.net.*;
  4. import java.util.*;
  5. import java.io.*;
  6. import borland.samples.apps.chess.client.ServerMessage;
  7.  
  8. public class ChessServer extends Thread
  9. {
  10.   static Hashtable playerlist = new Hashtable();
  11.   static Hashtable gamelist = new Hashtable();
  12.   static boolean availablePort[];
  13.   static int maxConnections = 50;
  14.   final static int basePort = 101;
  15.   static boolean shutdown = false;
  16.   static int warningTime = 2;
  17.   static int socketnum ;
  18.  
  19.   public static void main(String args[]) {
  20.     ChessServer m = new ChessServer();
  21.     for (int i = 0;i< args.length;i++) {
  22.       if (args[i].startsWith("connections=")) {
  23.         try {
  24.           maxConnections = Integer.parseInt(args[i].substring(12));
  25.         }
  26.         catch (Exception e){
  27.           System.out.println("connections param " + args[i] + " not a valid int");
  28.         }
  29.       }
  30.       else
  31.         System.out.println("don\'t know what to do with " + args[i]);
  32.  
  33.     }
  34.     socketnum = basePort;
  35.     m.start();
  36.   }
  37.  
  38.   static void portIdle(int portNum) {
  39.     System.out.println("giving back port " + portNum);
  40.     availablePort[portNum - basePort] = true;
  41.   }
  42.  
  43.   static int getPortNumber() {
  44.     for (int i=socketnum - basePort;i<maxConnections;i++) {
  45.       if (availablePort[i]) {
  46.         socketnum = i + basePort + 1;
  47.         availablePort[i] = false;
  48.         return i + basePort;
  49.       }
  50.     }
  51.     System.out.println("looking from the beginning");
  52.     for (int i=0;i<maxConnections;i++) {
  53.       if (availablePort[i]) {
  54.         socketnum = i + basePort + 1;
  55.         availablePort[i] = false;
  56.         return i + basePort;
  57.       }
  58.     }
  59.     return 0;
  60.   }
  61.  
  62.   public void run() {
  63.     File shutdownfile = new File("shutdown.txt");
  64.     if (shutdownfile.exists())  
  65.       shutdownfile.renameTo(new File("shutdown.xyz"));
  66.     PlayerId.ReadUserFile();
  67.     Thread h = new HelloThread();
  68.     h.start();
  69.     while (true)  {
  70.       synchronized (this) {
  71.         try {
  72.           wait(60000);   //60000 = one minute
  73.         }
  74.         catch (InterruptedException e) {
  75.         }
  76.       }
  77.       if (shutdownfile.exists())  {
  78.         System.out.println("we be shutting down ");
  79.         shutdown = true;
  80.         synchronized (h) {
  81.           h.stop();
  82.         }
  83.         SendWarning (shutdownfile);
  84.         SuspendGames();
  85.         PlayerId.WriteUserList();
  86.         break;
  87.       }
  88.       PlayerId.WriteUserList();
  89.     } //end of while (true)
  90.     System.out.println("TTFN");
  91.     System.exit(0);
  92.   }
  93.  
  94.   public void SuspendGames(){
  95.     SendThread np;
  96.     for (Enumeration e = ChessServer.playerlist.elements();e.hasMoreElements();){
  97.       np = (SendThread)e.nextElement();
  98.       np.addMsg(0,"Bye","");
  99.     }
  100.   }
  101.  
  102.   public void SendWarning(File shutdownFile){
  103.     //could read the delay time out of the shutdown file
  104.     SendThread np;
  105.     System.out.println("tell people they have " + warningTime + " minutes");
  106.     for (Enumeration e = ChessServer.playerlist.elements() ; e.hasMoreElements() ;) {
  107.       np = (SendThread)e.nextElement();
  108.       if (np != null ) {
  109.         np.addMsg(0,"Status","Chess Server shutting down in "+ warningTime + " minutes - sorry");
  110.       }
  111.     }
  112.     synchronized (this) {
  113.     try {
  114.       wait(warningTime * 60000);
  115.     }
  116.     catch (InterruptedException e) {}
  117.     }
  118.   }
  119. } //end of ChessServer class
  120.  
  121.  
  122.